Question

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?

Was it helpful?

Solution

Edit: I think I now agree with the comment below about it not being a good idea to mix a test of your production code with a test of your testing code. I can't delete an accepted answer so I'll leave it unchanged.

** Please downvote my answer! **


It's a good idea to see the coverage of your tests as it can point to problems. If your test code isn't being run then there wasn't much point in writing it!

The one I always get is when I give two unit test functions the same name - I add a new test several months after the original and just happen to pick the same name. The unittest framework won't complain about this - one of the functions hides the other and it just won't run one of the tests! The detailed coverage report shows the problem immediately though.

If you have other code in your tests that isn't being run then that may also point to other bugs, although typically there's often a couple of lines of boilerplate code that might not get covered depending on how the tests get invoked, so don't obsess about getting to 100%.

And if you have test code that really isn't needed any more then it's always good to delete!

OTHER TIPS

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

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

[run]
omit = *tests*

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top