pytest-dev / pytest-dev/pytest-django
Idea: Add support to specify db suffix from command line
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 367
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with pytest_django.fixtures._set_suffix_to_test_databases and the pytest_addoption and django_db_suffix entry points shown in the issue. Trace how test-database settings are modified, then verify that a command-line suffix selects an isolated test database without changing the default behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- cli, databases, testing
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100