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

问题描述 投票:0回答:3

当使用 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",
            "type": "python",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "justMyCode": false
        },
        {
            "name": "Debug Tests",
            "type": "python",
            "request": "test",
            "console": "internalConsole",
            "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

和测试.py:

def test_with_assert():
    assert 42==2.71828

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

python visual-studio-code pytest
3个回答
14
投票

如果你想在 VSCode 中运行 pytest 时进入调试并停留在一行代码上,可以在选择 pytest 测试后点击方法顶部的“

Debug Test
”,如图所示:

另外,

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

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

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

参考:调试测试

更新:

通常,我们在VSCode中调试测试时,如果不为其设置断点,则只会显示测试结果。 (成功或失败)。 只会在控制台OUTPUT中显示相关测试信息。

当我使用扩展“

Python Test Explorer for Visual Studio code
”时,它会在控制台上显示调试测试信息,提示该问题。


5
投票

这个答案实际上解决了问题。我希望有一种更直接的方法,可以使用 Visual Studio Code,也可以使用 pytest 的简单

raise
标志。


0
投票

如果您使用的是 Python Test Explorer for Visual Studio Code,则需要在

.vscode/lauch.json
文件中添加特定配置。它需要包含条目
type == python
purpose == ["debug-test"]
,如官方文档中所述。您可以在下面看到一个有效的示例:

{
   "name": "Python Test Debugger",
   "type": "python",
   "request": "launch",
   "console": "integratedTerminal",
   "justMyCode": false,
   "purpose": ["debug-test"]
}

.vscode/launch.json
文件的完整示例如下所示。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Test Debugger",
            "type": "python",
            "request": "launch",
            "console": "integratedTerminal",
            "justMyCode": false,
            "purpose": ["debug-test"]
        }
        {
            "name": "Python Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "justMyCode": false,
        }
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.