Home > Blockchain >  How to load and use iterable data object in Pytest?
How to load and use iterable data object in Pytest?

Time:01-20

I'm writing my first code test ever for the data processing step of an ML project using Pytest 6.2.5. The code is made up of some functions (I haven't written a class). I want to load some test data for each step of the process and check whether the functions return what they are supposed to. I'd like to save the sample data as a dictionary so I can loop through like:

def test_myfunc():
    for datum in data:
        newProperty = myfunc(dataum['property'])
        assert testData[datum['idNumber']]['newProperty'] == newProperty

I could only do this if I read the testData from file at each test function, which seems stupid. If I write a fixture to return the testData dictionary, it is still treated as a function as "E TypeError: 'function' object is not subscriptable."

What am I doing wrong? In all the tutorials they just define a dummy data inside the fixture so I haven't found an example yet.

Thanks heaps!

Update: I should have written exactly how I am writing and using the fixture. Here's an example:

@pytest.fixture()
def actual_oxide_check():
    with open('testDataDir/oxide_check_dict.pkl', 'rb') as f:
        oxide_check_dict = pickle.load(f)
    return oxide_check_dict

def test_oxide_check(actual_oxide_check):
    mytestData = np.load('testData.npy', allow_pickle=True)
    for Tdatum in mytestData:
        other_anion, other_oxidation, bad_structure, primStruc = oxide_check(Tdatum['structure'])
        assert other_anion == actual_oxide_check[Tdatum['material_id']]['other_anion']

Here I load mytestData. I'd have a similar problem with that if I use a fixture. "Function is not iterable".

CodePudding user response:

you need to supply the fixture as an argument to your test.

@pytest.fixture(scope="session")
def test_data():
    # code to load data
    return data_dict

def test_myfunc(test_data):
    for datum in data:
        new_property = myfunc(datum['property'])
        assert test_data[datum['id_number']]['new_property'] == new_property

CodePudding user response:

Ok, I might have figured out what I've done wrong. When you write test functions with pytest, do you actually call those functions? Cos if I don't call them, they pass :D Sorry, first time writing a test!

  •  Tags:  
  • Related