2

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'
François B.
  • 1,096
  • 7
  • 19
jacobcan118
  • 7,797
  • 12
  • 50
  • 95

1 Answers1

1

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'
Amit
  • 19,780
  • 6
  • 46
  • 54
  • yes, but i wonder if i can just use that in setup_method because funcA is executed between each testcase – jacobcan118 Oct 09 '17 at 16:25
  • I guess you don't need a fixture. You can do in setup itself whatever you are trying to do with fixture. – Amit Oct 09 '17 at 16:51
  • but my funcA is actually a function inherit from other other class. so i do not want to duplicated the code in setup_method but just using it – jacobcan118 Oct 09 '17 at 17:24
  • whatever you are doing inside the fixture, you should be able to do the same in the set up method. – Amit Oct 09 '17 at 18:07