Skip to content

Commit 54f4626

Browse files
committed
Fix transactional tests in classes not sorted correctly after regular tests
Regression in 4.5.0. Fix #975.
1 parent a920a7f commit 54f4626

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

pytest_django/plugin.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -377,17 +377,10 @@ def pytest_collection_modifyitems(items: List[pytest.Item]) -> None:
377377

378378
def get_order_number(test: pytest.Item) -> int:
379379
test_cls = getattr(test, "cls", None)
380-
if test_cls:
381-
# Beware, TestCase is a subclass of TransactionTestCase
382-
if issubclass(test_cls, TestCase):
383-
uses_db = True
384-
transactional = False
385-
elif issubclass(test_cls, TransactionTestCase):
386-
uses_db = True
387-
transactional = True
388-
else:
389-
uses_db = False
390-
transactional = False
380+
if test_cls and issubclass(test_cls, TransactionTestCase):
381+
# Note, TestCase is a subclass of TransactionTestCase.
382+
uses_db = True
383+
transactional = not issubclass(test_cls, TestCase)
391384
else:
392385
marker_db = test.get_closest_marker('django_db')
393386
if marker_db:

tests/test_db_setup.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ def test_db_order(django_testdir) -> None:
2929
"""Test order in which tests are being executed."""
3030

3131
django_testdir.create_test_module('''
32-
from unittest import TestCase
3332
import pytest
34-
from django.test import SimpleTestCase, TestCase as DjangoTestCase, TransactionTestCase
33+
from unittest import TestCase
34+
from django.test import SimpleTestCase
35+
from django.test import TestCase as DjangoTestCase
36+
from django.test import TransactionTestCase
3537
3638
from .app.models import Item
3739
@@ -45,13 +47,32 @@ def test_run_second_fixture(transactional_db):
4547
def test_run_second_reset_sequences_fixture(django_db_reset_sequences):
4648
pass
4749
50+
class MyTransactionTestCase(TransactionTestCase):
51+
def test_run_second_transaction_test_case(self):
52+
pass
53+
4854
def test_run_first_fixture(db):
4955
pass
5056
57+
class TestClass:
58+
def test_run_second_fixture_class(self, transactional_db):
59+
pass
60+
61+
def test_run_first_fixture_class(self, db):
62+
pass
63+
5164
@pytest.mark.django_db(reset_sequences=True)
5265
def test_run_second_reset_sequences_decorator():
5366
pass
5467
68+
class MyDjangoTestCase(DjangoTestCase):
69+
def test_run_first_django_test_case(self):
70+
pass
71+
72+
class MySimpleTestCase(SimpleTestCase):
73+
def test_run_last_simple_test_case(self):
74+
pass
75+
5576
@pytest.mark.django_db
5677
def test_run_first_decorator():
5778
pass
@@ -63,34 +84,24 @@ def test_run_first_serialized_rollback_decorator():
6384
class MyTestCase(TestCase):
6485
def test_run_last_test_case(self):
6586
pass
66-
67-
class MySimpleTestCase(SimpleTestCase):
68-
def test_run_last_simple_test_case(self):
69-
pass
70-
71-
class MyDjangoTestCase(DjangoTestCase):
72-
def test_run_first_django_test_case(self):
73-
pass
74-
75-
class MyTransactionTestCase(TransactionTestCase):
76-
def test_run_second_transaction_test_case(self):
77-
pass
7887
''')
7988
result = django_testdir.runpytest_subprocess('-q', '--collect-only')
8089
assert result.ret == 0
8190
result.stdout.fnmatch_lines([
8291
"*test_run_first_fixture*",
92+
"*test_run_first_fixture_class*",
93+
"*test_run_first_django_test_case*",
8394
"*test_run_first_decorator*",
8495
"*test_run_first_serialized_rollback_decorator*",
85-
"*test_run_first_django_test_case*",
8696
"*test_run_second_decorator*",
8797
"*test_run_second_fixture*",
8898
"*test_run_second_reset_sequences_fixture*",
89-
"*test_run_second_reset_sequences_decorator*",
9099
"*test_run_second_transaction_test_case*",
91-
"*test_run_last_test_case*",
100+
"*test_run_second_fixture_class*",
101+
"*test_run_second_reset_sequences_decorator*",
92102
"*test_run_last_simple_test_case*",
93-
])
103+
"*test_run_last_test_case*",
104+
], consecutive=True)
94105

95106

96107
def test_db_reuse(django_testdir) -> None:

0 commit comments

Comments
 (0)