2016-11-17 81 views
1

Pytesthassetup and teardowns钩子为module, class, methodPytest设置/拆卸会话的钩子

我想在安装程序(开始测试会话之前)中创建自定义测试环境,并在所有测试完成后进行清理。 换句话说,我怎样才能使用像setup_session and teardown_session这样的挂钩?

+0

http://stackoverflow.com/questions/23667610/difference-between -setupclass-and-setup-in-python-unittest –

+0

@AriGold。其实我不使用单元测试课。我正在编写使用pytest fixture的测试函数。我在链接中看到了setup_class/teardown_class用法的示例。但我正在寻找诸如setup_session/teardown_session之类的东西。 –

回答

4

这些钩子为我工作很好:

def pytest_sessionstart(session): 
    # setup_stuff 

def pytest_sessionfinish(session, exitstatus): 
    # teardown_stuff 

但实际上与会话范围下夹具看起来更漂亮:

@fixture(autouse=True, scope='session') 
def my_fixture(): 
    # setup_stuff 
    yield 
    # teardown_stuff