LoginSignup
0
0

More than 1 year has passed since last update.

docker compose で起動したコンテナ内での pytest 実行時に breakpoint で止める

Posted at

はじめに

web sever の実行時に breakpoint で止める方法を書いた記事はたくさん見かけます。
しかし、dokcer compose したコンテナ内での pytest 実行時に breakpoint で止める記事は見かけず、苦労したので書きます。
(test code に break point を置いた場合の話です)

確認した環境

  • macOS Big Sur 11.6.1
  • Visual Studio Code Version: 1.59.0
  • Docker Desktop 4.3.1.
  • Docker Engine v20.10.11

実践

ローカルでのステップ

  • VsCodeからコンテナに attach するための extension を install する
  • docker compose up でアプリケーションの dev コンテナ を起動する
  • VsCode の左端の緑部分をクリック
    • スクリーンショット 2022-02-03 23.43.37.png
  • Attach to Running Container... を選択
    • スクリーンショット 2022-02-03 23.46.56.png
  • attach できるコンテナの一覧が出るので、 アプリケーションの dev コンテナを選択する
    • 選択するとリモートコンテナコンテナに入れる

リモートコンテナ(アプリケーションの dev コンテナ)内のステップ

  • リモートコンテナの中身が新しく VSCode
  • 公式の debug 記事 に記載のある VsCode の debugger 機能を含む extension を install する
  • VsCode のエクスプローラーで以下を確認する
    • テストコードが存在すること
    • アプリケーションのコードが存在すること
    • .vscode フォルダ内に launch.json が存在すること
  • launch.json の configurations 項目を確認する
    • name : 任意 (ここでは exec pytest)
      • 後で debug を開始する際の名称になる
    • request : launch
    • module : pytest
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: Module",
            "type": "python",
            "request": "launch",
            "module": "pytest",
            "args": [
                "tests"
            ]
        }
    ]
}
  • vscode の下バーのインタプリタ設定項目が remote コンテナのインタプリタになっていることを確認する
    • スクリーンショット 2022-02-04 0.14.03.png
  • pytest 実行時に止めたい場所に break point を置く
    • test code に break point を置きます
  • 左メニューの debug をクリックする
    • スクリーンショット 2022-02-04 0.10.21.png
  • 下記ボタンを押して pytest が実行される
    • スクリーンショット 2022-02-04 0.12.42.png
  • break point で止まってくれるはずです (あとはよしなに debug してください)
  • お疲れ様でした!!
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0