pythonunit-testingtestingpytestfixtures

pytest fixtures in nested Classes


I have written the following testcase using pytest.

import pytest
data_arg = ["arg1", "arg2"]

class TestParentClass1:
    @pytest.fixture(scope="class", params=data_arg,autouse=True)
    def common_setup(self, request):
      print(f'Configure the system according to {request.param}')

    class TestClass1:
        def test_class1_test1(self):
            print("Executing test1 of class1")

        def test_class1_test2(self):
            print("Executing test2 of class1")

    class TestClass2:
        def test_class2_test1(self):
            print("Executing test1 of class2")

        def test_class2_test2(self):
            print("Executing test2 of class2")

I have the following requirements:-

I am unable to achieve this flow. Can somebody please help me on how can I do it?


Solution

  • You can try with changing scope=class to scope=module

    @pytest.fixture(scope="module", params=data_arg,autouse=True)