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
}
]
}