VS Code / Python / Debugging pytest Test ด้วย Debugger

Aug 27 2020

ฉันจะรับ VS Code เพื่อนำฉันเข้าสู่ดีบักเกอร์เมื่อถึงจุดที่ล้มเหลวเมื่อเรียกใช้การทดสอบด้วย pytest ได้อย่างไร

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วิธีที่ถูกต้องหรือไม่? หรือฉันจะป้อนดีบักเกอร์เมื่อมีการยืนยันหรือข้อผิดพลาดได้อย่างไร

คำตอบ

2 JillCheng Aug 31 2020 at 07:54

หากคุณต้องการเข้าสู่การดีบักเมื่อเรียกใช้ pytest ใน VSCode และอยู่ที่บรรทัดของโค้ดคุณสามารถคลิก ' Debug Test' ที่ด้านบนของวิธีการหลังจากเลือกการทดสอบ pytest ดังที่แสดงในภาพหน้าจอ:

นอกจากนี้"python.testing.pytestArgs": [],ในการ.vscode\settings.jsonเป็นเส้นทางโฟลเดอร์ของแฟ้มทดสอบตัวอย่างเช่นไฟล์ทดสอบของฉันอยู่ในTest_ccภายใต้aโฟลเดอร์

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

หากนี่ไม่ใช่สิ่งที่คุณต้องการโปรดแจ้งให้เราทราบและอธิบายรายละเอียดความต้องการของคุณ

อ้างอิง: การทดสอบตรวจแก้จุดบกพร่อง

อัปเดต:

โดยปกติเมื่อเราดีบักการทดสอบใน VSCode โดยไม่ตั้งค่าเบรกพอยต์จะแสดงเฉพาะผลการทดสอบเท่านั้น (ความสำเร็จหรือความล้มเหลว). จะแสดงเฉพาะข้อมูลการทดสอบที่เกี่ยวข้องใน OUTPUT ของคอนโซล

เมื่อฉันใช้ส่วนขยาย ' Python Test Explorer for Visual Studio code' มันจะแสดงข้อมูลการทดสอบการดีบักบนคอนโซลเพื่อแจ้งปัญหา

mgf Sep 05 2020 at 07:53

คำตอบนี้ช่วยแก้ปัญหาได้จริง ฉันหวังว่าจะมีวิธีที่ตรงไปตรงมามากกว่านี้ไม่ว่าจะจาก Visual Studio Code หรือraiseแฟล็กธรรมดาจาก pytest