Running pytest-cov along with pytest in VS Code
Asked Answered
T

2

13

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.

Telson answered 4/10, 2021 at 22:57 Comment(0)
D
17

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
        }
    ]
}
Deannadeanne answered 18/3, 2022 at 10:37 Comment(2)
I used this launch.json file and I get "Could not load unit test config from launch.json as it is missing a field"Marshallmarshallese
@Marshallmarshallese You need to add a "version": "0.2.0" field in the launch.json file in order for VS Code to load it successfully.Revegetate
M
1

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/"
    ]
}
Marshallmarshallese answered 10/12, 2022 at 0:18 Comment(1)
This was what I needed, thank you!Amendment

© 2022 - 2024 — McMap. All rights reserved.