VSCode 未检测到我的任何 pytest 测试

Sco*_*ott 7 pytest visual-studio-code

我试图让我的单元测试显示在 VSCode 的测试资源管理器中,但它似乎不起作用。测试发现不会因输出中出现任何错误而失败,但也不会显示我的任何测试。

这是我正在使用的存储库,您可以在那里看到文件结构。

这可能与我使用诗歌来管理我的虚拟环境有任何关系,也可能没有任何关系,尽管我确信 python 解释器设置正确。pytest我可以通过运行或在我的基本目录中手动让我的测试完美运行poetry run pytest__init__.py我的测试目录中确实有一个空目录。另外值得注意的是,我在 WSL 2 上的 Ubuntu 中运行。

在此输入图像描述

我通过以下过程设置了这个环境:

  • 在 Windows 上安装 WSL2
  • 在 Windows 上安装 VSCode
  • 安装“远程 - WSL”VSCode 扩展
  • 在 VSCode 中打开一个新的 WSL 窗口
  • 使用本指南在 Ubuntu 中安装 Anaconda
  • 安装 VSCode Python 扩展
  • 将 VSCode python 解释器设置为 anaconda 安装,并打开一个新终端
  • pip install poetry
  • 转到我的项目根文件夹
  • poetry install
  • 将VSCode python解释器设置为poetry环境,并打开一个新终端
  • 完成,我可以通过命令行运行 pytest

这是我的工作区设置.json:

{
    "restructuredtext.confPath": "${workspaceFolder}/docs/source",
    "python.testing.pytestEnabled": true,
}
Run Code Online (Sandbox Code Playgroud)

以下是我尝试刷新测试时显示的输出:

~/.cache/pypoetry/virtualenvs/monaco-bfS1OgpY-py3.9/bin/python ~/.vscode-server/extensions/ms-python.python-2021.11.1422169775/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/coding/monaco -s --cache-clear
cwd: ~/coding/monaco
Run Code Online (Sandbox Code Playgroud)

如果我在终端中手动运行上述命令,它会打印出一个大字典,看起来它描述了我所有的测试。(下面,被截断,但它会这样持续一段时间)。

[{"rootid": ".", "root": "/mnt/c/Users/Scott/Documents/Documents/Coding/monaco", "parents": [{"id": "./tests", "kind": "folder", "name": "tests", "parentid": ".", "relpath": "./tests"}, {"id": "./tests/test_MCCase.py", "kind": "file", "name": "test_MCCase.py", "parentid": "./tests", "relpath": "./tests/test_MCCase.py"}, {"id": "./tests/test_MCSim.py", "kind": "file", "name": "test_MCSim.py", "parentid": "./tests", "relpath": "./tests/test_MCSim.py"}, {"id": "./tests/test_MCSim_io.py", "kind": "file", "name": "test_MCSim_io.py", "parentid": "./tests", "relpath": "./tests/test_MCSim_io.py"}, {"id": "./tests/test_MCVal.py", "kind": "file", "name": "test_MCVal.py", "parentid": "./tests", "relpath": "./tests/test_MCVal.py"},
Run Code Online (Sandbox Code Playgroud)

Tej*_*tty 0

表面看来,要么

  • settings.json 中保存的目录与您的实际工作目录不同
  • VS 代码可以访问的 pytest 安装出现问题,而终端中的 pytest 似乎按照您所说的那样工作。

无论如何,为了帮助您,我们需要更多信息。

  • 您的文件夹结构、文件列表等
  • 如何安装 python、pytest 并将其与 VS code 链接
  • 的输出
> ~/.cache/pypoetry/virtualenvs/myproject-bfS1OgpY-py3.9/bin/python ~/.vscode-server/extensions/ms-python.python-2021.11.1422169775/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/coding/myproject/tests -s --cache-clear .
cwd: ~/coding/myproject/tests
Run Code Online (Sandbox Code Playgroud)

即使是胡言乱语

  • 您认为有帮助但忘记包含在此处的任何其他信息