使用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
和test.py:
def test_with_assert():
assert 42==2.71828
--pdb
是正确的方法吗?还是在出现断言或错误时如何进入调试器?
答案 0 :(得分:2)
如果要在VSCode中运行pytest时进入调试状态并停留在一行代码中,可以在选择pytest测试后单击方法顶部的“ Debug Test
”,如屏幕截图所示:
此外,"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
'时,它将在控制台上显示调试测试信息,提示问题。
答案 1 :(得分:0)
此Answer实际上解决了问题。我希望有一种更简单的方法,可以是Visual Studio Code中的方法,也可以是pytest中的简单raise
标志。