Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Django Test Runs with Coverage #24199

Open
eleanorjboyd opened this issue Sep 26, 2024 · 4 comments
Open

Support Django Test Runs with Coverage #24199

eleanorjboyd opened this issue Sep 26, 2024 · 4 comments
Assignees
Labels
area-testing bug Issue identified by VS Code Team member as probable bug needs PR Ready to be worked on

Comments

@eleanorjboyd
Copy link
Member

make sure Coverage works while running Django and that the scenario is supported.

originally raised in #22827 (comment)

@slank
Copy link

slank commented Feb 16, 2025

The problem occurs because coverage is run in-process using coverage.start() (here), but the test runs under a subprocess and is unobservable to coverage.

Here are some approaches to fixing it.


Keep coverage in-process. Also run manage.py in-process.

Three variations on this:

  1. Use exec() to run manage.py in the same interpreter. This has a bad security smell but it's hard to say if there's really any difference from ...
  2. Import manage.py and run main() (here). Because manage.py is hard-coded to read its arguments from sys.argv, this approach would require manipulation of that variable.
  3. Do the stuff manage.py does. Something like this:
from django.core.management import execute_from_command_line
execute_from_command_line(["/path/to/manage.py", "test", ...])

Don't run coverage in-process. Call manage.py as designed

In this case, we'd simply replace [sys.executable] (here) with ["coverage", "run"]. The test execution code would need to be cleaned up.


I think all of these have their plusses and minuses. This is my first time here and I'm just working on a weekend project so I don't know the backstory on the current implementation. I like the execute_from_command_line() approach for simplicity.

@mcobalchinisoftfocus
Copy link

I read the documentation about testing and it doesn't mention that coverage in Django tests is not yet supported, so I was lost in the dark until I found this issue right here, I think it would be nice if this information was added as a disclaimer

@eleanorjboyd
Copy link
Member Author

great point @mcobalchinisoftfocus! @cwebster-99, could we include this in our docs update?

@danila-grobov
Copy link

Hi,

@eleanorjboyd Could you take a look at my PR #24927? I think I've fixed the issue.

karthiknadig pushed a commit that referenced this issue Apr 3, 2025
#24199

Co-authored-by: Danila Grobov (s4642g) <[email protected]>
karthiknadig pushed a commit that referenced this issue Apr 7, 2025
Related to this issue:
#24199

@mcobalchinisoftfocus Discovered an issue with older django versions,
which didn't have the main function in the manage.py


#24927 (comment)

I've fixed this issue by executing the code in manage.py with __name__
set to __main__ instead of relying on main function being there.

I've also adjusted the test, so that it would cover this case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-testing bug Issue identified by VS Code Team member as probable bug needs PR Ready to be worked on
Projects
None yet
Development

No branches or pull requests

4 participants