I have an API that I am working on written in Python. The API is using a set of exposed functions from a C DLL. When the DLL is loaded is allocates a lot of memory and sets up several things on the DLL side, then returns a Python class. I need to load a fixture with a file path and an integer, return a class object, and pass that object to all of my test classes. I have been building this globally without fixtures by creating a global class and allowing all my test functions to use that class. This works fine for a single case. Since working with the DLL takes a lot of resources, I only want to build each fixture one time and then run all of my test classes. In my example I load case1.txt to the DLL, use the object in all my tests, then load case2.txt, run all my tests. In the final version I will be loading these cases from an external source and there could be hundreds. There are multiple test classes.
I've tried several test cases with fixtures and @pytest.mark.parametrize. I am able to build and use a fixture, but I'm having issues passing all the parameters to all the tests. I have provided an example of what I have tried recently and that is not working, but gives the idea of where I would like to end up.
import os
import pytest
import apiModule as pt
case_dirp = r'C:\path\to\cases\'
case_fp1 = os.path.join(case_dirp, "case1.txt")
case_fp2 = os.path.join(case_dirp, "case2.txt")
case_fps.append((case_fp1, 13))
case_fps.append((case_fp2, 14))
@pytest.fixture
def load_api_fixture(request, fn=None, ver=None):
myApi = pt.API(dllFilePath=r'C:\path\to\dll\apiDLL.dll')
myApi.loadCase(caseFilePath=fn, Ver=ver)
yield myApi
@pytest.mark.parametrize(
'load_api_fixture',
case_fps,
indirect=True
)
class TestClass():
def test_gen_figs(load_api_fixture):
assert load_api_fixture.numFigs() > 0
def test_gen_apples(load_api_fixture):
assert load_api_fixture.numApples() > 0
class TestClass2():
def test_gen_oranges(load_api_fixture):
assert load_api_fixture.numOranges() > 0
def test_gen_grapes(load_api_fixture):
assert load_api_fixture.numGrapes() > 0
if __name__ == "__main__":
pytest.main([__file__])
I don't have any specific results as shown, i have several failed examples. I am actually running pytest using the command line scripts, but this setup helps me to debug into the functions to find the parameter names. There are several named parameters in pytest, and I'm trying to figure out the correct configuration. I am unable to share my actual test code. I would like to see the standard pytest output for my 60+ tests for each case I would like to load.
Aucun commentaire:
Enregistrer un commentaire