结合python覆盖文件?

tun*_*rob 15 python unit-testing code-coverage coverage.py python-coverage

我想知道是否可以将coverage.xml文件合并到1个文件中以查看HTML输出中的全局报告.

我的unit/functional tests运行是1命令和integration tests第二个命令.这意味着我的覆盖范围unit/functional tests被覆盖了unit tests.

如果我对这个问题有一些解决方案,那将是很好的,主要是通过将这些文件组合成1个文件.

Ned*_*der 16

您无法组合.xml文件,但可以组合原始数据文件.您的工作流程如下所示:

$ COVERAGE_FILE=.coverage_func coverage run the_functional_tests.py
$ COVERAGE_FILE=.coverage_inte coverage run the_integration_tests.py
$ coverage combine
$ coverage xml
Run Code Online (Sandbox Code Playgroud)

  • Coverage.py 有一个功能可以专门帮助解决这个问题:https://coverage.readthedocs.io/en/v4.5.x/config.html#paths 这似乎经常被人们遗漏。我该怎么做才能让它更明显? (2认同)

tun*_*rob 16

我找到了另一种解决方案.我使用了combine功能(在这里阅读)所以我运行我的报道:coverage run -p然后我做coverage combine.

就这样.它生成1个组合报告.

  • `coverage combine` 将合并目录中的所有 `.coverage_*` 文件并创建一个组合的 `.coverage` 文件并删除其他文件。仅在合并不同目录时需要。 (2认同)

Ris*_*sal 8

您可以通过附加选项来达到相同的结果。假设您在三个python脚本上运行了coverage,第一次覆盖后,请使用-a进行追加。

coverage run first.py
coverage run -a second.py
coverage run -a third.py
Run Code Online (Sandbox Code Playgroud)

打印报告

coverage report -m
Run Code Online (Sandbox Code Playgroud)

输出:报告

Name             Stmts   Miss  Cover   Missing
----------------------------------------------
first.py           97      1    99%   95
second.py            1      0   100%
third.py            10      0   100%
----------------------------------------------
TOTAL               108      1    99%
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您的源代码位于名为 的目录中my_project,并且您已在虚拟环境中包含pytest和,则也可以执行此操作:pytest-cov

pytest --cov-report html --cov=my_project unit_tests
pytest --cov-report html --cov=my_project --cov-append functional_tests
Run Code Online (Sandbox Code Playgroud)

--cov-append会将功能测试覆盖率信息添加到运行单元测试时创建的覆盖率文件中。