Rij*_*pta 5 python pytest vscode-extensions vscode-python
在多项目(VSCode 将其称为多根)python 存储库中工作时,用于测试的 VSCode python 插件在选择运行所有测试时无法运行所有测试,但运行单个文件夹或测试会通过。这显然是一个奇怪的问题,所以我进一步调查了这一点。
vscode python插件在“测试结果”面板中的输出如下:
Running tests (pytest): /home/workspace
Running test with arguments: --rootdir /home/workspace --override-ini junit_family=xunit1 --junit-xml=/tmp/tmp-289km5DwtjAXzkh.xml
Current working directory: /home/workspace
Workspace directory: /home/workspace
Run completed, parsing output
Test result not found for: ./my_awesome_dir/tests/my_awesome_module/my_awesome_test.py::TestAwesomeness::test_is_awesome[input_data0-AwesomeStuff]
Run Code Online (Sandbox Code Playgroud)
这个问题之前已经在 vscode-python 存储库中提到过: https: //github.com/microsoft/vscode-python/issues/18658
然而,即使我找到了解决问题的方法,VSCode Python 存储库也不允许向现有问题添加更多注释,也不允许打开新问题,以便我可以在那里添加更适合的信息。因此,我打算在这里添加这些信息,这可能是第二好的地方。
本质上,当 settings.json 设置为以下内容时,就会出现问题:
{
"python.testing.pytestArgs": ["my_awesome_module"],
"python.testing.pytestEnabled": true,
"python.analysis.extraPaths": ["my_awesome_module"],
}
Run Code Online (Sandbox Code Playgroud)
测试尝试从 /home/workspace 文件夹运行,而项目位于 /home/workspace/my_awesome_module
将 settings.json 更改为以下内容可修复问题
{
"python.testing.pytestEnabled": true,
"python.testing.cwd": "${workspaceFolder}/my_awesome_module",
"python.analysis.extraPaths": ["my_awesome_module"],
}
Run Code Online (Sandbox Code Playgroud)
我们得到以下输出:
Running tests (pytest): /home/workspace/my_awesome_dir
Running test with arguments: --rootdir /home/workspace --override-ini junit_family=xunit1 --junit-xml=/tmp/tmp-289hG1kDbHbe219.xml ./my_awesome_dir
Current working directory: /home/workspace
Workspace directory: /home/workspace
Run completed, parsing output
./my_awesome_dir/tests/my_awesome_module/my_awesome_test.py::TestAwesomeness::test_is_awesome[input_data0-AwesomeStuff] Passed
Run Code Online (Sandbox Code Playgroud)
我怀疑这个问题也可以通过使用 VSCode 工作区来解决,VSCode 工作区会将存储库中的每个不同项目视为它自己的根,但是在我的特殊情况下,设置工作区是不可取的,因此迄今为止唯一的解决方案是更改工作区目录。