From 54f4626209dc21d988cd84933038c3436e0bdb81 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Thu, 2 Dec 2021 09:31:08 +0200 Subject: [PATCH] Fix transactional tests in classes not sorted correctly after regular tests Regression in 4.5.0. Fix #975. --- pytest_django/plugin.py | 15 ++++--------- tests/test_db_setup.py | 47 +++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 845c4aec..f8ce8c2e 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -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: diff --git a/tests/test_db_setup.py b/tests/test_db_setup.py index 380c5662..8f10a680 100644 --- a/tests/test_db_setup.py +++ b/tests/test_db_setup.py @@ -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 @@ -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 @@ -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: