Skip to content

Handle setUpClass also for unittest.TestCase #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,43 @@ def test_transactions_enabled(self):
pytest.skip('transactions required for this test')

assert not noop_transactions()


def test_unittest_interaction(django_testdir):
"Test that (non-Django) unittests cannot access the DB."

django_testdir.create_test_module('''
import pytest
import unittest
from .app.models import Item

class TestCase_setupClass(unittest.TestCase):
@classmethod
def setUpClass(cls):
Item.objects.create(name='foo')

def test_db_access_1(self):
Item.objects.count() == 1

class TestCase_setUp(unittest.TestCase):
@classmethod
def setUp(cls):
Item.objects.create(name='foo')

def test_db_access_2(self):
Item.objects.count() == 1

class TestCase(unittest.TestCase):
def test_db_access_3(self):
Item.objects.count() == 1
''')

result = django_testdir.runpytest('-v', '--reuse-db')
result.stdout.fnmatch_lines([
"*test_db_access_1 ERROR*",
"*test_db_access_2 FAILED*",
"*test_db_access_3 FAILED*",
"*ERROR at setup of TestCase_setupClass.test_db_access_1*",
"*no such table: app_item*",
"*Failed: Database access not allowed, use the \"django_db\" mark to enable*",
])