3

我正在将一堆nose2测试迁移到pytest,那些yield和pytest不能开箱即用的测试;所以我必须为这些类型制作一些东西,因此我收集“myassert val1 == val2,错误消息”类型的每个自定义断言,我将它们收集在字典中,并通过下面的钩子将它们添加到最终报告中

@pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): # 在下一个钩子执行之前做任何你想做的事情 output = yield

# outcome.excinfo may be None or a (cls, val, tb) tuple
res = outcome.get_result()  # will raise if outcome was exception

if res.when == 'call':
    if not internal_errors == 0:
       #add all collected errors 
       for error in test_results[caller_function_name][False]:
          res.sections.append(("Captured stder call", "ERROR: (%s): %s" % (caller_function_name, error)))
       res.sections.append(("Captured stdout call", "Test case final result FAILED"))

最终报告包含它们,当从命令行运行或在 pycharm 输出窗口中运行时会列出它们,但它们不会显示在 junitxml 报告中。最终失败次数仍然基于有多少 test_* 失败而不是我收集了多少错误,我该如何在报告中更改它?要更新什么报告对象属性?此外,当在 pycharm 中运行时,我可以在报告树的根目录中看到错误,就像我在从命令行运行时在输出报告中看到的一样,但它们与我运行的每个 test_* 无关,它们与根报表对象。prolly 这就是为什么它们没有显示在 junitxml 报告中的原因。我想解释一下要更新哪些钩子和哪些对象以正确更改最终报告、更新失败的测试数以及将错误数正确链接到每个测试。谢谢一堆

4

0 回答 0