Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/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结果JUnit风格xml文件测试用例计数_Python_Junit_Pytest_Bazel - Fatal编程技术网

Python Pytest结果JUnit风格xml文件测试用例计数

Python Pytest结果JUnit风格xml文件测试用例计数,python,junit,pytest,bazel,Python,Junit,Pytest,Bazel,源代码库由几个相互分离的Python库/模块组成。对于每一项测试,都有一组测试,即: 模块名为:foo,包含多个文件 相应的测试文件test\u foo.py,其中两个测试使用Pytest编写 运行测试后,我确实会生成一个XML文件,其内容与以下内容类似: <?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name="foo/test_foo" tests="1" failures=

源代码库由几个相互分离的Python库/模块组成。对于每一项测试,都有一组测试,即:

  • 模块名为:
    foo
    ,包含多个文件
  • 相应的测试文件
    test\u 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>

注意:未对齐的零件由
系统输出
标记封装)

我的
pytest.ini
文件相当简单,只包含有关日志记录(
log\u cli*
log\u file*
)和
超时的信息。我使用bazel作为构建系统,它定义了
py_test
作业,我的假设是它以某种方式隐式添加了一个默认的输出设置。然而,即使在直接运行时,这也应该起到类似的作用

现在,我想处理这些XML文件,并提取运行的测试、错误等的总计数。示例中显示的问题是,XML文件声明测试计数为
tests=“1”
,而日志中的stdout显示至少收集了两项

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

Python中是否有任何简单的方法使testsuite包含
收集项的计数?

是否使用--junitxml=something.xml标志?

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

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

还有一件事。如果您的xml文件包含所有捕获的输出,那么我建议您执行以下操作:

  • 使用-s运行以不捕获输出
  • 如果需要会话日志,请以“pytest-s--junitxml=out.xml[其他标志、测试目录、文件等]>out.txt”的形式运行整个过程

  • 然后您还需要归档out.txt,但它可以防止xml太大。

    很可能是bazel
    py_test
    造成的:(bazel似乎将整个pytest视为一个测试,就像您编写的一样)。不幸的是,但是可以解决,例如,使用我自己的显式junitxml:)谢谢!