Skip to content

Commit 9d3f2b2

Browse files
authored
Use django.test.utils.{setup_databases,teardown_databases} when available. (#379)
This removes the ugly workaround of instantiating DiscoverRunner. This fixes #376.
1 parent 1645d4c commit 9d3f2b2

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

pytest_django/compat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
try:
2+
# Django 1.11
3+
from django.test.utils import setup_databases, teardown_databases # noqa
4+
except ImportError:
5+
# In Django prior to 1.11, teardown_databases is only available as a method on DiscoverRunner
6+
from django.test.runner import setup_databases, DiscoverRunner as _DiscoverRunner # noqa
7+
8+
def teardown_databases(db_cfg, verbosity):
9+
(_DiscoverRunner(verbosity=verbosity,
10+
interactive=False)
11+
.teardown_databases(db_cfg))

pytest_django/fixtures.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def django_db_setup(
7272
django_db_modify_db_settings,
7373
):
7474
"""Top level fixture to ensure test databases are available"""
75-
from django.test.runner import setup_databases, DiscoverRunner
75+
from .compat import setup_databases, teardown_databases
7676

7777
setup_databases_args = {}
7878

@@ -98,9 +98,10 @@ def django_db_setup(
9898

9999
def teardown_database():
100100
with django_db_blocker:
101-
(DiscoverRunner(verbosity=pytest.config.option.verbose,
102-
interactive=False)
103-
.teardown_databases(db_cfg))
101+
teardown_databases(
102+
db_cfg,
103+
verbosity=pytest.config.option.verbose,
104+
)
104105

105106
if not django_db_keepdb:
106107
request.addfinalizer(teardown_database)

0 commit comments

Comments
 (0)