Andreas Pelme gave a talk at EuroPython 2013 on testing in Django with py.test. It shows the benefits of using py.test and how to get started:
pytest-django is available directly from PyPi, and can be easily installed with pip:
pip install pytest-django
pytest-django uses py.test‘s module system and can be used right away after installation, there is nothing more to configure.
Tests are invoked directly with the py.test command, instead of manage.py test/django-admin.py test:
Django needs the environment variable DJANGO_SETTINGS_MODULE set for tests runs to work. This plugin allows you to specify this in multiple ways:
- Using the --ds command line option.
- By writing a DJANGO_SETTINGS_MODULE setting in the [pytest] section of your py.test configuration file
- By using the DJANGO_SETTINGS_MODULE environment variable.
py.test will find tests in your project automatically, INSTALLED_APPS will not be consulted. This means that 3rd-party and django.contrib.* apps will not be picked up by the test runner.
If you use virtualenv you can automatically use the environment variable. See faq-django-settings-module.
If you are interested in how to select specific tests, format tracebacks in different way, see the excellent py.test documentation.