diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 6dc05fdb..2362713a 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -124,7 +124,7 @@ def django_db_createdb(request: pytest.FixtureRequest) -> bool: def _get_databases_for_test(test: pytest.Item) -> tuple[Iterable[str], bool]: """Get the database aliases that need to be setup for a test, and whether they need to be serialized.""" - from django.db import DEFAULT_DB_ALIAS, connections + from django.db import connections from django.test import TransactionTestCase test_cls = getattr(test, "cls", None) @@ -147,9 +147,7 @@ def _get_databases_for_test(test: pytest.Item) -> tuple[Iterable[str], bool]: databases = None else: return (), False - if databases is None: - return (DEFAULT_DB_ALIAS,), serialized_rollback - elif databases == "__all__": + if databases is None or databases == "__all__": return connections, serialized_rollback else: return databases, serialized_rollback diff --git a/tests/test_database.py b/tests/test_database.py index c6389756..d4f43d21 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -454,6 +454,35 @@ def test_db_access(self): result.assert_outcomes(passed=1) +def test_fixture_multi_db(django_pytester: DjangoPytester) -> None: + """Test that fixture multi-db support works.""" + + django_pytester.create_test_module( + """ + import pytest + from django.test import TestCase + from .app.models import Item, SecondItem + + TestCase.databases = ["default", "second"] + + @pytest.fixture + def item(db): + return Item.objects.create(name="test") + + @pytest.fixture + def second_item(db): + return SecondItem.objects.create(name="test") + + def test_db_access(item, second_item): + Item.objects.count() == 1 + SecondItem.objects.count() == 1 + """ + ) + + result = django_pytester.runpytest_subprocess("-v", "--reuse-db") + result.assert_outcomes(passed=1) + + class Test_database_blocking: def test_db_access_in_conftest(self, django_pytester: DjangoPytester) -> None: """Make sure database access in conftest module is prohibited."""