这取决于您要发出什么数据。如果一个简单的完成检查就足够了,请在conftest.py 文件中编写一个自定义的pytest_runtest_logfinish 挂钩,因为它直接提供大量测试信息:
def pytest_runtest_logfinish(nodeid, location):
(filename, line, name) = location
print('finished', nodeid, 'in file', filename,
'on line', line, 'name', name)
如果您需要访问测试结果,自定义pytest_runtest_makereport 是一个不错的选择。您可以从item 参数中获得与上述相同的测试信息(以及更多信息):
import pytest
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
report = yield
result = report.get_result()
if result.when == 'teardown':
(filename, line, name) = item.location
print('finished', item.nodeid, 'with result', result.outcome,
'in file', filename, 'on line', line, 'name', name)
您也可以按照 cmets 中的建议使用夹具拆卸选项:
@pytest.fixture(autouse=True)
def myhook(request):
yield
item = request.node
(filename, line, name) = item.location
print('finished', item.nodeid, 'in file', filename,
'on line', line, 'name', name)
但是,这取决于您希望 webhook 何时发出 - 上面的自定义 hookimpls 将在测试完成并且所有固定装置都已完成时运行,而对于固定装置示例,您不能保证所有固定装置都已完成,因为没有明确的夹具顺序。此外,如果您需要测试结果,您无法在夹具中访问它。