在使用pytest运行测试时,如何让VS代码在出现故障时将我放入调试器
Pytest捕获所有错误和断言,VS代码仅在未捕获错误时调用调试器(我可以将其更改为引发的异常,但在try下引发的所有错误上它都会停止)
我试图将--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
{
如果希望在VSCode中运行pytest时进入调试并停留在代码行上,可以在选择pytest测试后单击方法顶部的“
Debug Test
”,如屏幕截图所示:此外,
"python.testing.pytestArgs": [],
In.vscode\settings.json
是测试文件的文件夹路径,例如,我的测试文件位于a
文件夹下的Test_cc
如果这不是你想要的,请让我知道并描述你需要的细节
参考:Debug tests
更新:
通常,当我们在VSCode中调试测试时,没有为它设置断点,它只会显示测试结果。(成功或失败)。 它将仅在控制台输出中显示相关测试信息
当我使用扩展名“
Python Test Explorer for Visual Studio code
”时,它将在控制台上显示调试测试信息,提示问题这个Answer实际上解决了这个问题。我希望有一种更直接的方法,无论是来自VisualStudio代码还是来自pytest的一个简单的
raise
标志相关问题 更多 >
编程相关推荐