Coverage.py makes it easy to see how much of your code is covered by your test suite. It can be configured to spit out reports in XML (for consumption by a continuous integration server like Jenkins), in HTML (for human viewing), or simply dumping out plain text.
Getting started with coverage and Django is easy. First make sure you have the latest version of coverage (3.5 at time of writing) installed by running
pip install -U coverage, then run your test suite and print a report:
coverage run manage.py test coverage report
--omit=PAT1,PAT2,... Omit files when their filename matches one of these patterns. Usually needs quoting on the command line. --include=PAT1,PAT2,... Include files only when their filename path matches one of these patterns. Usually needs quoting on the command line.You’ll notice that these take file paths instead of python module names. Running coverage as follows will only report coverage on files in
coverage run manage.py test coverage report --include="/path/to/my_project/*"
coverage run manage.py test coverage report --include="`python -c \"import os, my_project; print os.path.dirname(my_project.__file__)\"`/*"
TEST_MODULES="my_project my_lib your_lib" #get comma-separated file paths for module names MOD_PATHS="" for mod in `echo $TEST_MODULES` do MOD_PATHS="$MOD_PATHS,`python -c \"import os, $mod; print os.path.dirname($mod.__file__)\"`/*" done #remove preceding comma MOD_PATHS=`echo $MOD_PATHS|cut -c2-` coverage run manage.py test coverage report —include=$MOD_PATHS
coverage help reportfor more options). It’s worth noting that the techniques described here are not Django-specific and can be used with any Python code. Simply replace
coverage run manage.py testwith your test script, eg.
coverage run tests.py.