【发布时间】:2019-01-23 11:26:47
【问题描述】:
源代码库由多个相互分离的 Python 库/模块组成。对于它们中的每一个,都存在一组测试,即:
- 模块调用:
foo,包含多个文件, - 对应的测试文件
test_foo.py,有两个使用Pytest编写的测试。
在运行测试时,我确实得到了一个生成的 XML 文件,其内容类似于:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="foo/test_foo" tests="1" failures="0" errors="0">
<testcase name="foo/test_foo" status="run" duration="9" time="9"></testcase>
<system-out><![CDATA[============================= test session starts ==============================
platform linux -- Python 3.6.6, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: ..., inifile: pytest.ini
plugins: timeout-1.3.2, mock-1.10.0, cov-2.6.0
timeout: 60.0s
timeout method: thread
timeout func_only: False
collecting ... collected 2 items
::test_foo_test1
-------------------------------- live log setup --------------------------------
# ... some logging performed in the test...
PASSED [ 50%]
::test_foo_test2
-------------------------------- live log call ---------------------------------
# ... some logging performed in the test...
PASSED [100%]
=========================== 2 passed in 6.63 seconds ===========================]]></system-out>
</testsuite>
</testsuites>
(注意:未对齐的部分被system-out标记封装)
我的pytest.ini 文件相当简单,仅包含有关日志记录的信息(log_cli*、log_file*)和timeout。我使用 bazel 作为构建系统,它定义了py_test 作业,我的假设是它以某种方式隐式添加了默认输出设置。但是,即使直接运行,这也应该类似地工作。
现在,我想处理一堆这些 XML 文件并提取测试运行、错误等的总数。示例显示的问题是 XML 文件声称测试计数为 @987654330 @,而日志中的标准输出显示至少收集了两个项目。
在我看来,该格式将整个文件计为测试用例,而不是文件中收集的实际测试用例。
在 Python 中是否有任何简单的方法可以使测试套件包含 collected items 的计数?
【问题讨论】: