using pytest.fixture on setup_method

Viewed 1196

Is it possible to use pytest.fixture on setup_method so some operation can be always finish between each testcase? I have tried to use fixture like following and the structure looks like ok. I am able to execute each testcase before funcA is completed. However, I do not want to include ``` @pytest.mark.usefixtures('funcA')


    class TestSuite(TestBase):
        @classmethod
        def setup_class(cls):
            super(TestSuite, cls).setup_class()
            'do something for setup class'

        @classmethod
        def teardown_class(cls):
            super(TestSuite, cls).teardown_class()
            'do something for teardown class'


        def setup_method(self):
            'do log in'
            'do a,b c for setup_method'

        @pytest.fixture(scope="function")
        def funcA(self):
            print('do function A')

        @pytest.mark.usefixtures('funcA')
        def test_caseA(self):
            'check a'

        @pytest.mark.usefixtures('funcA')
        def test_caseB(self):
            'check b'

        @pytest.mark.usefixtures('funcA')
        def test_caseC(self):
            'check c'
1 Answers

You can just pass the fixture as argument to the test:

def test_caseA(self, funcA):
    'check a'

def test_caseB(self, funcA):
    'check b'

def test_caseC(self, funcA):
    'check c'
Related