Repository navigation
Utilize hatch to run Django tests? #1826
Replies: 1 comment
|
There is one premise worth correcting first: Django's built-in test runner does not use pytest underneath. manage.py test uses Django's test runner (based on unittest) unless you explicitly replace it. pytest-django is the integration that lets pytest set up Django and run Django tests. Current hatch test is intentionally a pytest-oriented command: Hatch's internal hatch-test environment installs pytest and its generated run script invokes pytest. Adding native Django-runner support there would therefore introduce a second runner model into a command whose configuration, coverage integration, plugins, parallelism, and retries are pytest-oriented. You don't need to wrap each test in a pytest function, though. If you want hatch test, use pytest-django and put DJANGO_SETTINGS_MODULE in the test environment (or configure it through pytest-django). Hatch's test matrix can vary settings/configuration. If you want Django's native runner, make it an ordinary Hatch environment script such as python tests/manage.py test and run it with hatch run. Hatch still creates and manages the isolated environment; Django owns test discovery/execution. For multiple settings, a Hatch matrix variable can feed an environment variable into each environment. I would use that generic environment/script layer rather than add a Django-specific hook to hatch test. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have a repository that currently utilizes the Django test runner, but I was curious if I could transition to using the hatch runner
hatch testinstead. Django usespytestunder the hood, so in theory it should be possible.To me it seems like there's two "django things" that aren't easily pushed into hatch's workflow
manage.pyfile. This file is pretty simple, it sets up the environment variableDJANGO_SETTINGS_MODULEand then calls the functiondjango.core.management.execute_from_commandline.manage.pyprocesses sys args. It adds a whole slew command line arguments to pytest. The most important arg is--settings <NAME>which allows for testing multiple unique/discrete Django configurations.While I could just wrap all of my Django tests in a classic
pytestfunction and directly executesubprocess.run(["python", "manage.py", "test", "..."], that doesn't truly run any of the actual tests within Hatch's test runner, so it's a bit moot.Given that Django is one of the biggest web frameworks, is supporting the Django test runner worthwhile to incorporate into Hatch natively?
And if so, this would likely require Hatch to have a light wrapper to "hook into" Django-Land.
The currently existing alternative is either to require all Django users to...
pytest-django, which is a bit janky.All reactions