Does pants support concurrent tests with the pytes...
# general
l
Does pants support concurrent tests with the pytest-django plugin? I found that running tests from multiple files that all make database changes leads to errors like "Can't create database...". I found a workaround by creating a random test database name when a test is executing but this feels like a hack to me. Workaround code is in the thread. Thanks in advance!
Example solution:
Copy code
# settings.py
    import os, random
    
    TESTING = os.environ.get("PANTS_EXECUTION_SLOT") is not None
    def get_test_db_name():
       suffix = os.getenv("TEST_RUN_ID", str(random.randint(1000, 9999)))
       return f"test_database_{suffix}"
    
    DATABASES = {
        "default": {
               **REST_OF_DB_CONFIG,
              "NAME": get_test_db_name() if TESTING else 'app_db',
          }
      }
Copy code
# pants.toml
[pytest]
execution_slot_var = "PANTS_EXECUTION_SLOT"
b
I don't know anything about Django, but I did notice https://www.pantsbuild.org/2.20/docs/python/goals/test#setting-environment-variables has a callout that references Django specifically, which uses the
PANTS_EXECUTION_SLOT
value directly, rather than a random number. Although potentially you've already seen it, because your code looks like it might be inspired by it 🙂
👀 1
l
Great success! Thank you! Just for easier reading for future readers here is the explanation of the fix from https://www.pantsbuild.org/2.20/docs/python/goals/test#setting-environment-variables which adds a different prefix for each concurrent thread to the test database name so the database creation and teardown between different threads doesn't conflict. pants.toml
Copy code
[pytest]
execution_slot_var = "PANTS_EXECUTION_SLOT"
src/conftest.py
Copy code
from pytest_django.fixtures import _set_suffix_to_test_databases
from pytest_django.lazy_django import skip_if_no_django

@pytest.fixture(scope="session")
def django_db_modify_db_settings():
    skip_if_no_django()
    if "PANTS_EXECUTION_SLOT" in os.environ:
        _set_suffix_to_test_databases(os.environ["PANTS_EXECUTION_SLOT"])
👍 1