Unit testing in Django

Erick McCollum | 09 Dec 2021

DISCLAIMER: The opinions expressed on this website are solely my own, and they are not associated with my employer, another person, or another organization in any way. All information on this website is provided "as is", without guarantee or warranty of any kind. Read the full disclaimer here.

After creating a Django project and application, a default tests directory is created within the Django application directory. At this point, the following command can be run in order to execute all unit tests within a Django project:

    ./manage.py test

The above command will run all unit tests in any files that match the test*.py pattern. For example, a file named tests.py will match this pattern, but a file named validations.py will not. It is important to note that unit test files do not need to be contained within the tests/ directory. However, it is generally accepted as good practice.

Run only the unit tests in a specific sub-folder.

Some additional command line parameters can be passed to the manage.py test command in order to run specific unit tests, rather than running all of them. For example, it is possible to run only the unit tests within a specific sub-directory.

In order to only run the unit tests located in the tests/sub-tests sub-directory, the sub-directory needs to be configured as a Python package. This means that both the tests directory and the tests/sub-tests sub-directory need to have an __init__.py file created within them.

Once the sub-directory has been configured as a Python package, the following command can be used to run only the unit tests that are located in that package:

    ./manage.py test {app_name}.tests.sub-tests

It is also possible to simply pass a folder name to the manage.py test command, rather than configuring a folder as a package. For example:

    ./manage.py test {app_name}/tests/sub-tests

Sample code.

I have created a sample Django application that demonstrates some of the different ways to run unit tests for a Django project. The source code for this sample application can be found on my GitHub profile at the following link: code-samples/Django/unit-testing-django.

More Information

Django unit testing documentation: Writing and running tests