How do I get code coverage out of a Django project's view code (and code called by view code)?
coverage gunicorn <params>
does not show any lines being hit.
How do I get code coverage out of a Django project's view code (and code called by view code)?
coverage gunicorn <params>
does not show any lines being hit.
coverage gunicorn <params>
does not work, because gunicorn creates worker processes, and thecoverage
module can't work across forks (basically, creation of new processes). You can use the coverage API, though, for example in the python module that contains your WSGI application:Then run
gunicorn -w 1 wsgi_with_coverage:application <other params>
.The problem is, the
atexit
functions are not called if you kill the gunicorn process, for example via CTRL+C. But they are called onSIGHUP
, so if you dokill -HUP $(cat <gunicorn_pidfile_here>)
, the coverage data should be saved (by default to ".coverage" in the current directory).A possible caveat is that this won't work with more than one gunicorn worker, because they would all overwrite the ".coverage" file. If you absolutely need more than one worker, you could write to
".coverage-%d" % os.getpid()
(set the file name via thedata_file
parameter to thecoverage
constructor) and use thecombine()
method to merge the individual measurements.This should work on other WSGI servers, too, depending on whether they allow their worker processes to clean up via the
atexit
method.