Description
This issue replaces some historical issues: #76, #342, #423, #461, #828, #838, #839 (probably a partial list).
Background
Django supports multi databases. This means defining multiple entries in the DATABASE
setting, which then allows directly certain queries to certain databases.
One case is when an extra database is entirely independent, has its own migrations, setups etc.
Second case is when an extra database is readonly, only used for read queries, not managed by Django.
Third case is a readonly replica, for this Django provides the MIRROR
setting
Django allows configuring the order in which test databases are set up.
Django's multi-db testing support
pytest-django mostly relies on Django's underlying TransactionTestCase
and TestCase
classes for dealing with DB setups and such. Each pytest-django test gets run in a dynamically-generated TestCase
/TransactionTestCase
.
The main setting for mutli-db support is TransactionTestCase.databases
. This tells Django which databases to consider for the test case. By default it's only default
. It's possible to specify __all__
to include all databases.
Historical note: The TransactionTestCase.databases
attribute was added in Django 2.2. Before that a multi_db
attribute was used. pytest-django only supports Django>=2.2 so we happily don't need to concern ourselves with that.
Previous attempts
#397 - Adds multi_db=True
argument to pytest.mark.django_db()
, adds django_multi_db
fixture. Problem: uses the old multi_db
attribute instead of the databases
attribute.
#431 - Adds django_db_testcase
fixture which allows the user to completely customize the test case class, including setting databases
. Rejected for being too flexible, I'd prefer direct support for multi-db.
#896 - Adds a global per-database setting for whether to add to the databases
value or not. Rejected because I think it should be possible to customize per-test.
Proposed solution
IMO we want something like #397/#416, but modernized to use databases
instead of multi_db
. The fixture part would be a bit problematic because it's no longer just a boolean (fixture enabled/not enabled), but a list of database aliases. So some solution would be needed for that, or maybe only the mark would be supported.
I'll try to work on it myself, but if for some reason I don't, PRs are definitely welcome!