Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 自定义pytest junitxml失败报告_Python_Logging_Pytest - Fatal编程技术网

Python 自定义pytest junitxml失败报告

Python 自定义pytest junitxml失败报告,python,logging,pytest,Python,Logging,Pytest,我试图反思测试失败,并在JUnitXML测试报告中包含额外的数据。具体来说,这是一套针对外部产品的功能测试,我想将产品的日志包括在故障报告中 使用找到的方法,我能够在执行multicall之前将日志打印到stdout,multicall最终显示在jenkin的fail报告中。但我相信有更好的方法来实现这一点 我尝试使用pytest_runtest_logreport钩子将日志附加到'sections'属性中,该属性已经包含'captured stdout'和'captured stderr'流。

我试图反思测试失败,并在JUnitXML测试报告中包含额外的数据。具体来说,这是一套针对外部产品的功能测试,我想将产品的日志包括在故障报告中

使用找到的方法,我能够在执行multicall之前将日志打印到stdout,multicall最终显示在jenkin的fail报告中。但我相信有更好的方法来实现这一点

我尝试使用pytest_runtest_logreport钩子将日志附加到'sections'属性中,该属性已经包含'captured stdout'和'captured stderr'流。但是,新添加的部分无法写入xml文件。我也直接在pytest_runtest_makereport钩子中尝试了上述技术,得到了类似的结果

pytest 2.7的发行说明指出,在2.8中正在放弃使用multicall支持,并且@pytest.mark.hookwrapper是实现这一点的新方法,但是我似乎根本无法做到这一点——“yield”返回None而不是callocome对象(在makereport钩子中尝试过)。即使它返回了一些东西,我也不确定是否可以添加xml报告中显示的东西


是否有我缺少的功能可以让我以灵活的方式完成这项工作?(所谓灵活,我的意思是:不必绑定到标准输出或捕获日志插件之类的日志调用)

要向测试报告(XML、控制台或其他)添加信息,请查看,更具体地说,请查看。

编辑:因为我需要访问测试项的函数(和测试结果)才能进行报告,我能够将逻辑移到
pytest\u runtest\u makereport(项,\uuu multicall\uuuu)
hook。诀窍是执行multicall,它返回报告对象:

@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
    report = __multicall__.execute()
    # then I was able to manipulate report and get the same results as below

布鲁诺的回答给了我更彻底地分析这一特性所需的动力:)

下面是它的工作原理:

def pytest_runtest_logreport(report):
    if report.failed:
        report.longrepr.sections.append(("Header", "Message", "-"))
        report.sections.append(("Captured stdout", "This is added to stdout"))
        report.sections.append(("Captured stderr", "This is added to stderr"))
        report.sections.append(("Custom Section", "This can only be seen in the console - the xml won't have it."))
longrepr
属性仅在出现故障时可用。它采用3元组,最后一个值是用于修饰/环绕标题的字符。它将出现在报告的“失败”部分:

----------------------------------- Header ------------------------------------
Message
自定义节将创建额外的结果节,以打印到控制台。但它们不会进入junitxml:

------------------------------- Custom Section --------------------------------
This can only be seen in the console - the xml won't have it.
junitxml报告只有两个部分:out和err。要向其中添加自定义文本,必须创建名为“Captured std”的节,并且只有这些节才能将其添加到xml文件中。任何其他名称都将生成一个自定义节,该节仅在控制台中可见

以下是使用上述代码生成的junitxml,并为本文进行了一些重新格式化:

<?xml version="1.0" encoding="utf-8" ?> 
<testsuite errors="0" failures="1" name="pytest" skips="0" tests="1" time="0.646">
  <testcase classname="test_reporting" name="test_fail" time="0.000999927520752">
    <failure message="test failure">
      @ut def test_fail(): > assert 0, "It failed"
      E AssertionError: It failed 
      E assert 0 test_reporting.py:346: AssertionError
      ----------------------------------- Header ------------------------------------
      Message
    </failure> 
    <system-out>This is added to stdout</system-out> 
    <system-err>This is added to stderr</system-err> 
  </testcase>
</testsuite>

@ut def test_fail():>断言0,“它失败了”
断言者:它失败了
E断言0测试报告。py:346:AssertionError
-----------------------------------标题------------------------------------
消息
这被添加到标准输出中
这将添加到stderr中

使用
pytest\u runtest\u makereport(项,调用)
@pytest.hookimpl(hookwrapper=True)
允许在报告用于创建xml之前访问该报告,例如

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(项目,调用):
结果=产量
如果result.get_result()失败:
report.longrepr.addsection(“标题”、“消息”)
使用pytest-3.9.3

不过,对于钩子,包装纸确实建议谨慎

如果底层钩子的结果是一个可变对象,他们可能会修改该结果,但最好避免它


我不是故意要把这个问题说得具体点。我真正需要的是一种将任意数据从代码注入pytest的失败报告的方法。日志记录方法是有效的(这就是我正在使用的),但阻止我使用“-s”开关(使用它将使pytest无法捕获stdout,因此,登录到控制台将根本无法访问junit报告)。然后更新了我的答案。Tnx;)但是这个钩子已经在我原来的帖子中提到了。。。我猜我用错了?很抱歉。你能给出一个例子和你的期望吗?这个钩子应该足够了。我终于有时间来分析它并发布了我的发现。这个解决方案现在为
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
参数生成了一个弃用警告。我想知道是否有办法很好地避免它。我唯一能做的就是调用这个钩子的pytest原始实现,使用
from\u pytest.runner导入pytest\u runtest\u makereport as\u pytest\u runtest\u makereport
来获取报告是的,您可以通过生成for output report来避免
\u multicall\u
弃用警告。内部
pytest\u runtest\u logreport()
尝试:
output=yield
然后
report=output.get\u result()
`