遵循以下模式: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`?

最佳答案

您必须切换到pytest样式的夹具才能访问夹具。等效的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'


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

关于python - Pytest:在setup_method中使用固定装置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55068240/

10-16 22:45