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 · 2 comments
Open

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

paduszyk opened this issue Jan 28, 2025 · 2 comments

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!

@kingbuzzman
Copy link
Contributor

if anyone ever tried to debug this / add any arguments, this will get chaotic quick.

Personally, I use the pytest args --last-failed / -x / -s / -vv / --pdb daily. Not being able to specify them at will, because you dont want them on all the time, makes this solution very poor.
And --pdbcls=IPython.terminal.debugger:TerminalPdb is on by default inside pyproject.toml. Example

But like @bluetech, said, if this solution works for you, 👍


@bluetech Why isn't PytestTestRunner included in the library to lower the entry level? It would be much faster to add TEST_RUNNER = 'pytest_django.runner.PytestTestRunner' than it is to copy it somewhere into your own repo and maintain it -- if you need more customizing just override it with your own while inheriting from one provided by pytest_django -- no?

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

3 participants