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

VSCode Pytest测试资源管理器可识别测试但路径错误无法执行

问题

VSCode测试资源管理器能正常识别所有测试,但运行时会调用错误路径。当前使用Poetry虚拟环境,其路径与测试文件路径不一致。

测试适配器日志

INFO pytest at 'myproject': Running pytest with arguments: --rootdir=/Users/me/Desktop/myproject , Users/me/Desktop/project/tests/db/queries/my_test.py::test_fn

INFO pytest at 'myproject': Running /Users/me/Library/Caches/pypoetry/virtualenvs/myproject-api-NEejG4ix-py3.9/bin/pytest as an executable

CRIT pytest at 'myproject': Execution of the test Users/me/Desktop/project/tests/db/queries/my_test.py::test_fn" failed: Error: Process exited with code 4: ERROR: file or directory not found: 

当前VSCode设置

{
  "python.testing.pytestPath": "/Users/me/Library/Caches/pypoetry/virtualenvs/myproject-api-NEejG4ix-py3.9/bin/pytest", 
  "python.testing.unittestEnabled": false,
  "python.testing.pytestEnabled": true,
  "python.testing.pytestArgs": ["api/tests"]
}

使用的VSCode扩展:littlefoxteam.vscode-python-test-adapter

询问:是否存在其他设置可配置测试资源管理器运行时的路径?


解决方案

针对littlefoxteam.vscode-python-test-adapter扩展,可尝试以下配置调整:

  • 指定测试执行工作目录:在VSCode设置中添加python.testing.cwd,将其设置为项目根目录(即/Users/me/Desktop/myproject),确保pytest从正确的目录启动:

    "python.testing.cwd": "/Users/me/Desktop/myproject"
    
  • 修正pytest参数中的测试路径:当前pytestArgs设置的["api/tests"]可能与实际测试路径不匹配,可调整为相对项目根目录的正确路径,或直接使用绝对路径:

    "python.testing.pytestArgs": ["/Users/me/Desktop/project/tests"]
    
  • 配置扩展专属的测试目录:该适配器支持pythonTestAdapter.testDirectory设置,直接指定测试文件所在的根目录:

    "pythonTestAdapter.testDirectory": "/Users/me/Desktop/project/tests"
    
  • 确认虚拟环境关联正确性:检查VSCode左下角的Python解释器选择器,确保当前使用的是Poetry虚拟环境中的Python解释器(路径为/Users/me/Library/Caches/pypoetry/virtualenvs/myproject-api-NEejG4ix-py3.9/bin/python)


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 04:33:37