pip install pytest-django
- Make sure
DJANGO_SETTINGS_MODULEis defined and and run tests with the
- (Optional) If you want tests of Django’s default application layout be discovered (
tests.py), if you put your tests under a
tests/directory , or your files are not named
test_FOO.py, see the FAQ at My tests are not being found. Why?.
Table of Contents¶
- Getting started with pytest and pytest-django
- Configuring Django settings
- Managing the Python path
- Usage and invocations
- Database creation/re-use
- Enabling database access in tests
- Testing transactions
- Tests requiring multiple databases
--reuse-db- reuse the testing database between test runs
--create-db- force re creation of the test database
- Example work flow with
--nomigrations- Disable Django 1.7+ migrations
- Advanced database configuration
- Django helpers
- Environment autouse fixtures
- I see an error saying “could not import myproject.settings”
- How can I make sure that all my tests run with a specific locale?
- My tests are not being found. Why?
- Does pytest-django work with the pytest-xdist plugin?
- How can I use
manage.py testwith pytest-django?
- How can I give database access to all my tests without the django_db marker?
- How/where can I get help with pytest/pytest-django?
- Contributing to pytest-django
Why would I use this instead of Django’s manage.py test command?¶
Running the test suite with pytest offers some features that are not present in Django’s standard test mechanism:
- Less boilerplate: no need to import unittest, create a subclass with methods. Just write tests as regular functions.
- Manage test dependencies with fixtures.
- Database re-use: no need to re-create the test database for every test run.
- Run tests in multiple processes for increased speed.
- There are a lot of other nice plugins available for pytest.
- Easy switching: Existing unittest-style tests will still work without any modifications.
See the pytest documentation for more information on pytest.