Skip to content

A fixture which gives access to all databases in tests #1113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

dferens
Copy link

@dferens dferens commented Feb 13, 2024

Hi,

At my company we have a problem that we have to use mark on every test:

@pytest.mark.django_db(databases="__all__")
def test_something(db):
   ...

@pytest.mark.django_db(databases="__all__")
def test_other(db):
    ...

# + a bunch of tests which needs to be marked as well

I'd like to propose a solution:

def setup_db(db, django_db_all_databases):
    pass

def test_something(setup_db):
    ...

def test_other(setup_db):
    ...

Let me know what you think. If you approve this, I will improve this PR with tests, docs, docstrings and so on. I wanted to know your opinion first.

Thanks!

@dferens
Copy link
Author

dferens commented May 7, 2024

Hey, can anyone look into this?

@dferens dferens marked this pull request as ready for review May 7, 2024 14:25
@bluetech
Copy link
Member

bluetech commented Apr 1, 2025

I would not want to merge something like this since it encourages a bad practice. So my suggestion, if you want to do this, is to put the following in an initial conftest.py file:

import pytest

@pytest.hookimpl(tryfirst=True)
def pytest_collection_modifyitems(items):
    marker = pytest.mark.django_db(databases="__all__")
    for item in items:
        item.add_marker(marker)

@bluetech bluetech closed this Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants