Questions tagged [coverage.py]

coverage.py is a tool for measuring test code coverage of Python programs

About

coverage.py is a tool for measuring test code coverage of Python programs. It monitors your program, noting which parts of the code have been executed, then analyzes the source to identify code that could have been executed but was not.
-- taken from https://coverage.readthedocs.io/

Install Instructions

You can install coverage.py with pip

$ pip install coverage

Usage

Run coverage.py with:

$ coverage run my_program.py arg1 arg2

Output looks like:

$ coverage report -m 
Name                      Stmts   Miss  Cover   Missing
-------------------------------------------------------
my_program                   20      4    80%   33-35, 39
my_other_module              56      6    89%   17-23
-------------------------------------------------------
TOTAL                        76     10    87%

Details can be found here.

472 questions
122
votes
5 answers

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

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…
Kyle Fox
  • 3,133
  • 4
  • 23
  • 26
70
votes
4 answers

How to properly use coverage.py in Python?

I've just started using Coverage.py module and so decided to make a simple test to check how it works. Sample.py def sum(num1, num2): return num1 + num2 def sum_only_positive(num1, num2): if num1 > 0 and num2 > 0: return num1 +…
Groosha
  • 2,897
  • 2
  • 22
  • 38
65
votes
10 answers

Coverage.py warning: No data was collected. (no-data-collected)

I am trying to find the coverage using coverage module for a django project but gets Coverage.py warning: No data was collected. (no-data-collected) My project folder has src and tests folders. When I run coverage run -m pytest && coverage…
1010101
  • 843
  • 1
  • 6
  • 11
64
votes
5 answers

coverage.py: exclude files

How do I exclude entire files from coverage.py reports? According to the documentation you can exclude code by matching lines. I want to exclude entire files, so that the reports don't include 3rd party libraries. Am I missing something? Can it be…
flybywire
  • 261,858
  • 191
  • 397
  • 503
64
votes
2 answers

How to Fix Python Nose: Coverage not available: unable to import coverage module

I can't seem to get code coverage with Nose to work, despite having the plugin installed. Any ideas on how to fix this? 12:15:25 ~/sandbox/ec$ nosetests --plugins Plugin xunit Plugin deprecated Plugin skip Plugin multiprocess Plugin…
doremi
  • 14,921
  • 30
  • 93
  • 148
62
votes
4 answers

How do I make coverage include not tested files?

I have just started writing some unit tests for a python project I have using unittest and coverage. I'm only currently testing a small proportion, but I am trying to work out the code coverage I run my tests and get the coverage using the…
user4591756
51
votes
3 answers

Excluding abstractproperties from coverage reports

I have an abstract base class along the lines of: class MyAbstractClass(object): __metaclass__ = ABCMeta @abstractproperty def myproperty(self): pass But when I run nosetests (which coverage) on my project, it complains that the…
Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
48
votes
5 answers

Using py.test with coverage doesn't include imports

For Jedi we want to generate our test coverage. There is a related question in stackoverflow, but it didn't help. We're using py.test as a test runner. However, we are unable to add the imports and other "imported" stuff to the report. For example…
Dave Halter
  • 15,556
  • 13
  • 76
  • 103
45
votes
2 answers

How to get coverage reporting when testing a pytest plugin?

Context I am updating an inherited repository which has poor test coverage. The repo itself is a pytest plugin. I've changed the repo to use tox along with pytest-cov, and converted the "raw" tests to use pytester as suggested in the pytest…
Thomas Thorogood
  • 2,150
  • 3
  • 24
  • 30
33
votes
5 answers

combine python coverage files?

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…
tunarob
  • 2,768
  • 4
  • 31
  • 48
33
votes
4 answers

coverage.py does not cover script if py.test executes it from another directory

I got a python script which takes command line arguments, working with some files. I'm writing succeeding tests with py.test putting this script through its paces, executing it with subprocess.call. Now I want to analyze code coverage with…
Christoph
  • 5,480
  • 6
  • 36
  • 61
31
votes
6 answers

How can I exclude South migrations from coverage reports using coverage.py

I use coverage.py to check the test coverage of my django application. However since I use South for my database migrations, all those files show up with 0% and mess up the overall percentage. I already tried using --omit=*migrations* in both run…
Jonas Obrist
  • 336
  • 3
  • 4
29
votes
3 answers

Python Code Coverage and Multiprocessing

I use coveralls in combination with coverage.py to track python code coverage of my testing scripts. I use the following commands: coverage run --parallel-mode --source=mysource --omit=*/stuff/idont/need.py ./mysource/tests/run_all_tests.py coverage…
SmCaterpillar
  • 6,683
  • 7
  • 42
  • 70
28
votes
2 answers

Making py.test, coverage and tox work together: __init__.py in tests folder?

I'm having a weird problem with tox, py.test, coverage and pytest-cov: when py.test with the --cov option is launched from tox, it seems to require an __init__.py file in the tests folder which is not immediately obvious. While writing this post, I…
aldanor
  • 3,371
  • 2
  • 26
  • 26
27
votes
2 answers

Finding unused Django code to remove

I've started working on a project with loads of unused legacy code in it. I was wondering if it might be possible to use a tool like coverage in combination with a crawler (like the django-test-utils one) to help me locate code which isn't getting…
Ludo
  • 2,739
  • 2
  • 28
  • 42
1
2 3
31 32