2👍
Django doesn’t know how to run pytest
-style test functions. Plain pytest
also doesn’t provide a django integration. You can either
-
write a custom test runner (a minimal example):
# myapp/runner.py class PytestRunner: def run_tests(self, test_labels): import pytest return pytest.main(test_labels)
and configure your app to use it instead of the default
DiscoverRunner
:# myapp/settings.py TEST_RUNNER = 'myapp.runner.PytestRunner'
Now
python manage.py test
will invokepytest
instead of the defaultunittest
. -
Or use the
pytest-django
plugin. Add it todeps
:deps = ... pytest pytest-django ...
This doesn’t provide integration with
manage.py test
per se, but you will be able to invoke tests as usual viapytest
:$ pytest --ds=myapp.settings ...
The plugin also provides many useful fixtures that reimplement Django’s test helpers (like
RequestFactory
,Client
etc) so you can do a complete port of theunittest
-style test classes topytest
-style test functions. Refer topytest-django
docs for configurations details and code examples.