combine python coverage files?

2020-07-02 05:51发布

I'm wondering if it's possible to combine coverage.xml files into 1 file to see global report in HTML output.

I've got my unit/functional tests running as 1 command and integration tests as the second command. That means my coverage for unit/functional tests are overridden by unit tests.

That would be great if I had some solution for that problem, mainly by combining those files into 1 file.

5条回答
聊天终结者
2楼-- · 2020-07-02 06:27

You can achieve same result by using appending option. Suppose you ran the coverage on three python scripts.After first coverage use -a for appending.

coverage run first.py
coverage run -a second.py
coverage run -a third.py

Print the report

coverage report -m

Output:Report

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%
查看更多
一纸荒年 Trace。
3楼-- · 2020-07-02 06:29

You can't combine .xml files, but you can combine the raw data files. Your workflow would look like this:

$ COVERAGE_FILE=.coverage_func coverage run the_functional_tests.py
$ COVERAGE_FILE=.coverage_inte coverage run the_integration_tests.py
$ coverage combine
$ coverage xml
查看更多
女痞
4楼-- · 2020-07-02 06:35

I found a different solution. I used combine feature (read here) So I run my coverage like: coverage run -p and then I do coverage combine.

That's all. It generates 1 combined report.

查看更多
我想做一个坏孩纸
5楼-- · 2020-07-02 06:52

If your source code is in a directory called my_project, you can also do this if you have included pytest and pytest-cov in your virtual environment:

pytest --cov-report html --cov=my_project unit_tests
pytest --cov-report html --cov=my_project --cov-append functional_tests

The --cov-append will add the functional test coverage info to the coverage file that was created when you ran the unit tests.

查看更多
放荡不羁爱自由
6楼-- · 2020-07-02 06:53

Use -a option with coverage run command. eg : coverage run -a test1.py This will append the results to existing coverage file, verify the same by doing coverage report

查看更多
登录 后发表回答