【问题标题】:Is it possible to have pytest call a webhook after each test executed?每次执行测试后是否可以让 pytest 调用 webhook?
【发布时间】:2019-01-15 20:00:22
【问题描述】:

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

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

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

【问题讨论】:

标签: python integration-testing pytest webhooks


【解决方案1】:

这取决于您要发出什么数据。如果一个简单的完成检查就足够了,请在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 将在测试完成并且所有固定装置都已完成时运行,而对于固定装置示例,您不能保证所有固定装置都已完成,因为没有明确的夹具顺序。此外,如果您需要测试结果,您无法在夹具中访问它。

【讨论】:

  • 是的,我想访问测试结果。 pytest_runtest_makereport 看起来很有希望。让我测试一下。谢谢!
  • pytest_runtest_makereport 方法有效,但当result.when == 'teardown' 时结果“通过”。当result.when == 'call' 时,我可以看到结果“失败”。
猜你喜欢
  • 2013-04-14
  • 2022-09-24
  • 2017-08-12
  • 2013-10-10
  • 2013-01-26
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多