【问题标题】:Add logs in junit xml from pytest_generate_tests method从 pytest_generate_tests 方法在 junit xml 中添加日志
【发布时间】:2020-07-24 18:54:18
【问题描述】:

我正在使用 Jenkins 来解析 Junit XML。任何开发人员也会出于分类目的查看 Jenkins 失败。这意味着所有日志都应该在 Junit xml 中可用。

问题在于方法 pytest_generate_tests 有一个非常大的实现,并且需要日志消息来进行故障排除。

但是,在 pytest_generate_tests 中添加的日志消息不会出现在 Junit xml 中。如果测试发现失败,有没有办法在 Junit xml 中包含日志消息?

我提供的命令

pytest -vv --log-level=INFO --junit-xml=sample.xml

pytest.ini(添加 junit_logging 希望 Junit_xml 包含日志)

[pytest]
junit_logging = all

我的示例 Junit.xml(没有任何与 pytest_generate_tests 相关的日志)

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite errors="1" failures="0" hostname="W117-JayJ" name="pytest" skipped="0" tests="1" time="0.960" timestamp="2020-04-12T10:27:31.178617">
<testcase classname="tests.test_my_addon" file="tests/test_my_addon.py" name="Test_App" time="0.000"><error message="collection failure">pyparallel\lib\site-packages\pluggy\hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs

    . . . a Big stack trace . . .
    raise Exception(&quot;This is a random Failure &quot;)
E   Exception: This is a random Failure </error>
</testcase>
</testsuite>
</testsuites>

Junit xml 中没有附加日志消息。

“正在收集...”后可以在控制台查看日志

========================================================================================== test session starts ==========================================================================================
platform win32 -- Python 3.7.4, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: C:\Jay\Work\Automation\pytest-sample-addon\new_dev_environment, inifile: pytest.ini, testpaths: tests
plugins: lovely-pytest-docker-0.1.0, forked-1.1.3, xdist-1.31.0, sample-addon-0.1.0
collecting ... generating testcases for app. fixture=app_positive_cases
generating field tests..
collected 0 items / 1 error

有没有办法在控制台中正确处理日志并将其添加到 Junit.xml 中?

【问题讨论】:

  • pytest 不处理插件中的日志,除了在激活实时日志记录时将它们打印到标准输出/文件。您必须在插件中注册一个自定义处理程序,它将日志附加到 JUnit 报告。

标签: python logging junit pytest


【解决方案1】:

通过尝试,我在类似情况下取得了一些成功:

junit_logging = system-out

尝试使用 all 无效,但 system-out 确实有效。试试这个,看看它是否会产生日志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2016-10-31
    • 2017-01-22
    相关资源
    最近更新 更多