python-3.xvisual-studio-codepytestpytest-cov

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.


Solution

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