繁体   English   中英

每次测试执行后是否可以让 pytest 调用 webhook?

[英]Is it possible to have pytest call a webhook after each test executed?

我们使用 py.test 来执行我们的集成测试。 由于我们有相当多的测试,我们希望在我们使用的仪表板中监控进度。

是否可以配置 webhook 或 pytest 将调用的每个测试执行的结果(通过/失败/跳过)的东西?

我确实找到了 teamcity 集成,但我们更愿意在不同的仪表板上监控进度。

这取决于您要发出的数据。 如果简单的完成检查就足够了,请在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)

您还可以按照评论中的建议使用夹具拆卸选项:

@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 将在测试完成且所有装置已完成时运行,而对于装置示例,您不能保证所有装置都已完成,因为没有明确的夹具订购。 此外,如果您需要测试结果,则无法在夹具中访问它。

暂无
暂无

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

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