Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run pytest-cov with pytest in VS Code?

I'm trying to make it so that when I run my test in test explorer it will automatically generate a cov.xml file at the same time in the project folder. Ive tried adding in the arguments to the pytest argument field on VS Code but it does not seem to make any changes to the way the the test explorer runs the tests/pytest. I may be missing something or this just may not be something that is possible.

like image 758
Sam T Avatar asked Jan 01 '26 13:01

Sam T


2 Answers

First, pytest and pytest-cov must both be installed via pip:

$ pip install pytest
$ pip install pytest-cov

In your local repository settings, add the following configuration to the .vscode/settings.json file:

{
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.testing.pytestArgs": [
        "-v",
        "--cov=myproj/",
        "--cov-report=xml",
        "--pdb",
        "tests/"
    ]
}

Now you can run the tests with the builtin test explorer: Testing > Run Tests. The xml file will be generated and is located in your working directory. See also the pytest-cov documentation on reporting and the vscode documentation for pytest configuration. As stated in the vscode docs, I would suggest also adding the launch configuration below to .vscode/launch.json in order not to break debugging by using pytest-cov:

{
    "configurations": [
        {
            "name": "Python: Debug Tests",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "purpose": ["debug-test"],
            "console": "integratedTerminal",
            "env": {
                "PYTEST_ADDOPTS": "--no-cov"
            },
            "justMyCode": false
        }
    ]
}
like image 65
ncw Avatar answered Jan 03 '26 07:01

ncw


I went for a slight modification from @ncw's answer to resolve 2 issues:

  1. Debugger hanging
  2. Error : "Could not load unit test config from launch.json as it is missing a field". The field was version

launch.json:


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

settings.json

{
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.testing.pytestArgs": [
        "-v",
        "--cov",
        "--cov-report=xml",
        "python/tests/"
    ]
}
like image 38
MattG Avatar answered Jan 03 '26 08:01

MattG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!