Is it possible exclude test directories from cover

2020-05-24 19:22发布

I'm kind of a rookie with python unit testing, and particularly coverage.py. Is it desirable to have coverage reports include the coverage of your actual test files?

Here's a screenshot of my HTML report as an example.

You can see that the report includes tests/test_credit_card. At first I was trying to omit the tests/ directory from the reports, like so:

coverage html --omit=tests/ -d tests/coverage

I tried several variations of that command but I could not for the life of me get the tests/ excluded. After accepting defeat, I began to wonder if maybe the test files are supposed to be included in the report.

Can anyone shed some light on this?

4条回答
冷血范
2楼-- · 2020-05-24 19:38

Leaving this here in case if any Django developer needs a .coveragerc for his project.

[run]
source = .
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py

[report]
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py

Create a file named .coveragerc on your projects root directory, paste the above code and then just run the command:

coverage run manage.py test

In addition, if you want the tests to execute faster run this command instead.

coverage run manage.py test --keepdb --parallel

This will preserve the test DB and will run the tests in parallel.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-05-24 19:38

You can also explicitly specify which directory has the code you want coverage on instead of saying which things to omit. In a .coveragerc file, if the directory of interest is called demo, this looks like

[run]
source = demo
查看更多
贪生不怕死
4楼-- · 2020-05-24 19:39

Create .coveragerc file in your project root folder, and include the following:

[run]
omit = *tests*
查看更多
SAY GOODBYE
5楼-- · 2020-05-24 19:52

coverage html --omit="*/test*" -d tests/coverage

查看更多
登录 后发表回答