pytestは、Pythonのテストフレームワークで、単体テストを簡単に実行できる便利なツールです。
テストをする際、全てのテストを実行するのではなく、特定のファイルや特定のテストだけを実行したい場面もよくあります。
この記事では、pytestを使用して「特定のファイルだけ実行する方法」や「特定のテストだけ実行する方法」をサンプルコードを用いて、わかりやすく解説します。
前提条件
以下のようなディレクトリ構成がすでに設定されていることを前提とします。
project_root/
│
├── app/
│ └── example.py : テスト対象のPythonファイル
└── tests/
├── test_example.py : example.pyに対するテストファイル
└── test_another.py : 別のテストファイル上記のディレクトリ構成において、以下の方法について説明します。
- 特定のファイルだけ実行する方法
test_example.pyのみを実行する方法
- 特定のテストだけ実行する方法
test_example.pyに記述されているテストの中で特定のテストのみを実行する方法
pytestで特定のファイルだけ実行する方法
特定のテストファイルだけを実行するには、pytestコマンドにテストファイル名を指定します。
# 構文
pytest (path)
# 例
pytest tests/test_example.py上記のコマンドを実行すると、tests/test_example.pyに記述されている全てのテストが実行されます。
では実際に、上記のコマンドを実行してみましょう。「テスト対象のexample.py」と「そのテストであるtest_example.py」のサンプルコードを以下に示します。
app/example.py
def add(x, y):
return x + ytests/test_example.py
from app.example import add
def test_add():
assert add(1, 2) == 3
def test_add_negative():
assert add(-1, -1) == -2上記のサンプルコードにおいて、pytest tests/test_example.pyを実行すると、test_example.pyに含まれるtest_addとtest_add_negativeの両方のテストが実行されます。
テストが失敗する場合
pytestした時にImportErrorが出る場合があります。この場合、以下に示すように、テストディレクトリ(tests/)に __init__.py ファイルを追加すると、そのディレクトリが Python のパッケージとして認識され、モジュールのインポートエラー(ImportError)が解消される場合があります。
project_root/
│
├── app/
│ └── example.py : テスト対象のPythonファイル
└── tests/
├── __init__.py
├── test_example.py : example.pyに対するテストファイル
└── test_another.py : 別のテストファイルpytestで特定のテストだけ実行する方法
ファイル内の特定のテストだけを実行するには、pytestコマンドにテストファイル名とテストメソッドを指定します。
# 構文
pytest (path)::(test_method)
# 例
pytest tests/test_example.py::test_add上記のコマンドを実行すると、tests/test_example.pyに含まれるtest_addだけが実行されます。
では実際に、上記のコマンドを実行してみましょう。「テスト対象のexample.py」と「そのテストであるtest_example.py」のサンプルコードを以下に示します。
app/example.py
def add(x, y):
return x + ytests/test_example.py
from app.example import add
def test_add():
assert add(1, 2) == 3
def test_add_negative():
assert add(-1, -1) == -2上記のサンプルコードにおいて、pytest tests/test_example.py::test_addを実行すると、tests/test_example.pyに含まれるtest_addだけが実行されます。
クラス内にテストメソッドが定義されている場合
pytestを使う際、テストメソッドがクラス内に定義されている場合でも、特定のテストメソッドだけを実行することができます。この場合、テストファイルだけでなくクラス名も指定する必要があります。
# 構文
pytest (path)::(TestClass)::(test_method)
# 例
pytest tests/test_example.py::TestAdd::test_add上記のコマンドを実行すると、tests/test_example.pyに含まれるTestAddクラスのtest_addメソッドだけが実行されます。
では実際に、上記のコマンドを実行してみましょう。「テスト対象のexample.py」と「そのテストであるtest_example.py」のサンプルコードを以下に示します。
app/example.py
def add(x, y):
return x + ytests/test_example.py
from app.example import add
class TestAdd:
def test_add(self):
assert add(1, 2) == 3
def test_add_negative(self):
assert add(-1, -1) == -2上記のサンプルコードにおいて、pytest tests/test_example.py::TestAdd::test_addを実行すると、TestAddクラスに含まれるtest_addメソッドだけが実行されます。
本記事のまとめ
この記事では『pytestで特定のファイルや特定のテストだけを実行する方法』を説明しました。
- 特定のファイルだけ実行する場合
pytest (path)コマンドで、特定のファイル内の全テストを実行できます。
- 特定のテストだけ実行する場合
pytest (path)::(test_method)コマンドで、ファイル内の特定のテストメソッドのみを実行できます。
- クラス内の特定のテストを実行する場合
- テストがクラス内に定義されている場合は、
pytest (path)::(TestClass)::(test_method)コマンドで、ファイル内の特定のクラス内の特定のテストメソッドのみを実行できます。
- テストがクラス内に定義されている場合は、
お読み頂きありがとうございました。