Tools and workflows

How to run pytest in VS Code and fix test discovery

Configure pytest with your project interpreter and use --collect-only to tell missing tests, import errors, and failed assertions apart.

In VS Code, select your project's Python interpreter. Run Python: Configure Tests, choose pytest, and open the Testing view. If the tests do not appear, run python -m pytest --collect-only from the same project folder. The terminal output can show whether pytest found no matching files or stopped while importing a test.

Check the interpreter and folder pytest sees

The project folder should contain or intentionally point to the settings and files pytest uses. Select the same environment from which a colleague can run the documented command. Opening a parent or nested folder may change which configuration is found and which files are visible to discovery.

Create a diagnostic terminal with the same interpreter as the editor. Print its path and then run collection. If pytest cannot import a dependency, fix the selected environment or workspace installation. If it reports zero tests, compare the chosen test directory and naming convention with project settings. Those outcomes deserve different changes.

Run one meaningful test

If you already know the test file and function, select the test by its full node identifier. For example, python -m pytest tests/test_summary.py::test_empty_input runs that function in the specified file when the node exists. This is useful when one behavior needs focused debugging.

A failure after collection usually means a test was identified and one of its setup steps or assertions failed. Start with the first specific error. Add temporary diagnostic output or debug the case while verifying the exact fixture it receives. A person can often explain the issue faster from a single named test than from a long scroll of equivalent failures.

Read the result as evidence

Pytest returns exit code 5 when it collects zero tests. Check the names and count after renaming tests or moving a folder. A green check for an earlier run says nothing about a new test that the runner never found.

A collection error can arise before a test runs. It might be a missing import, a syntax error, or an unsupported plugin. An assertion failure comes after the runner began exercising a case. Separating these stages prevents you from editing a correct test body to solve a setup problem.

Find the smallest difference between editor and shell

Suppose the terminal finds 12 tests but VS Code lists 8. Capture the exact collection command, working directory, and selected interpreter. Check whether the editor has a configured test path that includes only one folder. Compare by reproducing that narrower collection in the terminal. A targeted failure is easier to fix than deleting all workspace settings.

If the editor locates the tests but a file cannot be imported, read the first collection error. Pytest may be installed in an environment that cannot import your project. Check which interpreter VS Code selected and follow the project's development installation instructions before adding extra search paths.

Keep a visible record when a test has never run

A new test may show an empty or unknown state in the Testing view until you run it. Distinguish this from a passing result. In a contributor note, report the command, number collected, and number actually run if you used filters. A run that selected one case is evidence for that case. A run that never collected it needs an explanation before anyone treats the result as verification.

Share the repeatable command

Write a terminal command that someone can run without your editor profile. State the working directory, Python version, and required development dependencies. Add a short note explaining how to run one test. Let the editor surface the same results as a convenient interface.

When the editor and terminal disagree, compare the interpreter, configuration file, working directory, and selection filters. Reproduce the specific discrepancy with one test before changing broad settings. Once both paths exercise the same case, a failure report is easier to share and fix.

Sources and further reading

Related reading

Browse Tools and workflows · Explore project tutorials

Search PythonProjects.org