Is it possible exclude test directories from coverage.py reports?
Asked Answered
H

6

128

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?

Heretical answered 27/10, 2009 at 6:8 Comment(1)
I had a similar problem. I didn't want extra python files (standard lib, etc.) in the coverage report. I ended up stripping them out of the XML: #2294147Dovetailed
U
114

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

[run]
omit = *tests*
Unquiet answered 6/7, 2017 at 12:10 Comment(3)
@CynthiaSimiyu, your answer was really helpful since I needed to omit multiple directories.Lustrum
@Lustrum : The omit option is multi-string so you can enumerate the multiple directories to omit (see example in doc)Accredit
Alternately, add an entry to your tox.ini file [coverage:run] omit = *tests* which I figured out by reading coverage.readthedocs.io/en/latest/config.htmlFossiliferous
D
112

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

Dicta answered 23/12, 2013 at 7:30 Comment(2)
Just to add : Multiple directories/files can be added using comma like --omit="*/test*,config/*.conf"Glyph
Should be top rated answer. Aint nobody wanna create coverage config filesMcwherter
S
57

Leaving this here in case if any Django developer needs a .coveragerc for their 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.

Solar answered 26/3, 2020 at 10:39 Comment(2)
Use this answer if you are using Django, the omit lines in the .converagerc removes all the boilerplates from the coverageCinch
When you say "project root", you mean where manage.py is located or even above that?Meridional
C
20

You can specify the directories you want to exclude by creating a .coveragerc in the project root.

It supports wildcards (which you can use to exclude virtual environment) and comments (very useful for effective tracking).

The below code block shows how omit can be used (taken from the latest documentation) with multiple files and directories.

[run]
omit =
    # omit anything in a .local directory anywhere
    */.local/*
    # omit everything in /usr
    /usr/*
    # omit this single file
    utils/tirefire.py

In your case, you could have the following in your .coveragerc:

[run]
omit = 
    # ignore all test cases in tests/
    tests/*

For your question on coverage reports, you can think about testing and coverage in the following manner:

  • When you run pytest or unittest, all the test cases for your source code are executed

  • When you run coverage, it shows the part of the source code that isn't being used.

  • When you run pytest with coverage (something like pytest -v --cov), it runs all test cases and shows the part of the source code that isn't being used.

Extra:

  • You can also specify the location of your HTML report in the configuration file like:
[html]
directory = tests/coverage/html_report/

This is going to create html, js, css, etc. inside tests/coverage/html_report/ everytime you run coverage or pytest -v --cov

Cecum answered 5/11, 2020 at 20:1 Comment(1)
You'll have to specify in omit **/tests/* to ignore nested test folders.Brandon
A
8

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
Assibilate answered 2/3, 2019 at 2:50 Comment(1)
In my case, specifying --omit='*test*' was failing to omit tests/unit/__init__.py, but using --source=mypackage worked perfectly.Privet
C
0

I had the same issue and tried every response on this thread. Not wanting to create a .coveragerc file, I found the answer here: https://coverage.readthedocs.io/en/latest/source.html#source

you're missing the asterisk:

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

this worked for me - although I don't specify a directory so I just use:

coverage html --omit=tests/*
Cuspidor answered 20/12, 2023 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.