448 lines (331 sloc) 13.4 KB
"""All pytest-django fixtures"""
from __future__ import with_statement
import os
import warnings
from contextlib import contextmanager
from functools import partial
import pytest
from . import live_server_helper
from .django_compat import is_django_unittest
from .lazy_django import skip_if_no_django
__all__ = [
def django_db_modify_db_settings_xdist_suffix(request):
from django.conf import settings
for db_settings in settings.DATABASES.values():
test_name = db_settings["TEST"]["NAME"]
except KeyError:
test_name = None
if not test_name:
if db_settings["ENGINE"] == "django.db.backends.sqlite3":
return ":memory:"
test_name = "test_{}".format(db_settings["NAME"])
# Put a suffix like _gw0, _gw1 etc on xdist processes
xdist_suffix = getattr(request.config, "slaveinput", {}).get("slaveid")
if test_name != ":memory:" and xdist_suffix is not None:
test_name = "{}_{}".format(test_name, xdist_suffix)
db_settings.setdefault("TEST", {})
db_settings["TEST"]["NAME"] = test_name
def django_db_modify_db_settings(django_db_modify_db_settings_xdist_suffix):
def django_db_use_migrations(request):
return not request.config.getvalue("nomigrations")
def django_db_keepdb(request):
return request.config.getvalue("reuse_db")
def django_db_createdb(request):
return request.config.getvalue("create_db")
def django_db_setup(
"""Top level fixture to ensure test databases are available"""
from .compat import setup_databases, teardown_databases
setup_databases_args = {}
if not django_db_use_migrations:
if django_db_keepdb and not django_db_createdb:
setup_databases_args["keepdb"] = True
with django_db_blocker.unblock():
db_cfg = setup_databases(
def teardown_database():
with django_db_blocker.unblock():
teardown_databases(db_cfg, verbosity=request.config.option.verbose)
if not django_db_keepdb:
def _django_db_fixture_helper(
request, django_db_blocker, transactional=False, reset_sequences=False
if is_django_unittest(request):
if not transactional and "live_server" in request.funcargnames:
# Do nothing, we get called with transactional=True, too.
if transactional:
from django.test import TransactionTestCase as django_case
if reset_sequences:
class ResetSequenceTestCase(django_case):
reset_sequences = True
django_case = ResetSequenceTestCase
from django.test import TestCase as django_case
test_case = django_case(methodName="__init__")
def _disable_native_migrations():
from django.conf import settings
from .migrations import DisableMigrations
settings.MIGRATION_MODULES = DisableMigrations()
# ############### User visible fixtures ################
def db(request, django_db_setup, django_db_blocker):
"""Require a django test database.
This database will be setup with the default fixtures and will have
the transaction management disabled. At the end of the test the outer
transaction that wraps the test itself will be rolled back to undo any
changes to the database (in case the backend supports transactions).
This is more limited than the ``transactional_db`` resource but
If multiple database fixtures are requested, they take precedence
over each other in the following order (the last one wins): ``db``,
``transactional_db``, ``django_db_reset_sequences``.
if "django_db_reset_sequences" in request.funcargnames:
if (
"transactional_db" in request.funcargnames
or "live_server" in request.funcargnames
_django_db_fixture_helper(request, django_db_blocker, transactional=False)
def transactional_db(request, django_db_setup, django_db_blocker):
"""Require a django test database with transaction support.
This will re-initialise the django database for each test and is
thus slower than the normal ``db`` fixture.
If you want to use the database with transactions you must request
this resource.
If multiple database fixtures are requested, they take precedence
over each other in the following order (the last one wins): ``db``,
``transactional_db``, ``django_db_reset_sequences``.
if "django_db_reset_sequences" in request.funcargnames:
_django_db_fixture_helper(request, django_db_blocker, transactional=True)
def django_db_reset_sequences(request, django_db_setup, django_db_blocker):
"""Require a transactional test database with sequence reset support.
This behaves like the ``transactional_db`` fixture, with the addition
of enforcing a reset of all auto increment sequences. If the enquiring
test relies on such values (e.g. ids as primary keys), you should
request this resource to ensure they are consistent across tests.
If multiple database fixtures are requested, they take precedence
over each other in the following order (the last one wins): ``db``,
``transactional_db``, ``django_db_reset_sequences``.
request, django_db_blocker, transactional=True, reset_sequences=True
def client():
"""A Django test client instance."""
from django.test.client import Client
return Client()
def django_user_model(db):
"""The class of Django's user model."""
from django.contrib.auth import get_user_model
return get_user_model()
def django_username_field(django_user_model):
"""The fieldname for the username used with Django's user model."""
return django_user_model.USERNAME_FIELD
def admin_user(db, django_user_model, django_username_field):
"""A Django admin user.
This uses an existing user with username "admin", or creates a new one with
password "password".
UserModel = django_user_model
username_field = django_username_field
username = "" if username_field == "email" else "admin"
user = UserModel._default_manager.get(**{username_field: username})
except UserModel.DoesNotExist:
extra_fields = {}
if username_field not in ("username", "email"):
extra_fields[username_field] = "admin"
user = UserModel._default_manager.create_superuser(
username, "", "password", **extra_fields
return user
def admin_client(db, admin_user):
"""A Django test client logged in as an admin user."""
from django.test.client import Client
client = Client()
client.login(username=admin_user.username, password="password")
return client
def rf():
"""RequestFactory instance"""
from django.test.client import RequestFactory
return RequestFactory()
class SettingsWrapper(object):
_to_restore = []
def __delattr__(self, attr):
from django.test import override_settings
override = override_settings()
from django.conf import settings
delattr(settings, attr)
def __setattr__(self, attr, value):
from django.test import override_settings
override = override_settings(**{attr: value})
def __getattr__(self, item):
from django.conf import settings
return getattr(settings, item)
def finalize(self):
for override in reversed(self._to_restore):
del self._to_restore[:]
def settings():
"""A Django settings object which restores changes after the testrun"""
wrapper = SettingsWrapper()
yield wrapper
def live_server(request):
"""Run a live Django server in the background during tests
The address the server is started from is taken from the
--liveserver command line option or if this is not provided from
the DJANGO_LIVE_TEST_SERVER_ADDRESS environment variable. If
neither is provided ``localhost:8081,8100-8200`` is used. See the
Django documentation for its full syntax.
NOTE: If the live server needs database access to handle a request
your test will have to request database access. Furthermore
when the tests want to see data added by the live-server (or
the other way around) transactional database access will be
needed as data inside a transaction is not shared between
the live server and test code.
Static assets will be automatically served when
``django.contrib.staticfiles`` is available in INSTALLED_APPS.
import django
addr = request.config.getvalue("liveserver") or os.getenv(
if addr and ":" in addr:
if django.VERSION >= (1, 11):
ports = addr.split(":")[1]
if "-" in ports or "," in ports:
"Specifying multiple live server ports is not supported "
"in Django 1.11. This will be an error in a future "
"pytest-django release.",
if not addr:
if django.VERSION < (1, 11):
addr = "localhost:8081,8100-8200"
addr = "localhost"
server = live_server_helper.LiveServer(addr)
return server
@pytest.fixture(autouse=True, scope="function")
def _live_server_helper(request):
"""Helper to make live_server work, internal to pytest-django.
This helper will dynamically request the transactional_db fixture
for a test which uses the live_server fixture. This allows the
server and test to access the database without having to mark
this explicitly which is handy since it is usually required and
matches the Django behaviour.
The separate helper is required since live_server can not request
transactional_db directly since it is session scoped instead of
It will also override settings only for the duration of the test.
if "live_server" not in request.funcargnames:
live_server = request.getfixturevalue("live_server")
def _assert_num_queries(config, num, exact=True, connection=None, info=None):
from django.test.utils import CaptureQueriesContext
if connection is None:
from django.db import connection
verbose = config.getoption("verbose") > 0
with CaptureQueriesContext(connection) as context:
yield context
num_performed = len(context)
if exact:
failed = num != num_performed
failed = num_performed > num
if failed:
msg = "Expected to perform {} queries {}{}".format(
"" if exact else "or less ",
"but {} done".format(
num_performed == 1 and "1 was" or "%d were" % (num_performed,)
if info:
msg += "\n{}".format(info)
if verbose:
sqls = (q["sql"] for q in context.captured_queries)
msg += "\n\nQueries:\n========\n\n%s" % "\n\n".join(sqls)
msg += " (add -v option to show queries)"
def django_assert_num_queries(pytestconfig):
return partial(_assert_num_queries, pytestconfig)
def django_assert_max_num_queries(pytestconfig):
return partial(_assert_num_queries, pytestconfig, exact=False)