Python3 ユニットテスト assert関数編
assert関数についてのメモ
assert関数の代表的な使い方について纏めてみた。
使い方 | 検査内容 |
---|---|
assertEqual(a , b) | a == b |
assertNotEqual(a , b) | a != b |
assertTrue(x) | x == True |
assertFalse(x) | x == False |
実際にテストコードを書いてみた。
import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): """str.upper()メソッドが大文字アルファベットを返すかテスト.""" self.assertEqual('foo'.upper(), 'FOO') def test_not_upper(self): """str.upper()メソッドが文字列をそのまま返さないかをテスト.""" self.assertNotEqual('foo'.upper(), 'foo') def test_is_digit(self): """str.isdigit()メソッドが全て数字の文字列の場合はTrueを返すかテスト.""" self.assertTrue('123'.isdigit()) def test_is_not_digit(self): """str.isdigit()メソッドが数字以外の文字が混ざった文字列の場合はFalseを返すかテスト.""" self.assertFalse('123ppap'.isdigit())
実行結果
.... ---------------------------------------------------------------------- Ran 4 tests in 0.000s OK