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

Simpler way to run pytest from manage.py script? #1169

Open
paduszyk opened this issue Jan 28, 2025 · 1 comment
Open

Simpler way to run pytest from manage.py script? #1169

paduszyk opened this issue Jan 28, 2025 · 1 comment

Comments

@paduszyk
Copy link

paduszyk commented Jan 28, 2025

Docs suggest to define custom test runner to invoke pytest (thus, related pytest-django functionality) via the manage.py script.

I used the following snippet and everything works as expected:

# manage.py
import sys
from pathlib import Path

import environ

from django.core.management import execute_from_command_line


def main():
    if (dotenv_path := Path(__file__).parent / ".env").exists():
        environ.Env.read_env(dotenv_path)

    if sys.argv[1:2] == ["test"]:
        import pytest

        sys.exit(pytest.main(sys.argv[2:]))

    execute_from_command_line(sys.argv)


if __name__ == "__main__":
    main()

What are the advantages of the solution presented in the docs compared to the one above? Thanks in advance for any feedback.

@bluetech
Copy link
Member

The only advantage is that it's a bit more integrated with Django, e.g. the command line parsing. But if what you did works for you, I don't see much problem with it!

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

2 participants