Running a specific test case in Django when your app has a tests directory


The Django documentation ( says that you can run individual test cases by specifying them:

$ ./ test animals.AnimalTestCase

This assumes that you have your tests in a file in your Django application. If this is true, then this command works like expected.

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

├── tests
│   ├──
│   ├──
│   ├──

The tests/ file has a suite() function:

import unittest

from my_project.apps.my_app.tests import field_tests, storage_tests

def suite():
    tests_loader = unittest.TestLoader().loadTestsFromModule
    test_suites = []
    return unittest.TestSuite(test_suites)

To run the tests I do:

$ ./ test my_app

Trying to specify an individual test case raises an exception:

$ ./ test my_app.tests.storage_tests.StorageTestCase
ValueError: Test label 'my_app.tests.storage_tests.StorageTestCase' should be of the form app.TestCase or app.TestCase.test_method

I tried to do what the exception message said:

$ ./ test my_app.StorageTestCase
ValueError: Test label 'my_app.StorageTestCase' does not refer to a test

How do I specify an individual test case when my tests are in multiple files?

5/4/2011 7:41:21 AM

Accepted Answer

Checkout django-nose. It allows you to specify tests to run like:

python test another.test:TestCase.test_method

or as noted in comments, use the syntax:

python test another.test.TestCase.test_method
3/6/2017 6:15:38 PM

Since Django 1.6 you can run a complete test case, or single test, using the complete dot notation for the element you want to run.

Automatic test discovery will now find tests in any file that starts with test under the working directory, so addressing the question you would have to rename your files, but you can now keep them inside the directory you want. If you want to use custom file names you can specify a pattern (default Django test runner) with the option flag --pattern="my_pattern_*.py".

So if you are in your directory and want to run the test test_a inside TestCase subclass A inside a file under the app/module example you would do:

python test example.tests.A.test_a

If you don't want to include a dependency and are in Django 1.6 or later that's how you do it.

See the Django documentation for more information

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow