I am trying to use pytest-xdist for running parallel tests. It works and I see improved performance.
To improve further more, I wanted to provide it multiple databases by using django_db_modify_db_settings_xdist_suffix
I override that functions in my conftest.py.
Since I have four workers, I created four databases manually. Then I use conftest to modify the settings.DATABASES to test for DBNAME against DBNAME_
I verified that my settings.DATABASES has changed and is correct.
But the queries are still going to old db DBNAME (which is no longer in my settings.DATABASES)
What could be going wrong?
Do I need to make any other change? Or change in conftest.py fixture is enough?
Thanks in advance for any help or direction.
EDIT: My conftest:py has lot of stuff. At the end of django_db_modify_db_settings_xdist_suffix if I log settings.DATABASES it shows me correct & expected info. But the queries still go to different db. conftest.py is same in both runs (pytest -n4 or pytest). Since it depends on xdist_suffix it modifies the settings.DATABASE value in "-n 4" run only. Two relevant functions which I think are important here:
@pytest.fixture(scope="session")
def django_db_setup(
request,
django_test_environment,
django_db_blocker,
django_db_use_migrations,
django_db_keepdb,
django_db_createdb,
django_db_modify_db_settings,
):
pass
And
@pytest.fixture(scope="session")
def django_db_modify_db_settings_xdist_suffix(request):
from django.conf import settings
default = settings.DATABASES["default"].copy()
settings.DATABASES.clear()
settings.DATABASES["default"] = default
xdist_suffix = None
xdist_worker_id = get_xdist_worker_id(request)
if xdist_worker_id != 'master':
xdist_suffix = xdist_worker_id
if xdist_suffix:
for db_settings in settings.DATABASES.values():
test_name = db_settings.get("TEST", {}).get("NAME")
if not test_name:
test_name = "test_{}".format(db_settings["NAME"])
db_settings.setdefault("TEST", {})
db_settings["TEST"]["NAME"] = "{}_{}".format(test_name, xdist_suffix)
db_settings["NAME"] = db_settings["TEST"]["NAME"]