From fc676290faa072697b2d6e6f9eae35431672a73b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 20 Jan 2015 21:03:37 +0100 Subject: [PATCH] Add test_unittest_interaction to test integration with unittest Ref: https://github.com/pytest-dev/pytest-django/pull/197#issuecomment-71329313 --- tests/test_database.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/test_database.py b/tests/test_database.py index 65f4fc0e0..0a2449cd1 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -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*", + ])