【发布时间】: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("This is a random Failure ")
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