Pythonによるテスト入門
Alexander Levin
Data Scientist
class Rectangle:
# Constructor of Rectangle
def __init__(self, a, b):
self.a = a
self.b = b
# Area method
def get_area(self):
return self.a * self.b
# Usage example
r = Rectangle(4, 5)
print(r.get_area())
>> 20
class RedRectangle(Rectangle):
self.color = 'red'
unittest - Python 組み込みのテスト自動化フレームワーク(Python と一緒に導入)。unittest - ユニットテスト専用ではない。unittest
Python 配布物に含まれる)pytest
test_ で始まるスクリプト・関数を探索サードパーティ(Python とは別途インストールが必要)
アサーションメソッドは少なめ
累乗演算子のテスト:
import unittest
# Declaring the TestCase class
class TestSquared(unittest.TestCase):
# Defining the test
def test_negative(self):
self.assertEqual((-3) ** 2, 9)
.assertEqual(), .assertNotEqual().assertTrue(), .assertFalse().assertIs(), .assertIsNone().assertIsInstance(), .assertIn().assertRaises()unittest - OOP ベースの Python 組み込みテスト自動化フレームワークunittest におけるテスト単位unittest.TestCase を継承するクラスを定義Pythonによるテスト入門