VSCode Test Debugger not stopping at breakpoints when using coverage

55 Views Asked by At

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?

1

There are 1 best solutions below

0
MingJie-MSFT On

You could try to use the following codes in your launch.json:

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

pytest-debug.ini is a separate configuration file for debugging. This file should be similar to your pytest.ini file, but without the addopts line:

[pytest]
env = 
    TESTING=true
    ENV=local

This way, when you're debugging your tests, pytest will use the pytest-debug.ini configuration file.