Pete
Pete

Reputation: 2693

How do I prevent Nose from running and reporting duplicate tests?

I'm using django-nose to test our Django projects. It is common to split large test suites inside an application in Django like this:

myapp/
  __init__.py
  models.py
  tests/
    __init__.py
    test_views.py
    test_models.py
  views.py

tests/__init__.py would look like this:

from test_views import *
from test_models import *

Since Django will look for tests in myapp.tests, everything works as expected. Nose on the other hand will find the tests in tests_*.py and import them again in __init__.py. This results in the total number of tests reported being double what they should be.

Any ways around this problem (other than never using sub-modules) that will correctly report the tests with both django-nose and the default Django test runner?

Upvotes: 1

Views: 498

Answers (2)

spookylukey
spookylukey

Reputation: 6576

You can do the import conditionally.

The following does the trick, assuming you are setting TEST_RUNNER = 'django_nose.NoseTestSuiteRunner':

from django.conf import settings
if 'nose' not in getattr(settings, 'TEST_RUNNER', ''):
    # Support Django test discovery
    from .test_views import *
    from .test_models import *

In this way, you will be able to support both the normal Django test discovery and nose test discovery without duplicating tests for the latter, or losing them for the former.

Upvotes: 0

Max
Max

Reputation: 11607

Any ways around this problem (other than never using sub-modules)

Don't include the lines

from test_views import *
from test_models import *

in tests/__init__.py. What are those lines accomplishing?

Upvotes: 1

Related Questions