question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Debugger doesn't stop at breakpoints with pytest if pytest-cov is used

See original GitHub issue

Environment data

VS Code version: 1.19.3 Python Extension version: 2018.1.0 Python Version: 3.6.2 OS and version: Windows 10 latest update

Actual behavior

My initial setup has this setup.cfg file:

[tool:pytest]
addopts = --cov=mymodulename

This adds automatically a “–cov” to every “pytest” call, but prevents VSCode to stop at breakpoints. If I remove the file (or at least the --cov), I get back my breakpoints stop. I changed my setup to put that in my Travis file instead, but I feel like this should work 😕

Expected behavior

I should be able to have this file (and coverage) and debug at the same time.

Steps to reproduce:

  • Install pytest and pytest-cov
  • Create a setup.cfg as describe before
  • Create even a simple test with a breakpoint inside

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:46
  • Comments:30 (2 by maintainers)

github_iconTop GitHub Comments

26reactions
DonJayamannecommented, Feb 22, 2018

@lmazuel apologies for the delay. Looks like its not possible to debug the code with coverage enabled (http://pytest-cov.readthedocs.io/en/latest/debuggers.html). I guess we’ll have to disable ‘coverage’ on the fly when debugging (using the flag "--no-cov").

22reactions
AlexanderWells-diamondcommented, Sep 24, 2021

I use an alternative to @dferrante 's solution above, which seems to be seamless inside the VSCode IDE for pytest tests:

In setup.cfg I have:

[tool:pytest]
addopts =  --cov=<module name> --cov-report term --cov-report xml:cov.xml  <other arguments here>

And then in launch.json I define a new task:

 {
    "name": "Debug Unit Test",
    "type": "python",
    "request": "launch",
    "justMyCode": false,
    "program": "${file}",
    "purpose": ["debug-test"],
    "console": "integratedTerminal",
    "env": {
        "PYTEST_ADDOPTS": "--no-cov"
    },
}

When you press the “Debug Test” button in the testing panel or by right-clicking on a test itself, VSCode executes this task which just turns off all coverage for that run. No need to switch between configurations or altering config arguments on the fly.

Note this may only work for fairly new versions of VSCode - I’m on 1.60.

Read more comments on GitHub >

github_iconTop Results From Across the Web

debugging of py.test does not stop on breakpoints if coverage ...
Workaround that worked for me to ignore pytest coverage settings in pyproject.toml is to edit the pytest configuration (in edit configurations) and add...
Read more >
Unable to debug in pycharm with pytest - Stack Overflow
All the test suite is running ok in "Debug mode" but it doesn't stop on breakpoints. Debug Mode. I also have py.test as...
Read more >
How to fix the issue: Debugging pytest is not working with ...
One common reason why PyCharm debugger is not able to stop at breakpoints is that something else is using the tracing api (sys.settrace)....
Read more >
Debuggers and PyCharm — pytest-cov 4.0.0 documentation
This manifests itself in behaviour that PyCharm might not hit a breakpoint no matter what the user does, or encountering an error like...
Read more >
Testing Python in Visual Studio Code
pytestArgs : Looks for any Python ( .py ) file whose name begins with "test_" ... VS Code doesn't stop at breakpoints while...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found