VS Code / Python / Debugger로 pytest 테스트 디버깅

Aug 27 2020

pytest로 테스트를 실행할 때 실패 지점에서 디버거로 들어가도록 VS Code를 얻으려면 어떻게해야합니까?

Pytest는 모든 오류를 포착하고 어설 션하며 VS 코드는 포착되지 않은 오류에서만 디버거를 호출합니다 (이를 발생한 예외로 변경할 수 있지만 시도에서 발생한 모든 항목에서 중지됨).

--pdbpytest 인수 로 설정하려고 했지만 이로 인해 오류가 발생합니다.

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

VSCode에서 pytest를 실행할 때 디버깅을 시작하고 코드 한 줄에 Debug Test머물고 싶다면 스크린 샷과 같이 pytest 테스트를 선택한 후 메서드 상단의 ' '를 클릭하면 됩니다.

또한 "python.testing.pytestArgs": [],in .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 또는 raisepytest 의 간단한 플래그에서 보다 직접적인 방법이 있기 를 바랍니다.