Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell django-nose where my tests are?

I have my tests for a Django application in a tests directory:

my_project/apps/my_app/
├── __init__.py
├── tests
│   ├── __init__.py
│   ├── field_tests.py
│   └── storage_tests.py
├── urls.py
├── utils.py
└── views.py

The Django test runner requires that I put a suite() function in the __init__.py file of my application's tests directory. That function returns the test cases that will run when I do $ python manage.py test

I installed django-nose. When I try to run the tests with django-nose, 0 tests are run:

$ python manage.py test <app_name>

If I point directly at the test module, the tests are run:

$ python manage.py test my_project.apps.my_app.tests.storage_tests

Why does django-nose's test runner not find my tests? What must I do?

like image 431
hekevintran Avatar asked May 19 '11 21:05

hekevintran


1 Answers

If you use django-nose you can use a simple selector:

from nose.selector import Selector
from nose.plugins import Plugin
import os
import django.test

class TestDiscoverySelector(Selector):
    def wantDirectory(self, dirname):
        parts = dirname.split(os.path.sep)
        return 'my_app' in parts

    def wantClass(self, cls):
        return issubclass(cls, django.test.TestCase)

    def wantFile(self, filename):
        parts = filename.split(os.path.sep)
        return 'test' in parts and filename.endswith('.py')

    def wantModule(self, module):
        parts = module.__name__.split('.')
        return 'test' in parts

class TestDiscoveryPlugin(Plugin):
    enabled = True

    def configure(self, options, conf):
        pass

    def prepareTestLoader(self, loader):
        loader.selector = TestDiscoverySelector(loader.config)

This is just an example implementation and you can make it more configurable or adjust it to your needs. To use it in your Django project just provide the following option in settigs.py

NOSE_PLUGINS = ['lorepo.utility.noseplugins.TestDiscoveryPlugin']
like image 175
Mateusz Mrozewski Avatar answered Oct 04 '22 20:10

Mateusz Mrozewski