If VS Code’s Python Test Explorer finds no tests, reports import errors, or appears to search the wrong folder on macOS, check the Python testing working directory and framework arguments first. Set python.testing.cwd to the directory your test command expects, then refresh discovery and inspect the Python output log. For unittest, also check its separate -s start-directory argument; changing cwd does not replace that setting.
Confirm which directory VS Code is using
Open the Test Explorer and run Test: Refresh Tests from the Command Palette (⇧⌘P). If discovery fails, open View > Output, choose Python from the output channel menu, and read the latest discovery entry. The Python extension sends discovery errors to this log, which can help distinguish a path problem from a missing framework or syntax error. VS Code documents these discovery and logging steps.
Check which folder is open in the VS Code window. In a single-folder workspace, the workspace is usually the project root. If you opened a repository’s parent folder, or a multi-root workspace, relative paths may resolve from a different root than the one you assumed. A terminal’s current directory is not enough to establish the directory the extension used; look at the discovery output and the workspace settings. In a multi-root workspace, VS Code allows folder-specific settings, and ${workspaceFolder} refers to the folder associated with the setting. VS Code’s workspace guide explains multi-root folders, and its variable reference covers folder-scoped paths.
Set the Python test working directory
For example, if you opened the repository root but your test command should run from its backend subfolder, open workspace settings on Mac with ⇧⌘P and select Preferences: Open Workspace Settings (JSON). Add or merge this key into the existing JSON object:
{
"python.testing.cwd": "${workspaceFolder}/backend"
}
Use the actual directory for your project. If tests should run at the workspace root, you can omit this setting or set it to ${workspaceFolder}. Avoid a bare relative value such as backend if you are unsure which root it is relative to; the explicit workspace variable makes the intended base clearer. Workspace settings override user settings, so inspect both scopes if your change appears ineffective. VS Code stores single-folder workspace settings in .vscode/settings.json; use the Settings editor’s Workspace tab to review the active value. Workspace settings and precedence are described in VS Code’s settings documentation.
The Python extension’s cwd setting is optional. Its documentation says that setting it also adjusts pytest’s --rootdir; rootdir is pytest’s project-root configuration, so don’t treat it as interchangeable with every framework’s test search argument. The Python testing settings reference identifies this pytest behavior.
Check framework-specific discovery arguments
For pytest, review python.testing.pytestArgs for a path that points somewhere unexpected. A path such as tests tells pytest what to collect, while python.testing.cwd sets the working-directory context. Keep only the arguments your project needs, and make the path consistent with that context. After editing, run Test: Refresh Tests again. The extension discovers pytest files matching its conventions under the current folder and subfolders; discovery can still fail for reasons unrelated to cwd, including an unavailable pytest installation or test-file errors. The official testing guide lists discovery behavior and common failure reporting.
For unittest, inspect python.testing.unittestArgs separately. Its default arguments include -s ., where -s selects the start directory. If tests live under backend/tests and cwd is the repository root, configure the start directory accordingly, for example:
{
"python.testing.cwd": "${workspaceFolder}",
"python.testing.unittestEnabled": true,
"python.testing.pytestEnabled": false,
"python.testing.unittestArgs": ["-v", "-s", "backend/tests", "-p", "*test*.py"]
}
This is an example for a project using unittest; don’t copy it unchanged if your framework or file naming differs. unittest also requires discovered test files to be importable modules or packages. If the log shows importability errors in a test subfolder, check the package structure; VS Code notes that an __init__.py file can make such folders importable. The official guide describes unittest’s -s and -p options.
Refresh and isolate remaining failures
After changing settings, run Test: Refresh Tests again. If VS Code still uses an unexpected environment, run Python: Select Interpreter and choose the environment that has your test framework installed; the selected interpreter is used for Python features including testing. VS Code’s Python environments guide explains interpreter selection.
If the output still shows the wrong path, compare the logged working directory with the workspace root and the configured test arguments. If the path is correct but discovery fails, follow the actual error instead: check framework installation, import errors, syntax errors, and test filename patterns. A cwd adjustment cannot fix those independent problems. If a correctly configured project still logs an inconsistent path or ignores the setting, capture the Python output and relevant settings when reporting the issue; there is no universal VS Code command that forces every test framework to use a different directory.
Sources
Python testing in Visual Studio Code — Microsoft
User and workspace settings — Microsoft
Multi-root Workspaces — Microsoft
Variables Reference — Microsoft
Python environments in VS Code — Microsoft