VS Code not finding pytest tests

EDIT: I downgraded to Pytest 4.0.1 after reading issue 3911 and Test Discovery now works.


Me too. When I blow away .pytest_cache and rerun Python: Discover Unit Tests, I see that the freshly generated .pytest_cache/v/cache/nodeids contains all the tests, but I still get the dialog complaining about No tests discovered.

  • Python 3.7.2
  • macOS 10.13.6
  • VS Code 1.30.2
  • Python Extension 2018.12.1
  • Pytest 4.1.0

.vscode/settings.json:

{
    "python.linting.enabled": false,
    "python.unitTest.unittestEnabled": false,
    "python.unitTest.nosetestsEnabled": false,
    "python.unitTest.pyTestEnabled": true,
    "python.pythonPath": "venv3/bin/python"
}

Tests are in a top-level subdirectory called test. Running pytest manually works.


Another thing to check, if vscode fails to discover the tests, is to make sure it doesn't do so because of the coverage module being enabled. In my case the test cases were being discovered correctly but the discovery eventually kept failing due to low test coverage, as described here. So first, make sure that the tests could actually be collected by pytest as suggested here:

pytest --collect-only

and then make sure you're not forcing coverage check (e.g. in setup.cfg) with e.g.

addopts= --cov <path> -ra

If anyone comes across this post-2020, this issue in the vscode-python repo saved my life. Basically, just do the following:

  1. Uninstall the Python extension
  2. Delete the file that contains the extension from your ~/.vscode folder (mine looked like ms-python.python-[YEAR].[MONTH].[VERSION])
  3. Reinstall the extension

Worked like a charm.