[Python]unittestモジュール
Pythonのunittestモジュールは、Python標準ライブラリに含まれている単体テストフレームワークです。これを使うことで、関数やクラスの動作を確認するための自動化テストを書き、実行することができます。
基本的な使い方
簡単なテストケースの例
まず、unittestモジュールをインポートして、テストケースを定義します。テストケースはunittest.TestCaseクラスを継承して作成します。
import unittest
class TestClass(unittest.TestCase):
def test_case1(self):
self.assertEqual(1 + 1, 2)
def test_case2(self):
self.assertEqual(5 - 3, 2)
def test_case3(self):
self.assertTrue(True)
def test_case4(self):
self.assertFalse(False)
if __name__ == '__main__':
unittest.main()
テストケースの実行
上記のコードを実行すると、定義されたテストケースが実行され、すべてのテストがパスするかどうかが確認されます。結果は以下のように表示されます。
$ py test.py .... ---------------------------------------------------------------------- Ran 4 tests in 0.001s OK
テストケースの実行(失敗時)
test_case4を self.assertFalse(True) としたとき、このテストケースは失敗し、以下のように失敗した箇所、失敗した理由などが表示されます。
$ py test.py ...F ====================================================================== FAIL: test_case4 (__main__.TestClass.test_case4) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 15, in test_case4 self.assertFalse(True) AssertionError: True is not false ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED (failures=1)
同じテストケース内で複数のアサーションを使う
同じテストケース内で複数のアサーションを行いたい場合は、subTestを使うと各アサーションが独立したテストとして扱われ、どのアサーションが失敗したかを詳細に確認することができます。
subTestを使って複数のアサーションを検証する例
import unittest
class TestClass(unittest.TestCase):
def test_case1(self):
test_cases = [
(1 + 1, 2),
(2 * 2, 3),
(5 - 3, 2),
(6 / 2, 1),
]
for operation, expected in test_cases:
with self.subTest(operation=operation):
self.assertEqual(operation, expected)
if __name__ == '__main__':
unittest.main()
- test_case1メソッド:テストケースをリストで定義します。このリストには、各演算と期待される結果のペアが含まれています。
- forループ:各テストケースに対してループを回します。
- with self.subTest(operation=operation):各アサーションをsubTestコンテキスト内で実行します。これにより、各テストが独立して評価され、失敗した場合にどのテストが失敗したかが明確になります。
テストケースの実行(失敗時)
このテストケースは2つ失敗しているので、結果は以下のように評されます。
$ py test11.py FF ====================================================================== FAIL: test_case1 (__main__.TestClass.test_case1) (operation=4) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 15, in test_case1 self.assertEqual(operation, expected) AssertionError: 4 != 3 ====================================================================== FAIL: test_case1 (__main__.TestClass.test_case1) (operation=3.0) ---------------------------------------------------------------------- Traceback (most recent call last): File "test.py", line 15, in test_case1 self.assertEqual(operation, expected) AssertionError: 3.0 != 1 ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=2)
この方法を使うことで、テストが失敗した場合でも他のテストに影響を与えずに実行を続けることができます。また、テスト結果のレポートが詳細になります。
アサーションメソッド
assertEqual(a, b) | aとbが等しいか確認する。 |
---|---|
assertNotEqual(a, b) | aとbが等しくないか確認する。 |
assertTrue(x) | xがTrueであるか確認する。 |
assertFalse(x) | xがFalseであるか確認する。 |
assertIs(a, b) | aとbが同一オブジェクトであるか確認する。 |
assertIsNot(a, b) | aとbが同一オブジェクトでないか確認する。 |
assertIsNone(x) | xがNoneであるか確認する。 |
assertIsNotNone(x) | xがNoneでないか確認する。 |
assertIn(a, b) | aがbに含まれているか確認する。 |
assertNotIn(a, b) | aがbに含まれていないか確認する。 |
assertRaises(exception, callable, *args, **kwds) | 例外exceptionが発生するか確認する。 |
- アサーションメソッドにおいて、True == 1、False == 0 == [] (空配列)と判断される。つまり以下のアサーションメソッドによる検証は全てTrueを返す。
- assertTrue(1)
- assertFalse(0)
- assertFalse([])
コメント
コメントを投稿