Posts Tagged ‘coverage’

Coverage and pytest 2.0.0

6 December 2010 Leave a comment

I had been struggling to find a nice flexible command-line workflow for getting coverage reports on py.test runs. I ended up using pytest-cov and pytest-coverage (never managed to make up my mind between these two plugins) but I wasn’t satisfied. Since they haven’t yet been updated for pytest 2.0.0, I’ve been forced to find a better way. Here it is:


The only setup required is to use pytest’s stand-alone script generation utility:

(my_env)me@my_computer:~/my_project$ py.test

That’s it! Of course, you need to have the latest pytest and coverage installed and you should create and configure setup.ini and .coveragerc to your liking.

Using it

The generic invocation is coverage run [coverage options] [py.test options], which is about as clear as it gets, I think. A sample session looks like:

(my_env)me@my_computer:~/my_project$ coverage run --source=some_package,other_package run -k "feature1 feature2" --exitfirst somepackage/tests
================== test session starts ==================
platform linux2 -- Python 2.6.6 -- pytest-2.0.0
collected 77 items

somepackage/tests/ .......
somepackage/tests/ .....

======= 65 tests deselected by 'feature1 feature2'=======
======= 12 passed, 65 deselected in 18.02 seconds========
(my_env)me@my_computer:~/my_project$ coverage html
(my_env)me@my_computer:~/my_project$ firefox htmlcov/index.html

In an ideal world, the genscript step wouldn’t be required, but it’s not a big annoyance.

Categories: Python Tags: ,