Pytest:在 setup_method 中使用装置

X_T*_*ust 5 python pytest

遵循此模式:https : //docs.pytest.org/en/latest/xunit_setup.html

我如何使用/注射fixture_foo进入setup_method

class TestClassX:
    def setup_method(self, method):
        # I need `fixture_foo` here

    def teardown_method(self, method):
        # N/A

    def test_cool(self, fixture_foo):
        # Why can `fixture_foo` be injected here and not in `setup_method`?

Run Code Online (Sandbox Code Playgroud)

Ant*_*ile 11

您必须切换到pytest-style 固定装置才能访问固定装置。setup_method可以通过如下方式实现等效的:

@pytest.fixture
def f():
    return 'ohai'


class Test:
    @pytest.fixture(autouse=True)
    def setup_method_fixture(self, request, f):
        self.f = f
        self.method_name = request.function.__name__

    def test(self):
        assert self.method_name == 'test'
        assert self.f == 'ohai'
Run Code Online (Sandbox Code Playgroud)

autouse装置将在类内的每个测试方法中被调用一次