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.
Running pytest-cov along with pytest in VS Code
Asked Answered
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
}
]
}
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 I went for a slight modification from @ncw's answer to resolve 2 issues:
- Debugger hanging
- Error :
"Could not load unit test config from launch.json as it is missing a field"
. The field wasversion
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/"
]
}
This was what I needed, thank you! –
Amendment
© 2022 - 2024 — McMap. All rights reserved.