logo

Python 단위 테스트 - AssertEqual() 함수

Python의 AssertEqual()은 단위 테스트에서 두 값의 동등성을 확인하는 데 사용되는 단위 테스트 라이브러리 함수입니다. 이 함수는 세 개의 매개변수를 입력으로 사용하고 어설션 조건에 따라 부울 값을 반환합니다. 두 입력 값이 모두 같으면 AssertEqual()은 true를 반환하고, 그렇지 않으면 false를 반환합니다.

통사론: 주장Equal(firstValue, secondValue, 메시지)



매개변수: AssertEqual()은 아래에 설명과 함께 나열된 세 가지 매개변수를 허용합니다.

    firstValue 함수에 의한 비교에 사용되는 모든 유형의 변수 secondValue : 함수에 의한 비교에 사용되는 모든 유형의 변수 message : 테스트 케이스가 실패했을 때 표시되는 메시지인 문자열 문장.

아래에는 주어진 Assert 함수에 대한 긍정적인 테스트 사례와 부정적인 테스트 사례를 보여주는 두 가지 다른 예가 나열되어 있습니다.

예 1: 부정적인 테스트 사례



파이썬3






# 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()>

>

>

산출:

세트 대 지도
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: 긍정적인 테스트 사례

파이썬3




# 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