Skip to content

Commit 385a36a

Browse files
committed
Add test_unittest_interaction to test integration with unittest
Ref: #197 (comment)
1 parent 81138e5 commit 385a36a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/test_database.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,43 @@ def test_transactions_enabled(self):
162162
pytest.skip('transactions required for this test')
163163

164164
assert not noop_transactions()
165+
166+
167+
def test_unittest_interaction(django_testdir):
168+
"Test that (non-Django) unittests cannot access the DB."
169+
170+
django_testdir.create_test_module('''
171+
import pytest
172+
import unittest
173+
from .app.models import Item
174+
175+
class TestCase_setupClass(unittest.TestCase):
176+
@classmethod
177+
def setUpClass(cls):
178+
Item.objects.create(name='foo')
179+
180+
def test_db_access_1(self):
181+
Item.objects.count() == 1
182+
183+
class TestCase_setUp(unittest.TestCase):
184+
@classmethod
185+
def setUp(cls):
186+
Item.objects.create(name='foo')
187+
188+
def test_db_access_2(self):
189+
Item.objects.count() == 1
190+
191+
class TestCase(unittest.TestCase):
192+
def test_db_access_3(self):
193+
Item.objects.count() == 1
194+
''')
195+
196+
result = django_testdir.runpytest('-v', '--reuse-db')
197+
result.stdout.fnmatch_lines([
198+
"*test_db_access_1 ERROR*",
199+
"*test_db_access_2 FAILED*",
200+
"*test_db_access_3 FAILED*",
201+
"*ERROR at setup of TestCase_setupClass.test_db_access_1*",
202+
"*no such table: app_item*",
203+
"*Failed: Database access not allowed, use the \"django_db\" mark to enable*",
204+
])

0 commit comments

Comments
 (0)