logo

Python unittest – funkcija assertEqual().

assertEqual() v Pythonu je funkcija knjižnice unittest, ki se uporablja pri testiranju enote za preverjanje enakosti dveh vrednosti. Ta funkcija bo kot vhod vzela tri parametre in vrnila logično vrednost, odvisno od potrditvenega pogoja. Če sta obe vhodni vrednosti enaki, bo assertEqual() vrnil true sicer pa vrnil false.

Sintaksa: assertEqual(firstValue, secondValue, message)



Parametri: assertEqual() sprejme tri parametre, ki so navedeni spodaj z razlago:

    firstValue spremenljivka katere koli vrste, ki se uporablja pri primerjavi s funkcijo secondValue : spremenljivka katere koli vrste, ki se uporablja pri primerjavi s funkcijo sporočilo : nizovni stavek kot sporočilo, ki se prikaže, ko testni primer ni uspel.

Spodaj sta navedena dva različna primera, ki ponazarjata pozitivni in negativni testni primer za dano funkcijo trditve:

Primer 1: Negativni testni primer



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

>

>

Izhod:

java bool v niz
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)>

Primer 2: Pozitiven testni primer

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

>

>

Izhod:

. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK>

Referenca : https://docs.python.org/3/library/unittest.html