Pytest:在setup_method中使用固定装置

时间:2019-03-08 17:33:23

标签: 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`?

1 个答案:

答案 0 :(得分:0)

您必须切换到Houses.color = blue样式的夹具才能访问夹具。与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' 固定装置将在类中为每种测试方法调用一次