VS Code / Python / Debugging pytest Kiểm tra với Debugger
Làm cách nào tôi có thể lấy Mã VS để đưa tôi vào trình gỡ lỗi tại điểm không thành công khi chạy thử nghiệm với pytest?
Pytest bắt tất cả các lỗi và xác nhận, và mã VS chỉ gọi trình gỡ lỗi trên các lỗi chưa được xử lý (tôi có thể thay đổi điều này thành ngoại lệ được nâng cao, nhưng sau đó nó dừng lại trên mọi thứ được nêu ra khi thử).
Tôi đã cố gắng đặt --pdblàm đối số pytest, nhưng điều này dẫn đến lỗi:
============================= 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 ==============================
Tôi có một dự án rất đơn giản để kiểm tra điều này:
.vscode \ settings.json
{
"python.testing.pytestArgs": [
"--pdb"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"git.ignoreLimitWarning": false
}
.vscode \ launcher.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
và test.py:
def test_with_assert():
assert 42==2.71828
Là --pdbcách chính xác để làm điều này? Hoặc làm cách nào để nhập trình gỡ lỗi khi có xác nhận hoặc lỗi?
Trả lời
Nếu bạn muốn nhập gỡ lỗi khi chạy pytest trong VSCode và ở lại một dòng mã, bạn có thể nhấp vào ' Debug Test' ở đầu phương pháp sau khi chọn thử nghiệm pytest, như được hiển thị trong ảnh chụp màn hình:
Bên cạnh đó, "python.testing.pytestArgs": [],trong .vscode\settings.jsonlà đường dẫn thư mục của tập tin thử nghiệm, ví dụ, tập tin thử nghiệm của tôi là ở Test_ccdưới athư mục.
> "python.testing.pytestArgs": [
> "a/Test_cc"
> ],
Nếu đây không phải là điều bạn muốn, vui lòng cho tôi biết và mô tả chi tiết nhu cầu của bạn.
Tham khảo: Các bài kiểm tra gỡ lỗi .
Cập nhật:
Thông thường, khi chúng tôi gỡ lỗi kiểm tra trong VSCode, mà không đặt điểm ngắt cho nó, nó sẽ chỉ hiển thị kết quả kiểm tra. (thành công hay thất bại). Nó sẽ chỉ hiển thị thông tin kiểm tra có liên quan trong OUTPUT của bảng điều khiển.
Khi tôi sử dụng tiện ích mở rộng ' Python Test Explorer for Visual Studio code', nó sẽ hiển thị thông tin kiểm tra gỡ lỗi trên bảng điều khiển, nhắc sự cố.
Câu trả lời này thực sự giải quyết được vấn đề. Tôi ước gì sẽ có một cách dễ dàng hơn, từ Visual Studio Code hoặc một raisecờ đơn giản từ pytest.