Thursday, September 4, 2025
HomeLanguagesPython unittest – assertEqual() function

Python unittest – assertEqual() function

assertEqual() in Python is a unittest library function that is used in unit testing to check the equality of two values. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both input values are equal assertEqual() will return true else return false.

Syntax: assertEqual(firstValue, secondValue, message)

Parameters: assertEqual() accept three parameter which are listed below with explanation:

  • firstValue  variable of any type which is used in the comparison by function
  • secondValue: variable of any type which is used in the comparison by function
  • message: a string sentence as a message which got displayed when the test case got failed.

Listed below are two different examples illustrating the positive and negative test case for given assert function:

Example 1: Negative Test case

Python3




# unit test case
import unittest
  
class TestStringMethods(unittest.TestCase):
    # test function to test equality of two value
    def test_negative(self):
        firstValue = "Lazyroar"
        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()


Output:

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: 'Lazyroar' != 'gfg'
- Lazyroar
+ gfg
 : First value and second value are not equal!

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

Example 2: Positive Test case

Python3




# unit test case
import unittest
  
class TestStringMethods(unittest.TestCase):
    # test function to test equality of two value
    def test_positive(self):
        firstValue = "Lazyroar"
        secondValue = "Lazyroar"
        # 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()


Output:

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

OK


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

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS