繁体   English   中英

如何在 pytest 中的 setup_function 和 teardown_function 中使用夹具

[英]How to use fixtures within setup_function and teardown_function in pytest

我有一个测试,我需要为每个测试 function 运行设置/拆卸:

class A:
    def setup(self):
        return 'setup'

    def teardown(self):
        return 'teardown'

@pytest.fixture(scope='function')
def a():
    return A()

def setup_function(a):
    logger.info(a.setup())

def test_a(a):
    logger.info('test_a')

def teardown_function(a):
    logger.info(a.teardown())

a包含 function test_a而不是 setup_function 和 teardown_function 中的夹具。 如何在这些功能中访问夹具a

我发现可以使用另一个夹具来包装测试 function:

@pytest.fixture
def a():
    return A()

@pytest.fixture(scope='function')
def wrapper(a):
    a.setup()
    yield
    a.teardown()

def test_a(wrapper):
    logger.info('test_a')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM