mgf*_*mgf 12 python pytest visual-studio-code
使用 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 ==============================
Run Code Online (Sandbox Code Playgroud)
我有一个非常简单的项目来测试这个:
.vscode\settings.json
{
"python.testing.pytestArgs": [
"--pdb"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"git.ignoreLimitWarning": false
}
Run Code Online (Sandbox Code Playgroud)
.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
}
]
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
和 test.py:
def test_with_assert():
assert 42==2.71828
Run Code Online (Sandbox Code Playgroud)
为--pdb做到这一点的正确方法是什么?或者如何在断言或错误时进入调试器?
Jil*_*eng 12
如果你想在 VSCode 中运行 pytest 时进入调试并停留在一行代码上,可以在Debug Test选择 pytest 测试后单击方法顶部的“ ”,如截图所示:
另外,"python.testing.pytestArgs": [],in.vscode\settings.json是测试文件的文件夹路径,比如我的测试文件就在Test_cc该a文件夹下。
> "python.testing.pytestArgs": [
> "a/Test_cc"
> ],
Run Code Online (Sandbox Code Playgroud)
如果这不是您想要的,请告诉我并描述您的需求细节。
参考:调试测试。
更新:
通常,我们在VSCode中调试测试时,如果不为其设置断点,则只会显示测试结果。(成功或失败)。它只会在控制台OUTPUT中显示相关的测试信息。
当我使用扩展“ Python Test Explorer for Visual Studio code”时,它会在控制台上显示调试测试信息,提示该问题。
| 归档时间: |
|
| 查看次数: |
11152 次 |
| 最近记录: |