pytest-django Documentation
pytest-django is a plugin for pytest that provides a set of useful tools for testing Django applications and projects.
Quick Start
$ pip install pytest-django
Make sure DJANGO_SETTINGS_MODULE
is defined (see
Configuring Django settings) and make your tests discoverable
(see My tests are not being found. Why?):
Example using pytest.ini or tox.ini
# -- FILE: pytest.ini (or tox.ini)
[pytest]
DJANGO_SETTINGS_MODULE = test.settings
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py
Example using pyproject.toml
# -- Example FILE: pyproject.toml
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "test.settings"
# -- recommended but optional:
python_files = ["test_*.py", "*_test.py", "testing/python/*.py"]
Run your tests with pytest
:
$ pytest
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.
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.
Bugs? Feature Suggestions?
Report issues and feature requests at the GitHub issue tracker.
Table of Contents
- Getting started with pytest and pytest-django
- Configuring Django settings
- Managing the Python path
- Usage and invocations
- Database access
- 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
--reuse-db
and--create-db
. --no-migrations
- Disable Django migrations- Advanced database configuration
- Django helpers
- Assertions
- Markers
- Fixtures
rf
-RequestFactory
async_rf
-AsyncRequestFactory
client
-django.test.Client
async_client
-django.test.AsyncClient
admin_client
-django.test.Client
logged in as adminadmin_user
- an admin user (superuser)django_user_model
django_username_field
db
transactional_db
django_db_reset_sequences
django_db_serialized_rollback
live_server
settings
django_assert_num_queries
django_assert_max_num_queries
django_capture_on_commit_callbacks
mailoutbox
- Automatic cleanup
- FAQ
- I see an error saying “could not import myproject.settings”
- Are Django test tags supported?
- 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 test
with 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
- Changelog
- v4.8.0 (2024-01-30)
- v4.7.0 (2023-11-08)
- v4.6.0 (2023-10-30)
- v4.5.2 (2021-12-07)
- v4.5.1 (2021-12-02)
- v4.5.0 (2021-12-01)
- v4.4.0 (2021-06-06)
- v4.3.0 (2021-05-15)
- v4.2.0 (2021-04-10)
- v4.1.0 (2020-10-22)
- v4.0.0 (2020-10-16)
- v3.10.0 (2020-08-25)
- v3.9.0 (2020-03-31)
- v3.8.0 (2020-01-14)
- v3.7.0 (2019-11-09)
- v3.6.0 (2019-10-17)
- v3.5.1 (2019-06-29)
- v3.5.0 (2019-06-03)
- v3.4.8 (2019-02-26)
- v3.4.7 (2019-02-03)
- v3.4.6 (2019-02-01)
- v3.4.5 (2019-01-07)
- v3.4.4 (2018-11-13)
- v3.4.3 (2018-09-16)
- v3.4.2 (2018-08-20)
- v3.4.0 (2018-08-16)
- v3.3.3 (2018-07-26)
- v3.3.2 (2018-06-21)
- v3.3.0 (2018-06-15)
- v3.2.1
- v3.2.0
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.0
- v2.9.1
- v2.9.0
- v2.8.0
- v2.7.0
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.1
- v2.5.0
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- v1.4
- v1.3
- v1.2.2
- v1.2.1
- v1.2
- v1.1.1
- v1.1