Search code examples
pythonpytestfixtures

pytest fixture in setup_method


I have a function scoped fixture.

import pytest


@pytest.fixture(scope="function")
def some_fixture(req):
    print "This is not the end"
    return "okay"

Is it possible to invoke the fixture in the setup_method somehow.?

Something like the following snippet...

class TestOne(object):
    def setup_method(self, method, some_fixture): # this would give argument error
        print "okay"

    def test_one(self):
        pass

I know fixture would work same as setup_method, but I have got a very corner case scenario and I want the fixture to be executed in setup_method itself.


Solution

  • You can mark your setup_method as fixture & then call it in your tests.

    class TestOne(object):
    
        @pytest.fixture(scope="class")
        def setup_method(self, method, some_fixture): # this would give argument error
            print "okay"
    
        def test_one(self, setup_method):
            pass