[TIP] Coverage for the Django server

Samantha Zeitlin samanthazeitlin at gmail.com
Sat Apr 27 12:08:48 PDT 2019


Shaeed, did you try this? It looks like you might have to edit manage.py?

https://stackoverflow.com/questions/24668174/how-to-test-coverage-properly-with-django-nose

I'm pretty sure we were using coverage in one of my previous jobs, but 
my memory of the details is fuzzy now. I may be able to put you in touch 
with one of my former coworkers who might be able to help, if you're 
still stuck.

Sam


testing-in-python-request at lists.idyll.org wrote:
> Send testing-in-python mailing list submissions to
> 	testing-in-python at lists.idyll.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> 	http://lists.idyll.org/listinfo/testing-in-python
> or, via email, send a message with subject or body 'help' to
> 	testing-in-python-request at lists.idyll.org
>
> You can reach the person managing the list at
> 	testing-in-python-owner at lists.idyll.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of testing-in-python digest..."
>
>
> Today's Topics:
>
>     1. Re: Coverage for the Django server (Shaheed Haque)
>     2. Re: Coverage for the Django server (Marius Gedminas)
>     3. Re: Coverage for the Django server (Shaheed Haque)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 26 Apr 2019 21:37:38 +0100
> From: Shaheed Haque<shaheedhaque at gmail.com>
> Subject: Re: [TIP] Coverage for the Django server
> To: testing-in-python at lists.idyll.org
> Message-ID:
> 	<CAHAc2jdTfO5t6A1mBriq-Nk1XJeECsr_2pmq3NYVd_a53YyOOQ at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Apologies for the bump, but I'm really stuck for what to try next.
>
> P.S. I did also email the Django list, but that was similarly quiet
> <https://groups.google.com/d/msg/django-users/_kYGSeDQG0Q/FUlYshV_CAAJ>.
>
>
> On Tue, 16 Apr 2019 at 12:03, Shaheed Haque<shaheedhaque at gmail.com>  wrote:
>
>> 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).
>>
>>  From coverage.py's point of view, isn't Django just a normal Python
>> server program? What might I be doing wrong? Any clues much
>> appreciated.
>>
>> Thanks, Shaheed
>>
>> [1] My wider setup includes code running under Celery and so on, but I
>> realise that has its own limitations with regard to coverage.py
>> compatibility.
>> [2] See https://groups.google.com/forum/#!topic/django-users/_kYGSeDQG0Q
>> if needed.
>>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:<http://lists.idyll.org/pipermail/testing-in-python/attachments/20190426/955229f4/attachment.html>
>
> ------------------------------
>
> Message: 2
> Date: Sat, 27 Apr 2019 09:57:16 +0300
> From: Marius Gedminas<marius at gedmin.as>
> Subject: Re: [TIP] Coverage for the Django server
> To: Shaheed Haque<shaheedhaque at gmail.com>
> Cc: testing-in-python at lists.idyll.org
> Message-ID:<20190427065716.ql27aq6kyv7ztpvb at platonas>
> Content-Type: text/plain; charset=us-ascii
>
> Um, I sent a reply to this on Apr 16.  Did it get stuck in some
> moderation queue?  Are my PGP signatures at fault?
>
> On Fri, Apr 26, 2019 at 09:37:38PM +0100, Shaheed Haque wrote:
>> Apologies for the bump, but I'm really stuck for what to try next.
>>
>> P.S. I did also email the Django list, but that was [1]similarly quiet.
>>
>>
>> On Tue, 16 Apr 2019 at 12:03, Shaheed Haque<[2]shaheedhaque at gmail.com>  wrote:
>>
>>      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 ...
>
> TL;DR is try passing --noreload to runserver.
>
>>      Though I could be mistaken, AFAIK, Django does not use any of the
>>      constructs which are known sources of incompatibility.
>
> It does: the Django autoreloader spawns a subprocess to run the actual
> view code (that gets killed and restarted every time the autoreloader
> detects source code changes).
>
> Marius Gedminas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20190427/97e26375/attachment.htm>


More information about the testing-in-python mailing list