Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't coverage.py properly measure Django's runserver command?

Tags:

I should know the answer to this, but I don't: if you try to measure the coverage of a Django project like this:

coverage run manage.py runserver  

you get coverage measurement that misses all of your actual code. Something early on in the process is stopping the measurement, or all the real work happens in a new context that doesn't get measured at all.

Can someone point me to the specific point in the process where the measurement breaks down, so that I can try to fix coverage.py so that it will measure it properly the way people expect?

like image 527
Ned Batchelder Avatar asked Aug 13 '11 14:08

Ned Batchelder


People also ask

Why does Python manage PY Runserver not work?

The site could be temporarily unavailable or too busy. Try again in a few moments. If you are unable to load any pages, check your computer's network connection. If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

What does Runserver do in Django?

The runserver command is a built-in subcommand of Django's manage.py file that will start up a development server for this specific Django project.


1 Answers

Do you get the same problem if you run as follows?

coverage run manage.py runserver --noreload 

Without --noreload, another process is started behind the scenes. One process runs the server, the other looks for code changes and restarts the server when changes are made. The chances are, you're doing the coverage run on the monitoring process rather than the serving process.

Look at django/core/management/commands/runserver.py and django/utils/autoreload.py.

Update: I ran the coverage command, then used ps and lsof to look at what was happening. Here's what I observed:

ps output:  UID        PID  PPID  C STIME TTY          TIME CMD  vinay    12081  2098  0 16:37 pts/0    00:00:00 /home/vinay/.virtualenvs/watfest/bin/python /home/vinay/.virtualenvs/watfest/bin/coverage run manage.py runserver vinay    12082 12081  2 16:37 pts/0    00:00:01 /home/vinay/.virtualenvs/watfest/bin/python manage.py runserver  lsof output:  python    12082      vinay    5u     IPv4      48294      0t0        TCP localhost:8000 (LISTEN) 

IOW, even before any reloading there are two processes, and the one listening on the TCP port is not the one which coverage is running on.

Here's what I see with --noreload:

ps output:  UID        PID  PPID  C STIME TTY          TIME CMD  vinay    12140  2098  5 16:44 pts/0    00:00:00 /home/vinay/.virtualenvs/watfest/bin/python /home/vinay/.virtualenvs/watfest/bin/coverage run manage.py runserver --noreload  lsof output:  coverage  12140      vinay    4u     IPv4      51995      0t0        TCP localhost:8000 (LISTEN) 

So it's not obvious why coverage wouldn't work in the --noreload case. In my very brief test with --noreload, I got coverage of my view code, as shown by the following extract:

festival/__init__   8      7    13% manage              9      4    56% settings           33      1    97% 
like image 101
Vinay Sajip Avatar answered Sep 25 '22 01:09

Vinay Sajip