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

Pytest如何配置识别自定义前缀测试文件(VS Code环境)

VS Code Python Test Explorer 自定义测试文件识别规则配置方法

1. 修改Python Test Explorer扩展配置(核心配置)

这是适配该扩展的直接配置方式:

  • 打开VS Code设置界面:按快捷键Ctrl+,(Windows/Linux系统)或Cmd+,(Mac系统)
  • 搜索配置项 Python Test Explorer: Test File Pattern
  • 将默认的匹配规则**/test_*.py修改为**/{test_,moduletest_}*.py,该规则会同时匹配test_和moduletest_前缀的Python文件
  • 如果你习惯直接编辑JSON格式的配置,可以在settings.json中添加如下键值对:
"pythonTestExplorer.testFilePattern": "**/{test_,moduletest_}*.py"

修改完成后刷新测试列表即可识别moduletest_前缀的测试文件。

2. 适配原生Python测试功能(可选)

如果你同时使用VS Code原生的Python测试面板,需要根据你使用的测试框架补充对应配置,在项目根目录的.vscode/settings.json中添加对应内容:

  • 如果你使用pytest框架:
{
  "python.testing.pytestEnabled": true,
  "python.testing.pytestArgs": [
    "--glob=test_*.py",
    "--glob=moduletest_*.py"
  ]
}
  • 如果你使用unittest框架:
{
  "python.testing.unittestEnabled": true,
  "python.testing.unittestArgs": [
    "-v",
    "-p",
    "test_*.py",
    "-p",
    "moduletest_*.py"
  ]
}

保存配置后重载VS Code窗口即可生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 08:15:04