3

Using Django Nose. I have tests for my URL's but coverage is still giving me 0% for URLs, why?

python manage.py test profiles

This is my coverage:

Name                               Stmts   Miss  Cover   Missing
----------------------------------------------------------------
profiles                               0      0   100%
profiles.migrations                    0      0   100%
profiles.migrations.0001_initial       6      0   100%
profiles.models                        0      0   100%
profiles.urls                          4      4     0%   1-9
----------------------------------------------------------------
TOTAL                                 10      4    60%
----------------------------------------------------------------

This is one of my URL test...

url_tests.py

import nose.tools as noz
from django.test import TestCase
from django.core.urlresolvers import resolve, reverse

class URLsTest(TestCase):

    def test_user_list(self):
        url = reverse('api_user_list', args=[])
        noz.assert_equal(url, '/api/user/')
oz123
  • 27,559
  • 27
  • 125
  • 187
Prometheus
  • 32,405
  • 54
  • 166
  • 302
  • Without seeing more of your codebase, I'm not sure I can help; I can confirm that moving one of my projects (on Django 1.6) to use django-nose, running python manage.py test --with-coverage and having a test that exercises URLs results in correct coverage reports for my urls.py; so it seems likely this is something to do with your code/usage in particular. – James Aylett Sep 25 '14 at 16:07

1 Answers1

2

Usually this has to do with coverage.py being started too late in the process. The simplest way to ensure it starts early enough is to run the test runner under coverage:

$ coverage run nosetests.py ....

One relevant detail of urls.py: it contains only code that executes when it is imported. So the entire file is executed when Django starts up and imports urls.py. This is different than most files, which define classes or functions whose bodies are executed later.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • I am using Django Nose. However, why would the other tests be ok and not this one? Regardless of the order it should report something unless I am doing the testing of URLs wrong. – Prometheus Sep 21 '14 at 17:13
  • Coverage can only measure code that is executed after coverage is started. If Django Nose imports (and therefore executes) urls.py before it starts coverage.py, then you will see 0% reported for urls.py – Ned Batchelder Sep 26 '14 at 15:30
  • Have you tried running your tests under coverage as I've shown in the answer? – Ned Batchelder Sep 26 '14 at 15:32
  • @Sputnik your test shows that your urls.py was imported and processed, but doesn't directly exercise any of the code in urls.py... as Ned has said, it's likely that Django has already processed your urls.py before the test runner starts measuring coverage – Anentropic Sep 26 '14 at 17:11
  • @NedBatchelder I have coverage installed by the command coverage is not available – Prometheus Sep 29 '14 at 10:25