coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

Coverage data only changes for tests.py, not for application code when running Django app tests

Open leofernandesmo opened this issue 5 months ago • 3 comments

Describe the bug I'm experiencing an issue when trying to measure code coverage for my Django project using Coverage.py. When I run the tests for a specific app, the coverage report only changes for the tests.py file of that app. The coverage for the remaining application code remains unchanged. If I run the tests.py from another app in the same project, the coverage report is exactly the same, except for the coverage of the newly executed tests.py file.

It seems that the coverage data is being masked or affected by Django's internal task execution, and the actual application code is not being measured as expected.

To Reproduce 0. Run the command: coverage run --rcfile=".coveragerc" manage.py runserver

  1. Run tests for one Django app and generate a coverage report: Eg.: docker exec -ti <container_name> coverage run --source='.' manage.py test <app_name>.
  2. Generate the report and repeat the tst execution to another app.
  3. Observe that only the tests.py file for that app shows coverage changes.
  4. Run tests for another app in the same project.
  5. Notice that the coverage report only changes for the new tests.py file, with no changes in the rest of the application code.
  6. I expect the coverage report to reflect the code executed in the application modules, not just the tests.py files.

A clear and concise description of what you expected to happen.

Additional context It appears that Coverage.py is not tracking the execution of the actual application code during Django test runs, possibly due to the way Django manages test execution or subprocesses. Any guidance or workaround would be appreciated.

leofernandesmo avatar Jul 10 '25 18:07 leofernandesmo

I believe this is due to runserver using subprocesses. I thought there were existing issues with the same problem, but I can't seem to find them.

nedbat avatar Jul 10 '25 20:07 nedbat

I looked through both open and closed issues to see if there was anything similar. Although I’m working on a closed-source project, which makes it challenging to share the code, I can try to provide more details.

I’ve tried several approaches:

  • Calculating coverage by starting the Django server with coverage run (with and without the --noreload option). I also added code to save the coverage data in case the server is stopped.
  • Running the Python script in the root folder using coverage run --source='.' or pytest --cov=.
  • Moving the script into a Django command and running it with manage.py.

IMPORTANT: This script contains tests generated by a fuzzer and uses the requests library to test the API REST.

leofernandesmo avatar Jul 11 '25 08:07 leofernandesmo

Can you provide a way for me to reproduce the issue? One possibility is to try the new patch = subprocess feature to see if it helps.

nedbat avatar Aug 15 '25 11:08 nedbat