VS Code / Python / Debugging pytest Test dengan Debugger

Aug 27 2020

Bagaimana saya bisa mendapatkan VS Code untuk menempatkan saya di debugger pada titik kegagalan saat menjalankan tes dengan pytest?

Pytest menangkap semua kesalahan dan menegaskan, dan kode VS memanggil debugger hanya pada kesalahan yang tidak tertangkap (saya dapat mengubah ini menjadi pengecualian yang ditinggikan, tetapi kemudian berhenti pada semua yang diangkat saat dicoba).

Saya mencoba menetapkan --pdbsebagai argumen pytest, tetapi ini menyebabkan kesalahan:

============================= 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 ==============================

Saya memiliki proyek yang sangat sederhana untuk menguji ini:

.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

dan test.py:

def test_with_assert():
    assert 42==2.71828

Apakah --pdbcara yang benar untuk melakukan ini? Atau bagaimana cara memasukkan debugger saat ada pernyataan atau kesalahan?

Jawaban

2 JillCheng Aug 31 2020 at 07:54

Jika Anda ingin masuk debugging saat menjalankan pytest di VSCode dan tetap berada di baris kode, Anda dapat mengklik ' Debug Test' di bagian atas metode setelah memilih tes pytest, seperti yang ditunjukkan pada tangkapan layar:

Selain itu, "python.testing.pytestArgs": [],di .vscode\settings.jsonadalah jalur folder dari file yang diuji, misalnya, file pengujian saya ada di Test_ccbawah afolder.

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

Jika ini bukan yang Anda inginkan, beri tahu saya dan jelaskan detail kebutuhan Anda.

Referensi: Tes debug .

Memperbarui:

Biasanya, ketika kita men-debug pengujian di VSCode, tanpa menetapkan breakpoint untuknya, itu hanya akan menampilkan hasil pengujian. (sukses atau gagal). Ini hanya akan menampilkan informasi pengujian yang relevan di OUTPUT konsol.

Ketika saya menggunakan ekstensi ' Python Test Explorer for Visual Studio code', itu akan menampilkan informasi uji debugging di konsol, yang memicu masalah.

mgf Sep 05 2020 at 07:53

Jawaban ini sebenarnya menyelesaikan masalah. Saya berharap akan ada cara yang lebih mudah, baik dari Visual Studio Code atau raisebendera sederhana dari pytest.