python - Pytest 结果 JUnit 风格的 xml 文件测试用例计数

标签 python junit pytest bazel

源库由多个 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*)和 超时。我使用 bazel 作为构建系统,它定义了 py_test 作业,我的假设是它以某种方式隐式添加了默认输出设置。但是,即使直接运行,这也应该以类似的方式工作。

现在,我想处理一堆这样的 XML 文件并提取测试运行的总数、错误等。示例显示的问题是 XML 文件声称测试计数为 tests="1",而日志中的标准输出显示至少收集了两项。

在我看来,该格式将整个文件计为测试用例,而不是文件中收集的实际测试用例

在 Python 中是否有任何简单的方法可以使测试套件包含收集的项目的计数?

最佳答案

您使用的是 --junitxml=something.xml 标志吗? https://docs.pytest.org/en/latest/usage.html#creating-junitxml-format-files

当我使用 --junitxml 标志并且生成的输出包括正确数量的测试时。 我的猜测是 bazel 是生成 xml 输出的那个,而不是 pytest,并且它将整个 pytest 运行视为一个测试。

我认为这可能需要一个 bazel 标记的问题,因为它实际上是“我如何让 bazel 读取额外的 xml 文件?”,然后使用 --junitxml 生成您自己的 xml。

还有一件事。如果您的 xml 文件因所有捕获的输出而变大,我建议您执行以下操作:

  1. 运行 -s 不捕获输出
  2. 如果您需要 session 日志,请将整个东西运行为“pytest -s --junitxml=out.xml [其他标志、测试目录、文件等] > out.txt”

然后您还需要归档 out.txt,但这样可以避免 xml 变得庞大。

关于python - Pytest 结果 JUnit 风格的 xml 文件测试用例计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326191/

相关文章:

python - 从下面的代码中数据输出是水平的,是否可以垂直

java - 如何在单个浏览器中运行多个 UI 测试?

python - 在pytest中,如何确保在类级之前调用模块级 fixture

java - 使用 Zerocode 运行 Junit 测试套件

python - py.test 运行成功后,模块 'threading' 中的 KeyError

python - 以编程方式创建 pytest 固定装置

python - Keras 中的指标是如何计算的?

python - SimpleBlobDetector opencv python错误但缺少输出

python - Pandas - FillNa 具有相似列的另一个非空行

java - 如何使用 Junit 在 Java 中测试打印方法