简体   繁体   English

VS Code / Python / 使用调试器调试 pytest 测试

[英]VS Code / Python / Debugging pytest Test with the Debugger

How can I get VS Code to put me into the debugger at the point of failure when running tests with pytest?使用 pytest 运行测试时,如何让 VS Code 在出现故障时将我放入调试器?

Pytest catches all errors and asserts, and VS code invokes the debugger only on uncaught errors (I can change this to raised exception, but then it stops on everything raised under a try). Pytest 捕获所有错误并断言,并且 VS 代码仅在未捕获的错误上调用调试器(我可以将其更改为引发的异常,但它会在尝试下引发的所有错误时停止)。

I tried to set --pdb as pytest argument, but this leads to errors:我试图将--pdb设置为 pytest 参数,但这会导致错误:

============================= test session starts =============================
platform win32 -- Python 3.8.1, pytest-5.3.2, py-1.8.1, pluggy-0.13.1
rootdir: c:\Projects\debugtest, inifile: pytest.ini
collected 1 item

test.py F
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Traceback (most recent call last):
  File "C:\Projects\debugtest\test.py", line 4, in test_with_assert
    assert 42==2.71828
AssertionError: assert 42 == 2.71828
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>> PDB post_mortem (IO-capturing turned off) >>>>>>>>>>>>>>>>>>
> c:\projects\debugtest\test.py(4)test_with_assert()
-> assert 42==2.71828
(Pdb)

PYDEV DEBUGGER WARNING:
sys.settrace() should not be used when the debugger is being used.
This may cause the debugger to stop working correctly.
If this is needed, please check: 
http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html
to see how to restore the debug tracing back correctly.
Call Location:
  File "C:\Program Files\Python38\lib\bdb.py", line 359, in set_quit
    sys.settrace(None)


- generated xml file: C:\Users\tzhgfma1\AppData\Local\Temp\tmp-24044mEWMyB1nPYAu.xml -
!!!!!!!!!!!!!!!!!! _pytest.outcomes.Exit: Quitting debugger !!!!!!!!!!!!!!!!!!!
============================== 1 failed in 0.43s ==============================

I have a very simple project for testing this:我有一个非常简单的项目来测试这个:

.vscode\\settings.json .vscode\\settings.json

{
    "python.testing.pytestArgs": [
        "--pdb"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true,
    "git.ignoreLimitWarning": false
}

.vscode\\launch.json .vscode\\launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "internalConsole",
            "justMyCode":false
        },
        {
            "name": "Python: Attach using Process Id",
            "type": "python",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "justMyCode": false
        },
        {
            "name": "Debug Tests",
            "type": "python",
            "request": "test",
            "console": "internalConsole",
            "justMyCode": false
        }
    ]
}

pytest.ini pytest.ini

[pytest]

python_files = test*.py
python_classes = Test
python_functions = test
addopts = --tb=native
console_output_style = classic
junit_duration_report = call
filterwarnings =
    ignore::RuntimeWarning

and test.py:和 test.py:

def test_with_assert():
    assert 42==2.71828

Is --pdb the correct way to do this? --pdb是正确的方法吗? Or how do I enter the debugger upon an assert or error?或者如何在断言或错误时进入调试器?

If you want to enter debugging when running pytest in VSCode and stay at a line of code, you could click ' Debug Test ' at the top of the method after selecting the pytest test, as shown in the screenshot:如果想在VSCode中运行pytest时进入调试状态,停留在一行代码,可以在选择pytest测试后点击方法顶部的' Debug Test ',如截图所示:

在此处输入图片说明

In addition, "python.testing.pytestArgs": [], in .vscode\\settings.json is the folder path of the tested file, for example, my test file is in Test_cc under the a folder.另外, "python.testing.pytestArgs": [], .vscode\\settings.json是被测文件的文件夹路径,比如我的测试文件在Test_cc下的a文件夹。

>      "python.testing.pytestArgs": [
>             "a/Test_cc"
>         ],

If this is not what you want, please let me know and describe the details of your needs.如果这不是您想要的,请让我知道并描述您的需求的详细信息。

Reference: Debug tests .参考: 调试测试

Update:更新:

Usually, when we debug the test in VSCode, without setting a breakpoint for it, it will only display the test result.通常,我们在 VSCode 中调试测试时,没有为其设置断点,它只会显示测试结果。 (success or failure). (成功或失败)。 It will only display relevant test information in the console OUTPUT.它只会在控制台 OUTPUT 中显示相关的测试信息。

When I use the extension ' Python Test Explorer for Visual Studio code ', it will display the debugging test information on the console, prompting the issue.当我使用扩展‘ Python Test Explorer for Visual Studio code ’时,控制台会显示调试测试信息,提示问题。

在此处输入图片说明

This Answer actually solves the problem.这个答案实际上解决了这个问题。 I wish there would be a more straight-forward way, either from Visual Studio Code or a simple raise flag from pytest.我希望有一种更直接的方式,无论是来自 Visual Studio Code 还是来自 pytest 的简单raise标志。

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

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