0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】VSCodeでpytestを使ってテストコードを実行する方法

Posted at

はじめに

Pythonでの開発では、テストの自動化が重要です。今回は、VSCode上でpytestを使ってテストを実行する方法を解説します。

初心者向けに、環境構築から基本的なテストの実行方法まで詳しく説明します!


1. 環境構築

(1) Pythonのインストール

Pythonがインストールされていない場合は、以下の公式サイトからインストールしてください。
👉 Python公式サイト

インストール後、以下のコマンドでバージョンを確認します。

python --version

または(環境によってはpython3の場合もあります)

python3 --version

(2) VSCodeの拡張機能をインストール

VSCodeでPython開発を快適にするため、以下の拡張機能をインストールしましょう。

  1. Python Extension for Visual Studio Code

    • VSCodeの拡張機能 (Ctrl + Shift + X) で Python を検索し、Microsoft製の拡張機能をインストール
  2. Python Test Explorer for Visual Studio Code(任意)

    • Python Test Explorer をインストールすると、GUIでテストを実行可能

(3) 仮想環境の作成(推奨)

プロジェクトごとに仮想環境を作成すると、依存関係を管理しやすくなります。

python -m venv venv

仮想環境を有効化します。

  • Windows(PowerShell):
    .\venv\Scripts\Activate
    
  • macOS / Linux:
    source venv/bin/activate
    

(4) pytest のインストール

仮想環境を有効化した状態で、以下のコマンドを実行します。

pip install pytest

インストール確認:

pytest --version

2. 簡単なテストコードを書く

(1) テスト対象の関数を作る

まず、math_functions.py というファイルを作成し、以下の関数を定義します。

# math_functions.py
def add(x, y):
    return x + y

def multiply(x, y):
    return x * y

(2) テストコードを書く

次に、test_math_functions.py を作成し、テストコードを書きます。

# test_math_functions.py
from math_functions import add, multiply

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0
    assert add(0, 0) == 0

def test_multiply():
    assert multiply(2, 3) == 6
    assert multiply(-1, 1) == -1
    assert multiply(0, 10) == 0

ポイント

  • ファイル名は test_ で始める(pytesttest_*.py を自動的に探す)
  • assert を使って期待値と実際の値を比較する

3. pytest を使ってテストを実行

ターミナルで以下を実行。

pytest

特定のテストファイルだけを実行したい場合は:

pytest test_math_functions.py

成功した場合の出力例

============================= test session starts =============================
collected 2 items

test_math_functions.py ..                                         [100%]

============================== 2 passed in 0.12s ==============================

失敗した場合
エラーメッセージが表示されるので、修正して再実行。


4. VSCodeでpytestを実行する方法

(1) Python 拡張機能を使う

  1. テストの自動検出を有効化

    • VSCodeの左側のテストアイコン(🧪)をクリック
    • Configure Python Tests(「Python テストの構成」)をクリック
    • pytest を選択し、tests(またはプロジェクトのテストフォルダ)を選択
  2. テストを実行

    • Run Test ボタンをクリック

(2) Python Test Explorer 拡張機能を使う(GUI)

  1. Python Test Explorer をインストール
  2. テストの自動検出を有効にする
  3. Test Explorer タブからGUIでテストを実行&結果を確認

5. トラブルシューティング

pytestが見つからない場合

  • 仮想環境が有効になっているか確認
  • pytestがインストールされているか確認 (pip list | grep pytest)

テストが認識されない場合

  • pytest.ini を作成し、以下を設定すると改善することがあります。
# pytest.ini
[pytest]
python_files = test_*.py
python_functions = test_*

まとめ

手順のおさらい

  1. 環境構築(Python + VSCode + pytest をインストール)
  2. テストコードを書くassert を使って期待値をチェック)
  3. pytest でテスト実行(CLIまたはVSCodeのGUIから実行)

これで、VSCode上でpytestを使ってテストを実行できるようになりました!🎉

もっと学びたい場合は、公式ドキュメントもチェックしてみてください。
👉 pytest公式ドキュメント

この記事が役に立ったら、いいねやシェアをお願いします!😊

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?