Skip to content
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

pytest-django should handle MEDIA_ROOT #1126

Open
JasonGrace2282 opened this issue Jun 29, 2024 · 1 comment
Open

pytest-django should handle MEDIA_ROOT #1126

JasonGrace2282 opened this issue Jun 29, 2024 · 1 comment

Comments

@JasonGrace2282
Copy link

JasonGrace2282 commented Jun 29, 2024

Pytest django should redirect settings.MEDIA_ROOT automatically so that the state isn't shared between tests in the form of media.
For compatibility with pytest-xdist, maybe each test run can have it's own folder, so that folders can get deleted without other tests running at the same time being effected (note: this would be io heavy)

@JasonGrace2282
Copy link
Author

JasonGrace2282 commented Jun 30, 2024

For now I've implemented my own fixture

@pytest.fixture(autouse=True)
# the other fixtures come from pytest-xdist
# if not using that plugin just hash the current test
def media_setup(settings, worker_id: str, testrun_uid: str):
    # setup
    settings.MEDIA_ROOT = (
        Path(settings.BASE_DIR) / "test-media" / f"media-{worker_id}-{testrun_uid}"
    )

    # make sure no old/manual stuff added affects tests
    if settings.MEDIA_ROOT.exists():
        shutil.rmtree(settings.MEDIA_ROOT)
    settings.MEDIA_ROOT.mkdir(parents=True)

    yield
    # cleanup the media so it doesn't cause
    # problems elsewhere
    if settings.MEDIA_ROOT.exists():
        shutil.rmtree(settings.MEDIA_ROOT)

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

No branches or pull requests

1 participant