先确认底层命令能跑通,VSCode不执行测试只调用本地命令;若npx jest或pytest --collect-only无法列出测试项,则说明测试框架未正确安装或配置路径未覆盖测试文件。

VSCode里“Run All Tests”按钮没反应?先确认底层命令能跑通
VSCode不执行测试,它只调用你本地的测试命令。点“Run All Tests”没反应,90%是因为终端里连npx jest或pytest都跑不通。
- 在VSCode集成终端中执行
npx jest --listTests(Jest)或pytest --collect-only(Python),必须能列出测试项;报command not found就说明没装对——别用npm install -g jest,必须npm install --save-dev jest - 用pnpm?改用
pnpx jest --listTests;用yarn?用yarn jest --listTests - 如果命令能跑但没列测试,检查
jest.config.js里的testMatch或pyproject.toml里的testpaths是否覆盖了你的测试文件路径
批量运行时测试顺序乱、断点不生效?关掉并行执行
Jest/Vitest/pytest默认多进程并行跑测试,VSCode调试器只能attach到主进程。不关并行,断点基本无效,批量运行结果也可能错乱。
- Jest:必须加
--runInBand参数,推荐写进.vscode/launch.json的args字段,别依赖插件自动加 - Vitest:必须加
--no-file-parallelism,否则it()里的断点大概率跳过 - pytest:加
-n 0(禁用pytest-xdist)或直接删掉pytest.ini里的addopts = -n auto
测试文件被忽略?别靠默认规则,显式配testMatch或python_files
VSCode测试UI不会猜你把测试放哪——它严格按配置文件扫描。默认规则不匹配你的命名/路径,就等于没测试。
- Jest:
testMatch: ["**/*.spec.{js,ts,tsx}", "**/tests/**/*.{test,spec}.{js,ts,tsx}"],如果你用.unit.tsx后缀,必须加"**/*.unit.{ts,tsx}" - Vitest:
testMatch: ['src/__tests__/**/*.test.ts'],路径里用/或\都行,Vitest内部会normalize - Python pytest:
pyproject.toml里写[tool.pytest.ini_options]→testpaths = ["tests", "src"]+python_files = ["test_*.py", "*_test.py"]
Test Explorer里测试图标不亮、右键没菜单?检查适配器加载状态
图标不亮不是插件没装,是测试适配器根本没注册成功。VSCode的Testing API必须手动启用,且适配器扩展不能冲突。
- 按
Ctrl+Shift+P→ 输入Testing: Enable Testing回车,这是前提 - JavaScript项目:只启用
kavod-io.vscode-jest-test-adapter,禁用orta.vscode-jest——两者同时开必冲突 - Python项目:确保
ms-python.python和ms-python.vscode-pylance已启用,再装ms-python.python自带的测试支持(不用额外装Python Test Explorer) - 重启VSCode后,打开输出面板 → 切换到
Test Explorer标签页,看有没有Error: Cannot find module 'xxx'类报错


















