Coverage data only changes for tests.py, not for application code when running Django app tests
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
- 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>. - Generate the report and repeat the tst execution to another app.
- Observe that only the tests.py file for that app shows coverage changes.
- Run tests for another app in the same project.
- Notice that the coverage report only changes for the new tests.py file, with no changes in the rest of the application code.
- 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.
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.
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='.'orpytest --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.
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.