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

VSCode中Python测试重复显示及重复运行问题求助

VSCode中Python测试重复显示/运行的解决方法

近期更新后,VSCode里的Python测试会重复显示两次,调试模式下也会运行两次。即便新建空项目,按以下步骤操作后问题依旧存在:

复现步骤

  • 执行 python -m venv .venv 创建虚拟环境
  • 执行 source ./.venv/bin/activate 激活虚拟环境
  • 执行 pip install pytest==8.3.3 安装指定版本pytest
  • 在VSCode中选择虚拟环境对应的Python解释器
  • 执行 mkdir server server/tests 创建目录结构
  • 创建测试文件 server/tests/test_storage.py,内容如下:
# server/tests/test_storage.py
import pytest

class TestStorage:
    def test(self):
        pass
  • 在Settings.json中配置测试选项(已尝试不同目录配置):
{
  "python.testing.pytestArgs": [
    "tests"
  ],
  "python.testing.unittestEnabled": false,
  "python.testing.pytestEnabled": true
}

VSCode版本:1.94.0(Universal)

解决方法

  1. 修正pytest路径配置
    当前配置的pytestArgs为"tests",但测试文件实际在server/tests目录下。尝试将路径改为相对项目根目录的完整路径:
"python.testing.pytestArgs": [
  "server/tests"
]

也可以使用绝对路径,确保pytest只扫描一次测试文件。

  1. 回退Python插件版本
    近期VSCode Python插件更新可能引入bug,尝试回退到上一个稳定版本(例如v2024.8.0)后重新测试。

  2. 排查重复配置与插件
    检查工作区/用户设置中是否存在重复的测试配置,确保仅开启pytest测试框架;同时禁用其他可能干扰测试发现的第三方插件。

  3. 清除VSCode缓存
    关闭VSCode,删除对应系统的缓存目录后重启:

  • macOS: ~/Library/Application Support/Code/Cache、~/Library/Application Support/Code/CachedData
  • Windows: %APPDATA%\Code\Cache、%APPDATA%\Code\CachedData
  • Linux: ~/.config/Code/Cache、~/.config/Code/CachedData
  1. 校验pytest命名规范
    确保测试文件以test_开头、测试类以Test开头、测试方法以test_开头,避免pytest重复识别测试用例。

内容的提问来源于stack exchange,提问作者Semih Sezer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 14:47:11