Python のassertEqual() は、単体テストで 2 つの値が等しいかどうかを確認するために使用される単体テスト ライブラリ関数です。この関数は 3 つのパラメータを入力として受け取り、アサート条件に応じてブール値を返します。両方の入力値が等しい場合、assertEqual() は true を返し、それ以外の場合は false を返します。
構文: assertEqual(firstValue, SecondValue, メッセージ)
パラメーター: assertEqual() は、以下に説明とともにリストされている 3 つのパラメータを受け入れます。
firstValue 関数による比較で使用される任意の型の変数 SecondValue : 関数による比較で使用される任意の型の変数 message : テストケースが失敗したときに表示されるメッセージとしての文字列文。
以下にリストされているのは、指定された Assert 関数の肯定的なテスト ケースと否定的なテスト ケースを示す 2 つの異なる例です。
例 1: ネガティブなテスト ケース
Python3
# unit test case> import> unittest> > class> TestStringMethods(unittest.TestCase):> > # test function to test equality of two value> > def> test_negative(> self> ):> > firstValue> => 'geeks'> > secondValue> => 'gfg'> > # error message in case if test case got failed> > message> => 'First value and second value are not equal !'> > # assertEqual() to check equality of first & second value> > self> .assertEqual(firstValue, secondValue, message)> > if> __name__> => => '__main__'> :> > unittest.main()> |
>
>
出力:
C++での継承
F ====================================================================== FAIL: test_negative (__main__.TestStringMethods) ---------------------------------------------------------------------- Traceback (most recent call last): File 'p1.py', line 12, in test_negative self.assertEqual(firstValue, secondValue, message) AssertionError: 'geeks' != 'gfg' - geeks + gfg : First value and second value are not equal! ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (failures=1)>
例 2: ポジティブなテストケース
Python3
# unit test case> import> unittest> > class> TestStringMethods(unittest.TestCase):> > # test function to test equality of two value> > def> test_positive(> self> ):> > firstValue> => 'geeks'> > secondValue> => 'geeks'> > # error message in case if test case got failed> > message> => 'First value and second value are not equal !'> > # assertEqual() to check equality of first & second value> > self> .assertEqual(firstValue, secondValue, message)> > if> __name__> => => '__main__'> :> > unittest.main()> |
>
>
出力:
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK>
参照 : https://docs.python.org/3/library/unittest.html