VS Code / Python /デバッガーを使用したpytestテストのデバッグ
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これを行う正しい方法はありますか?または、アサートまたはエラー時にデバッガーに入るにはどうすればよいですか?
回答
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'を使用すると、コンソールにデバッグテスト情報が表示され、問題が発生します。
この回答は実際に問題を解決します。Visual Studio Codeから、またはraisepytestからの単純なフラグから、より簡単な方法があればいいのにと思います。