Skip to content

Commit

Permalink
Fix transactional tests in classes not sorted correctly after regular…
Browse files Browse the repository at this point in the history
… tests

Regression in 4.5.0.

Fix #975.
  • Loading branch information
bluetech committed Dec 2, 2021
1 parent a920a7f commit 54f4626
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
15 changes: 4 additions & 11 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,10 @@ def pytest_collection_modifyitems(items: List[pytest.Item]) -> None:

def get_order_number(test: pytest.Item) -> int:
test_cls = getattr(test, "cls", None)
if test_cls:
# Beware, TestCase is a subclass of TransactionTestCase
if issubclass(test_cls, TestCase):
uses_db = True
transactional = False
elif issubclass(test_cls, TransactionTestCase):
uses_db = True
transactional = True
else:
uses_db = False
transactional = False
if test_cls and issubclass(test_cls, TransactionTestCase):
# Note, TestCase is a subclass of TransactionTestCase.
uses_db = True
transactional = not issubclass(test_cls, TestCase)
else:
marker_db = test.get_closest_marker('django_db')
if marker_db:
Expand Down
47 changes: 29 additions & 18 deletions tests/test_db_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ def test_db_order(django_testdir) -> None:
"""Test order in which tests are being executed."""

django_testdir.create_test_module('''
from unittest import TestCase
import pytest
from django.test import SimpleTestCase, TestCase as DjangoTestCase, TransactionTestCase
from unittest import TestCase
from django.test import SimpleTestCase
from django.test import TestCase as DjangoTestCase
from django.test import TransactionTestCase
from .app.models import Item
Expand All @@ -45,13 +47,32 @@ def test_run_second_fixture(transactional_db):
def test_run_second_reset_sequences_fixture(django_db_reset_sequences):
pass
class MyTransactionTestCase(TransactionTestCase):
def test_run_second_transaction_test_case(self):
pass
def test_run_first_fixture(db):
pass
class TestClass:
def test_run_second_fixture_class(self, transactional_db):
pass
def test_run_first_fixture_class(self, db):
pass
@pytest.mark.django_db(reset_sequences=True)
def test_run_second_reset_sequences_decorator():
pass
class MyDjangoTestCase(DjangoTestCase):
def test_run_first_django_test_case(self):
pass
class MySimpleTestCase(SimpleTestCase):
def test_run_last_simple_test_case(self):
pass
@pytest.mark.django_db
def test_run_first_decorator():
pass
Expand All @@ -63,34 +84,24 @@ def test_run_first_serialized_rollback_decorator():
class MyTestCase(TestCase):
def test_run_last_test_case(self):
pass
class MySimpleTestCase(SimpleTestCase):
def test_run_last_simple_test_case(self):
pass
class MyDjangoTestCase(DjangoTestCase):
def test_run_first_django_test_case(self):
pass
class MyTransactionTestCase(TransactionTestCase):
def test_run_second_transaction_test_case(self):
pass
''')
result = django_testdir.runpytest_subprocess('-q', '--collect-only')
assert result.ret == 0
result.stdout.fnmatch_lines([
"*test_run_first_fixture*",
"*test_run_first_fixture_class*",
"*test_run_first_django_test_case*",
"*test_run_first_decorator*",
"*test_run_first_serialized_rollback_decorator*",
"*test_run_first_django_test_case*",
"*test_run_second_decorator*",
"*test_run_second_fixture*",
"*test_run_second_reset_sequences_fixture*",
"*test_run_second_reset_sequences_decorator*",
"*test_run_second_transaction_test_case*",
"*test_run_last_test_case*",
"*test_run_second_fixture_class*",
"*test_run_second_reset_sequences_decorator*",
"*test_run_last_simple_test_case*",
])
"*test_run_last_test_case*",
], consecutive=True)


def test_db_reuse(django_testdir) -> None:
Expand Down

0 comments on commit 54f4626

Please sign in to comment.