I have a test suite, wherein
- Master class Regression holds generic test functions, which are inherited by child classes
- I am dynamically parameterizing the test variables during initialization using pytest_generate_tests hook.
- The test initialization depends on the child class which is being run Eg. Test_window will have diff initialization Test_door will have diff initialization
My problem is:
- For both the test cases I use same parameter
- So the pytest_generate_tests hook gets called 2 times to generate the same test data ( i guess, couldn't find it in docs !!!)
- Is there a better way of implementing this scenario to avoid this overhead of calling the pytest_generate_tests 2 times.
Parent Class :
class Regression:
def test_work(self, data):
test_func1_call_using data(data)
def test_agg(self, data):
test_func2_call_using data(data)
Test Class 1 inherting regression :
@pytest.mark.usefixtures('generic_test_setup')
class Test_window(Regression):
dummy_var=0
Test Class 2 inherting regression :
@pytest.mark.usefixtures('generic_test_setup')
class Test_door(Regression):
dummy_var=0
conftest.py :
@pytest.fixture(scope="class")
def generic_test_setup(request, env):
// do setup
def pytest_generate_tests(metafunc):
class_name = metafunc.cls.__name__.split('_')[1]
logging.info(f"Collection Phase :: Initializing test_data for {class_name} !!!")
// Initialize the test data based on the calling class
test_data = //generated dictionary value with ids and test data
id_entity = [d['entity'] for d in test_data]
if "data" in metafunc.fixturenames:
metafunc.parametrize("data", test_data, ids=id_entity )
@pytest.fixture
def data(request):
return request.param
CodePudding user response:
As mentioned in the comments, the behavior of pytest_generate_tests
is as expected, as it is called for each test. If you want to cache your test data, you can just add a cache outside of the hook, e.g. something like:
testdata_cache = {}
def pytest_generate_tests(metafunc):
if "data" in metafunc.fixturenames:
class_name = metafunc.cls.__name__.split('_')[1]
if class_name not in testdata_cache:
// Initialize the test data based on the calling class
testdata_cache[class_name] = //generated dictionary value with ids and test data
test_data = testdata_cache[class_name]
id_entity = [d['entity'] for d in test_data]
metafunc.parametrize("data", test_data, ids=id_entity )
If you don't want to use a global variable, you could wrap this into a class, but this would be overkill in this case.