Python Tests, Zero Conf, for VS Code
The Python tests of every folder of the workspace, in VS Code's native Testing view, the way
PyCharm does it: open the workspace, the tests are there, run them. No setting to write, and the
extension writes nothing into the projects (no .vscode/settings.json, no conftest.py, no
__init__.py).
Why it exists: Microsoft's Python extension looks for no test at all until
python.testing.pytestEnabled or unittestEnabled is turned on for each folder, and even then its
unittest discovery does not see a tests/ folder without __init__.py (since Python 3.11,
unittest discover no longer enters such a folder).
Install
Requirements: VS Code 1.109 or later, Node.js, and the code command on the PATH (the VS Code
installer adds it on Windows). The Python extension (ms-python.python) is recommended but not
required. The Python Debugger (ms-python.debugpy) is needed for Debug.
cd C:\claude_checkouts\python-tests-zero-config
npm install # once: fetches the build tools
npm run install-local # checks, builds, creates python-tests-zero-conf.vsix and installs it in VS Code
Then, in VS Code: Ctrl+Shift+P, Developer: Reload Window.
- Install an already built package:
code --install-extension python-tests-zero-conf.vsix --force.
- Update after a code change: run
npm run install-local again.
- Uninstall:
code --uninstall-extension ThongvanAlexis.python-tests-zero-conf.
Use
Nothing to do: at startup each folder of the workspace is examined, one after the other, and its
tests appear in the Testing view (flask icon), under a node named after the folder followed by
the framework (pytest or unittest).
| Action |
Where |
| Run a test, a class, a file, a folder, a whole project |
Testing view, ▷ button on the line |
| Run a test file or folder from the explorer |
right-click → Run Tests (VS Code's own entry) |
| Run the test or class under the cursor |
▷ button in the gutter, or Ctrl+; then C |
| Run the tests of the open file |
Ctrl+; then F |
| Debug (breakpoints) |
the same, with Debug Test / right-click → Debug Tests |
| Find the tests again |
↻ button of the Testing view, or Python Tests: Find Tests Again |
| See what the extension does |
Python Tests: Show Log |
- Results: one status per test, and the failure message (full traceback) placed on the line that
failed. The raw output of pytest or unittest is in Test Results.
- Automatic updates: when a test file, a
conftest.py, an __init__.py or the pytest
configuration changes, the folder is examined again 1.5 s later. Same when the folder's
interpreter changes, or when a folder is added to the workspace.
- A file that does not import stays in the tree, in red, with the import error. It does not stop
the other tests when the whole folder runs.
Which framework
For each folder:
python.testing.pytestEnabled or unittestEnabled set to true by the user: the extension
leaves that folder to the Python extension, so the tests are not listed twice.
- pytest when the folder says so: a
[tool.pytest.ini_options] or [tool.pytest] section in
pyproject.toml, pytest.ini, .pytest.ini, [tool:pytest] in setup.cfg, [pytest] in
tox.ini, or a conftest.py. pytest runs from the folder's root and reads its own configuration
(testpaths, addopts, deselections, plugins). When pytest is not installed in the interpreter,
a message says so (nothing is installed for you).
- Otherwise unittest, when
test*.py or *_test.py files hold TestCase classes. No
__init__.py needed: each test folder is imported from itself, with the project root on
sys.path (like right-click → Run in PyCharm).
- Otherwise nothing.
A file named like a test but without a TestCase class (a script) is never imported by
discovery: a script that does its work at import time does not run by itself.
Which interpreter
- The virtual environment at the folder's root, whatever its name (the folder that holds
pyvenv.cfg). When there are several: the one the Python extension selected if it is one of
them, else .venv, else venv, else the first in alphabetical order.
- Otherwise the interpreter the Python extension selected for the folder (global, conda, a venv
kept elsewhere).
- Otherwise
python from the PATH.
When the interpreter selected in the Python extension no longer exists (a moved project), a
warning names the missing path and the one used instead, with a Select Interpreter button.
Why the folder's venv comes before the Python extension's selection: for a folder nobody configured,
that extension answers with the global interpreter, which lacks the project's packages (checked: 0
tests for GitPane instead of 15,563).
Setting
Only one, to force a folder (rarely useful):
| Setting |
Default |
Purpose |
pythonTestsZeroConfig.framework |
auto |
auto, pytest, unittest, or off to leave the folder alone |
Limits
- No code coverage (Coverage profile).
- Other frameworks (nose2, doctest on its own): not supported.
- Running a whole large pytest project happens in a single pytest process, as on the command
line. The project's options (pytest-xdist's
-n in addopts, for example) apply.
- pytest discovery of a large project takes as long as
pytest --collect-only (8 s for the 15,563
tests of GitPane), and runs again each time a test file is saved.
- unittest: a test class imported from another module shows under the file that imports it, as
unittest finds it.
Develop
npm run check # type check + unit tests (vitest, with a real Python)
npm run test:integration # a separate VS Code on a workspace of test projects
npm run watch # rebuild on every change
F5 in VS Code opens a second window with the extension loaded from the sources.
npm run test:integration builds five small projects in .vscode-test\fixture workspace\ (a pytest
one, a unittest one without __init__.py, a "moved" project, a folder handed to the Python
extension, a folder without tests), opens the installed VS Code on them with a throwaway profile
into which it copies the Python, Python Environments and Python Debugger extensions, then checks
discovery, runs, debugging, the explorer menu (right-clicks sent through the DevTools protocol: the
machine's mouse pointer does not move), automatic updates and that nothing is written in the
projects. Screenshots go to .vscode-test\screenshots\.
The acceptance checks on real projects, opened as they are (nothing is written to the usual VS Code
profile):
$env:PTZC_ACCEPTANCE_FOLDERS = 'C:\checkouts3\gem_website;C:\checkouts3\factory;C:\claude_checkouts\my_linux_setip;C:\claude_checkouts\GitPane'
npm run test:integration
$env:PTZC_ONLY = 'debug' runs only the checks whose name contains that text.
$env:PTZC_TEST_PYTHON picks the Python used by the tests (default python, which must have
pytest).
Documentation (in French)
docs/01-vscode-api.md: what VS Code and the Python extensions allow, and what was checked
docs/02-architecture.md: how the code is organised