微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决VS Code / Python /使用调试器调试pytest测试

使用pytest运行测试时,如何在出现故障时将VS Code放入调试器?

Pytest捕获所有错误并进行断言,并且VS代码仅在未捕获的错误调用调试器(我可以将其更改为引发的异常,但是在尝试进行的所有引发的异常中都停止)。

我试图将--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 ==============================

我有一个非常简单的项目对此进行测试:

.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

{
    // 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","request": "attach","processId": "${command:pickProcess}","justMyCode": false
        },{
            "name": "Debug Tests","request": "test","justMyCode": false
        }
    ]
}

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

和test.py:

def test_with_assert():
    assert 42==2.71828

--pdb是正确的方法吗?还是在出现断言或错误时如何进入调试器?

解决方法

如果要在VSCode中运行pytest时进入调试状态并停留在一行代码中,可以在选择pytest测试后单击方法顶部的“ Debug Test”,如屏幕截图所示:

enter image description here

此外,"python.testing.pytestArgs": [],中的.vscode\settings.json是被测试文件的文件夹路径,例如,我的测试文件位于Test_cc文件夹下的a中。

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

如果这不是您想要的,请告诉我并描述您的需求的详细信息。

参考:Debug tests

更新:

通常,当我们在VSCode中调试测试时,没有为其设置断点,它将仅显示测试结果。 (成功或失败)。 它将仅在控制台OUTPUT中显示相关的测试信息。

当我使用扩展名'Python Test Explorer for Visual Studio code'时,它将在控制台上显示调试测试信息,提示问题。

enter image description here

,

Answer实际上解决了问题。我希望有一种更简单的方法,可以是Visual Studio Code中的方法,也可以是pytest中的简单raise标志。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。