LoginSignup
0
0

More than 1 year has passed since last update.

vscode で Run Test 実行前に、サーバーを立ち上げておきたい!

Posted at

TL;TR

  • VSCODE の テスト(Run Debug)設定ファイル vscode/launch.json, tasks.jsonを用意
  • configuretion.preDebugTask を独自の前処理を記述

目的:Unit Test の前にサーバー起動したい。

Django で Unit Test を用いた開発をしている。

サーバーの挙動を確かめる機能テストする場合、テストの間、サーバーが立ち上がっていないといけない。

毎回テストのときに手動で立ち上げるのは面倒なので、python3 manage.py runserverpython3 manage.py testの前に勝手に実行して、勝手にバックグラウンドで走り、勝手に終了してほしい。

方法

  • VSCODE の テスト(Run Debug)設定ファイル vscode/launch.json, tasks.jsonを用意
  • tasks には 実行したいコマンドに名前をつけられて、いつ実行させるかを設定できる
  • taskstart serverを定義し、configuretion.preDebugTaskを実行させる。
    • その際に background で走る設定を加える。
  • taskstop serverを定義し、configuretion.postDebugTaskを実行させる。
    • テストが終わった時点で、サーバーの terminate する処理を実行させる。

launch.json

{
    // IntelliSense を使用して利用可能な属性を学べます。
    // 既存の属性の説明をホバーして表示します。
    // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Functional Test",
            "type": "python",
            "request": "launch",
            "program": "functional_tests.py",
            "console": "integratedTerminal",
            "cwd": "/workspaces/django_tdd/superlists",
            "preLaunchTask": "start server",
            "postDebugTask": "stop server"
        }, 
        {
            "name": "Unit Test",
            "type": "python",
            "request": "launch",
            "program": "manage.py",
            "args": ["test"],
            "console": "integratedTerminal",
            "cwd": "/workspaces/django_tdd/superlists"
        }
    ]
}

tasks.json


{
    "version": "2.0.0",
    "tasks": [
      {
          "label": "start server",
          "type": "shell",
          "isBackground": true,
          "command": "cd /workspaces/django_tdd/superlists; python3 manage.py runserver"
      },
      {
          "label": "stop server",
          "command": "echo ${input:terminate}",
          "type": "shell"
      }
    ],
    "inputs": [{
      "id": "terminate",
      "type": "command",
      "command": "workbench.action.tasks.terminate",
      "args": "terminateAll"
    }]
  }

終わりに

  • 便利。拾い物だけどいい感じ。
  • vscode がなにかポップアップを出してくるかもしれないが、一度許可すると以降は出なくなり、問題なく使用できるようになった。

(もっとシンプルな方法があったら教えていただきたい。)

参考文献

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