pytest-django test collection behavior is inconsistent

21 Views Asked by At

I have a Django project that is set up with pytest-django and everything seemed to be running just fine. However, I recently added tests to a test file and noticed that they weren't being picked up by the pytest command. Here are a few of the things I've tried:

  • Adding @pytest.mark.django_db to the new tests causes them to be correctly collected, but this decorator should only be used when database access is needed, and these tests don't need database access.
  • Moving the unmarked tests so they come after the marked test in the same file causes the 3 unmarked tests to be picked up by the test runner, but now the marked test fails to be collected.
  • Running pytest on that specific file causes all tests to be collected (and pass) with or without the decorators on the tests that don't need them.

I've been unable to identify anything wrong with my pytest configuration, though I did note that we're also using the Coverage tool with it to measure code coverage of the tests. The pytest.ini file is very simple:

[pytest]
env_files = .env
addopts = --cov . --cov-report html --cov-report xml --cov-config "coverage/.coveragerc" --disable-warnings
DJANGO_SETTINGS_MODULE = config.settings.test

I've also noticed that this issue doesn't only apply to this one file; there are several other files with unmarked tests that are not getting collected by pytest, though this behavior seems inconsistent.

How do I get pytest to collect all the tests in a file regardless of whether I run pytest on the one file or on all files, regardless of whether the tests are unnecessarily decorated, and also regardless of the order of the tests inside of the files?

0

There are 0 best solutions below