简体   繁体   English

如何在带有输出的 Visual Studio Code 和 Python 中运行仅运行单个测试

[英]How to run Run only a Single test in Visual Studio Code and Python with output

Does anyone know how to run only one python test with Test Explorer in Visual Studio Code?有谁知道如何在 Visual Studio Code 中使用测试资源管理器只运行一个 python 测试?

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

I am using pytest.我正在使用 pytest。

In the picture below:在下图中:

在此处输入图片说明

If I press "Run Test" then only one test runs, but I do not see any log / output.如果我按“运行测试”,则只运行一个测试,但我看不到任何日志/输出。

To see the output I have to press "Run", but this runs all my tests.要查看输出,我必须按“运行”,但这会运行我所有的测试。 This is incredibly frustrating.这是令人难以置信的令人沮丧。

I would like to be able to run only the one test with the Visual Studio Code interface and see the log for this run.我希望能够仅使用 Visual Studio Code 界面运行一个测试并查看此运行的日志。

Check the Output Panel for output (look under the View menu if you're unfamiliar with it).检查输出面板的输出(如果您不熟悉它,请查看查看菜单下)。 There's probably a channel there where test output is sent (eg for the Python extension for VS Code it creates a "Python Test Log" channel for all test output).那里可能有一个发送测试输出的通道(例如,对于 VS Code 的 Python 扩展,它为所有测试输出创建了一个“Python 测试日志”通道)。

在此处输入图片说明

I did not find one elegant way to just run one test and then see the correct sampled/collected output from that test, but I found a way to run only the test I was working on by tagging it with:我没有找到一种优雅的方法来只运行一个测试,然后从该测试中看到正确的采样/收集输出,但我找到了一种方法,通过标记它来只运行我正在处理的测试:

@pytest.mark.run_this_test

and hit F5 anywhere in VSCode to just run this test over and over again.并在 VSCode 中的任意位置按 F5 以一遍又一遍地运行此测试。 The PyTest debug config looks like this: 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}"
}

This 'tagging' does not scale in a team obviously, but it works for me alone.这种“标记”显然不能在团队中扩展,但它只对我有用。

I created a repo containing my solution: https://bitbucket.org/geircode/vscode-testsetup-python我创建了一个包含我的解决方案的 repo: https : //bitbucket.org/geircode/vscode-testsetup-python

To test it:要测试它:

  • run the script "requirements.install.bat"运行脚本“requirements.install.bat”
  • open vscode in the root folder在根文件夹中打开 vscode
  • select "PyTest" in the "debug and run" tab在“调试和运行”选项卡中选择“PyTest”
  • Hit F5 to start debugging and see the correct output from the test按 F5 开始调试并查看测试的正确输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM