Skip to content

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

@paduszyk

Description

@paduszyk

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions