pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.37k stars 344 forks source link

Fix env-var name for parallel tox tests #1112

Open meshy opened 7 months ago

meshy commented 7 months ago

When running tests in parallel, we need to add a suffix to Django DB names to avoid clashes. This previously used TOX_PARALLEL_ENV, but Tox no longer sets this environment variable.

Tox does set an environment variable called TOX_ENV_NAME, which works for this purpose, so use that instead.


I was able to get my local Tox tests running in parallel using this fixture, and it seemed as though the version in this repo should be updated.

import os
import pytest
from pytest_django import fixtures

@pytest.fixture(scope="session")
def django_db_modify_db_settings_tox_suffix() -> None:
    fixtures.skip_if_no_django()

    tox_environment = os.getenv("TOX_ENV_NAME")
    if tox_environment:
        fixtures._set_suffix_to_test_databases(suffix=tox_environment)
meshy commented 7 months ago

Oh dear, this broke more tests that I accounted for. I've moved this into draft for now.