首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VS Code / Python / Debugger pytest with the Debugger

VS Code / Python / Debugger pytest with the Debugger
EN

Stack Overflow用户
提问于 2020-08-27 23:36:55
回答 2查看 14.3K关注 0票数 14

在使用pytest运行测试时,如何让VS Code在失败点将我放入调试器?

Pytest捕获所有错误和断言,而VS代码仅在未捕获的错误时调用调试器(我可以将其更改为引发异常,但随后它会在try引发的所有错误上停止)。

我尝试将--pdb设置为pytest参数,但这会导致错误:

代码语言:javascript
复制
============================= 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

代码语言:javascript
复制
{
    "python.testing.pytestArgs": [
        "--pdb"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true,
    "git.ignoreLimitWarning": false
}

.vscode\launch.json

代码语言:javascript
复制
{
    // 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

代码语言:javascript
复制
[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:

代码语言:javascript
复制
def test_with_assert():
    assert 42==2.71828

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

EN

回答 2

Stack Overflow用户

发布于 2020-08-31 15:54:38

如果您想在VSCode中运行pytest时进入调试,停留在一行代码上,可以在选择pytest后点击方法顶部的'Debug Test‘,如截图所示:

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

代码语言:javascript
复制
>      "python.testing.pytestArgs": [
>             "a/Test_cc"
>         ],

如果这不是你想要的,请让我知道并描述你的需求的细节。

参考:Debug tests

更新:

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

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

票数 3
EN

Stack Overflow用户

发布于 2020-09-05 15:53:02

这个Answer实际上解决了这个问题。我希望有一种更直接的方法,要么来自Visual Studio代码,要么来自pytest的一个简单的raise标志。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63619498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档