如何在 Visual Studio Code 和 Python 中仅运行一个测试并输出

Gei*_*tad 3 python testing test-explorer visual-studio-code

有谁知道如何在 Visual Studio Code 中使用测试资源管理器仅运行一个 python 测试?

我已经安装了https://marketplace.visualstudio.com/items?itemName=LittleFoxTeam.vscode-python-test-adapter

我正在使用 pytest。

下图中:

在此输入图像描述

如果我按“运行测试”,则只会运行一个测试,但我看不到任何日志/输出。

要查看输出,我必须按“运行”,但这会运行我的所有测试。这是令人难以置信的令人沮丧。

我希望能够使用 Visual Studio Code 界面仅运行一个测试并查看此次运行的日志。

Gei*_*tad 5

我没有找到一种优雅的方法来仅运行一个测试,然后查看该测试的正确采样/收集的输出,但我找到了一种方法来仅运行我正在处理的测试,方法是使用以下标记:

@pytest.mark.run_this_test
Run Code Online (Sandbox Code Playgroud)

然后在 VSCode 中的任意位置按 F5 即可一遍又一遍地运行此测试。PyTest 调试配置如下所示:

{
    "name": "PyTest",
    "type": "python",
    "request": "launch",
    "console": "integratedTerminal",
    "module": "pytest",
    "args": [
        "-s", "-m run_this_test"
    ],
    "justMyCode": false,
    "pythonPath": "${config:python.pythonPath}",
    "env": {
        "PYTHONPATH": "${workspaceRoot}"
}
Run Code Online (Sandbox Code Playgroud)

这种“标签”显然在团队中无法扩展,但它仅对我一个人有效。

我创建了一个包含我的解决方案的存储库:https ://bitbucket.org/geircode/vscode-testsetup-python

要测试它:

  • 运行脚本“requirements.install.bat”
  • 在根文件夹中打开vscode
  • 在“调试和运行”选项卡中选择“PyTest”
  • 按 F5 开始调试并查看测试的正确输出