[TIP] Coverage for the Django server

Shaheed Haque shaheedhaque at gmail.com
Tue Apr 16 04:03:13 PDT 2019


Hi,

I'm trying to get coverage.py to generate results for code running
under the Django server [1]. I expected that the initial difficulty
would be with having the server exit in a manner which allowed the
end-of-run coverage logic to get a chance to run. I hacked a solution
to that [2], and I can now say

$ coverage -p manage.py runserver ...

and see a .coverage.xxx.yyy file created. Viewing the results of
"coverage combine" in pycharm shows that the static parts of the code
(e.g. class and function definitions) are covered, but none of the
function bodies are covered! For example, in this fragment, the first
2 lines are green, but the third is red:

    class CreateForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)

Though I could be mistaken, AFAIK, Django does not use any of the
constructs which are known sources of incompatibility. I also note
that most references to using Django with coverage refer to running
tests using Django's built-in test infrastructure ("python3 manage.py
test --with-coverage") but like a recent thread on this list, I am
running the tests separately (though in my case, they are Python
Selenium based tests running under pytest).



More information about the testing-in-python mailing list