You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode无法检测tests目录中的pytest测试用例,但终端运行pytest正常的问题排查求助

VSCode无法检测tests目录中的pytest测试用例,但终端运行pytest正常的问题排查求助

我正在做一个Python项目,用pytest来写测试,目前碰到个头疼的问题:终端里跑pytest完全正常,但VSCode的Testing面板一直是空的,还无限加载,根本检测不到tests目录里的测试用例。先跟大家说下我的项目情况和配置:

项目结构

.gitignore
.pytest_cache/
    .gitignore
    CACHEDIR.TAG
    README.md
    v/
        cache/
            nodeids
            stepwise
.python-version
.vscode/
    settings.json
hello.py
pyproject.toml
README.md
tests/
    __init__.py
    __pycache__/
    test_test.py
uv.lock

关键配置细节

  1. VSCode设置
    我的.vscode/settings.json里已经配置了pytest相关的选项:
{
    "python.testing.pytestArgs": [
        "tests"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true
}
  1. 测试文件内容
    tests/test_test.py里是个很简单的测试用例,确保逻辑没问题:
import pytest

def test_example():
    assert True  # 后续会替换成实际测试逻辑
  1. 项目依赖配置
    pyproject.toml里已经把pytest列为依赖了:
[project]
name = "compass-extractor"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
    "pytest>=8.3.5",
]
  1. 环境管理
    我用uv来管理项目依赖,在终端里直接跑pytest命令,测试能正常执行,完全没问题。

遇到的问题

VSCode的Testing面板一直是空的,而且一直在加载,根本检测不到我写的测试用例,但是终端跑pytest一切正常。

我已经尝试过的排查步骤

  • 确认pytest已经安装在当前项目的虚拟环境里了
  • 检查了python.testing.pytestEnabled确实设为true
  • 确认python.testing.pytestArgs指向的是正确的tests目录

(附VSCode Testing面板状态:面板无任何测试用例显示,处于持续加载状态)

想请教下各位,这种情况可能是什么原因导致的?有没有什么解决办法?

备注:内容来源于stack exchange,提问作者MarcellGranat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:13:04