VS Code / Python / Debugging pytest Test za pomocą debugera

Aug 27 2020

Jak mogę uzyskać VS Code, aby umieścić mnie w debugerze w momencie awarii podczas uruchamiania testów z pytest?

Pytest wyłapuje wszystkie błędy i potwierdzenia, a kod VS wywołuje debugger tylko w przypadku nieprzechwyconych błędów (mogę to zmienić na podniesiony wyjątek, ale wtedy zatrzymuje się na wszystkim, co zostało zgłoszone podczas próby).

Próbowałem ustawić --pdbjako pytest argument, ale prowadzi to do błędów:

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

Mam bardzo prosty projekt do testowania tego:

.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

i test.py:

def test_with_assert():
    assert 42==2.71828

Czy --pdbto właściwy sposób? Lub jak wejść do debugera po potwierdzeniu lub błędzie?

Odpowiedzi

2 JillCheng Aug 31 2020 at 07:54

Jeśli chcesz wejść do debugowania podczas uruchamiania pytest w VSCode i pozostać w wierszu kodu, możesz kliknąć „ Debug Test” u góry metody po wybraniu testu pytest, jak pokazano na zrzucie ekranu:

Ponadto "python.testing.pytestArgs": [],w .vscode\settings.jsonto ścieżka folderu badanego pliku, na przykład, mój plik testowy jest w Test_ccramach akatalogu.

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

Jeśli nie tego chcesz, daj mi znać i opisz szczegóły swoich potrzeb.

Źródła: testy debugowania .

Aktualizacja:

Zwykle, gdy debugujemy test w VSCode, bez ustawiania dla niego punktu przerwania, wyświetli się tylko wynik testu. (sukces lub porażka). Wyświetli tylko istotne informacje testowe w konsoli OUTPUT.

Kiedy używam rozszerzenia ' Python Test Explorer for Visual Studio code', wyświetli informacje o teście debugowania na konsoli, podpowiadając problem.

mgf Sep 05 2020 at 07:53

Ta odpowiedź faktycznie rozwiązuje problem. Chciałbym, żeby istniał prostszy sposób, albo z Visual Studio Code, albo z prostej raiseflagi z pytest.