Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode Test Debugger not stopping at breakpoints when using coverage

I've started to use pytest-cov for coverage reporting. Using VSCode.

This is how I set up my pytest.ini file so that every time I run tests from the VSCode test explorer, the coverage report gets updated:

[pytest]
addopts = "--cov=src/ --cov-report=lcov:lcov.info --cov-report=term"
env = 
    TESTING=true
    ENV=local

But I also want to be able to debug my tests and stop on breakpoints. As the VSCode docs say

Note If you have the pytest-cov coverage module installed, VS Code doesn't stop at breakpoints while debugging because pytest-cov is using the same technique to access the source code being run. To prevent this behavior, include --no-cov in pytestArgs when debugging tests, for example by adding "env": {"PYTEST_ADDOPTS": "--no-cov"} to your debug configuration. (See Debug Tests above about how to set up that launch configuration.) (For more information, see Debuggers and PyCharm in the pytest-cov documentation.)

So my configuration for debugging tests in launch.json is like this:

        {
            "name": "Python: Debug Tests",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "purpose": [
                "debug-test"
            ],
            "console": "integratedTerminal",
            "justMyCode": true,
            // "justMyCode": false,
            "env": {
                "ON_HEROKU": "0",
                "PYTEST_ADDOPTS": "--no-cov",
                "ECHO_SQL_QUERIES": "1"
            },
        },

Still, when debugging tests it doesn't stop on breakpoints even tho I set the PYTEST_ADDOPTS env var there. The only way to make it work is by commenting the addopts line in pytest.ini

[pytest]
# addopts = "--cov=src/ --cov-report=lcov:lcov.info --cov-report=term"
env = 
    TESTING=true
    ENV=local

How can I make it behave the way I want without having to comment and uncomment that line in pytest.ini?

like image 825
Xoel Avatar asked Jul 26 '26 01:07

Xoel


1 Answers

I went trough something similar recently and found this page when searching the error.

Unlike you, I didn't want to run coverage tests in VS Code, so I could simply disable it in settings.json passing the --no-cov argument:

"python.testing.pytestArgs": [
  "--import-mode=importlib",
  "--no-cov"
]

This solved the debugger not stopping at breakpoints problem for me.

like image 182
Julio Batista Silva Avatar answered Jul 28 '26 14:07

Julio Batista Silva