21

How can you get unittest2 and coverage.py working together?

In theory something like

coverage run unit2 discover 

should work, but it currently just errors out.

If you are a nose user that will be the equivalent of nosetests --with-coverage.

nbro
  • 15,395
  • 32
  • 113
  • 196
Jorge Vargas
  • 6,712
  • 7
  • 32
  • 29
  • I get `$ coverage run unit2 discover Coverage.py warning: No data was collected. No file to run: 'unit2'` I'm running Python 2.7 on OS X 10.6.8. – yesudeep Jul 11 '11 at 14:45

4 Answers4

47

Try:

coverage run -m unittest discover

works for me.

jbastos
  • 1,217
  • 1
  • 12
  • 19
5

This must be specific to your installation since it works fine for me

coverage run unit2 discover

to generate the coverage information then

coverage html

to generate the an HTML report (one of several reporting formats), and

open htmlcov/index.html

to see the results.

(Answering this because this is a top ghit for "unittest2 coverage" and I don't want people put off by the lack of any answer.)

Andrew Dalke
  • 14,889
  • 4
  • 39
  • 54
3

I am running Windows and encountered the same problem.

$ coverage run unit2 discover
No file to run: 'unit2'

I suspect this is related to differences in how the system path and python path are handled on various operating systems (I'm guessing Andrew is running linux?). Regardless, coverage run takes a -m option which allows you to run a module rather than a script. This is what I use:

coverage run -m unittest2 discover
Tim W.
  • 834
  • 1
  • 9
  • 18
1

In case you have multiple versions of coverage installed, use

coverage2 run -m unittest discover

or

coverage3 run -m unittest discover

to specify which one you're going to run.

laike9m
  • 18,344
  • 20
  • 107
  • 140