pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

Idea: Add support to specify db suffix from command line

Open gmuj opened this issue 2 years ago • 0 comments
trafficstars

I am working on a big project which has a lot of migrations and I am bouncing from one branch to another where there will be differences between migrations (eg develop vs master). So sometimes I cannot reuse the existing test database when I checkout a different branch.

So my thought was to add a specific db suffix that can be used to specify different test db (based on my branch). The following code did the trick:

from pytest_django.fixtures import _set_suffix_to_test_databases
from pytest_django.lazy_django import skip_if_no_django

 def pytest_addoption(parser) -> None:
    """pytest django plugin enhancements.
    Adds pytest command line argument to specify test database suffix.
    """
    group = parser.getgroup("django")
    group.addoption(
        "--db-suffix",
        action="store",
        dest="db_suffix",
        default=None,
        help="Use specific test database suffix.",
    )


@pytest.fixture(scope="session")
def django_db_suffix(request) -> str:
    return request.config.getvalue("db_suffix")


@pytest.fixture(scope="session", autouse=True)
def django_db_modify_db_settings_config_suffix(django_db_suffix) -> None:
    skip_if_no_django()

    if django_db_suffix:
        _set_suffix_to_test_databases(suffix=django_db_suffix)

I was wondering if this can be added to that library and help others.

gmuj avatar Dec 07 '22 07:12 gmuj