Skip to content

Commit fa4b6e9

Browse files
author
Alexander Karev
committed
xdist-one-db
1 parent 9b36d83 commit fa4b6e9

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

pytest_django/fixtures.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def _django_db_setup(request,
3232

3333
# xdist
3434
if hasattr(request.config, 'slaveinput'):
35-
db_suffix = request.config.slaveinput['slaveid']
35+
if request.config.getvalue('xdist_one_db'):
36+
db_suffix = None
37+
else:
38+
db_suffix = request.config.slaveinput['slaveid']
3639
else:
3740
db_suffix = None
3841

pytest_django/plugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def pytest_addoption(parser):
3939
action='store_true', dest='create_db', default=False,
4040
help='Re-create the database, even if it exists. This '
4141
'option will be ignored if not --reuse-db is given.')
42+
group._addoption('--xdist-one-db',
43+
action='store_true', dest='xdist_one_db', default=False,
44+
help='Use only one database with xdist plugin.')
4245
group._addoption('--ds',
4346
action='store', type='string', dest='ds', default=None,
4447
help='Set DJANGO_SETTINGS_MODULE.')

tests/test_db_setup.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,40 @@ def test_b(settings):
109109
result.stdout.fnmatch_lines(['*PASSED*test_b*'])
110110

111111

112+
@skip_on_python32
113+
def test_xdist_one_db(django_testdir):
114+
skip_if_sqlite()
115+
116+
django_testdir.create_test_module('''
117+
import pytest
118+
119+
from .app.models import Item
120+
121+
def _check(settings):
122+
# Make sure that the database name looks correct
123+
db_name = settings.DATABASES['default']['NAME']
124+
assert not 'gw' in db_name
125+
126+
assert Item.objects.count() == 0
127+
Item.objects.create(name='foo')
128+
assert Item.objects.count() == 1
129+
130+
131+
@pytest.mark.django_db
132+
def test_a(settings):
133+
_check(settings)
134+
135+
136+
@pytest.mark.django_db
137+
def test_b(settings):
138+
_check(settings)
139+
''')
140+
141+
result = django_testdir.runpytest('-vv', '-n2', '-s', '--reuse-db', '--xdist-one-db')
142+
result.stdout.fnmatch_lines(['*PASSED*test_a*'])
143+
result.stdout.fnmatch_lines(['*PASSED*test_b*'])
144+
145+
112146
class TestSqliteWithXdist:
113147

114148
pytestmark = skip_on_python32

0 commit comments

Comments
 (0)