Python unittest - assertAlmostEqual() function - GeeksforGeeks

29 Aug, 2020
assertAlmostEqual() in Python is a unittest library function that is used in unit testing to check whether two given values are almost equal or not. This function will take five parameters as input and return a boolean value depending upon the assert condition. 
This function check that first and second are approximately equal by computing the difference, rounding to the given number of decimal places (default 7), and comparing to zero.
If delta is supplied instead of places then the difference between first and second must be less or equal to delta.
Syntax: assertAlmostEqual(first, second, places=7, message=None, delta=None)
Parameters: assertAlmostEqual() accept three parameters which are listed below with explanation:
first: first input value (integer)
second:  second input value (integer)
places: decimal places for approximation
message: a string sentence as a message which got displayed when the test case got failed.
delta: delta value for approximation
Listed below are two different examples illustrating the positive and negative test case for given assert function:
Example :
Python3
# test suiteimport unittest    class TestStringMethods(unittest.TestCase):    # negative test function to test if values are almost equal with place    def test_negativeWithPlaces(self):        first = 4.4555        second = 4.4566        decimalPlace = 3        # error message in case if test case got failed        message = "first and second are not almost equal."        # assert function() to check if values are almost equal        self.assertAlmostEqual(first, second, decimalPlace, message)      # positive test function to test if values are almost equal with place    def test_positiveWithPlaces(self):        first = 4.4555        second = 4.4566        decimalPlace = 2        # error message in case if test case got failed        message = "first and second are not almost equal."        # assert function() to check if values are almost equal        self.assertAlmostEqual(first, second, decimalPlace, message)      # negative test function to test if values are almost equal with delta    def test_negativeWithDelta(self):        first = 4.4555        second = 4.4566        delta = 0.0001        # error message in case if test case got failed        message = "first and second are not almost equal."        # assert function() to check if values are almost equal        self.assertAlmostEqual(first, second, None, message, delta)      # positive test function to test if values are almost equal with delta    def test_positiveWithDelta(self):        first = 4.4555        second = 4.4566        delta = 0.002        # error message in case if test case got failed        message = "first and second are not almost equal."        # assert function() to check if values are almost equal        self.assertAlmostEqual(first, second, None, message, delta)    if __name__ == '__main__':    unittest.main()
Output:
FF..
======================================================================
FAIL: test_negativeWithDelta (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/2c36bbd2c940a6c7b81a901ebfdd2ad8.py", line 34, in test_negativeWithDelta
    self.assertAlmostEqual(first, second, None, message, delta)
AssertionError: 4.4555 != 4.4566 within 0.0001 delta : first and second are not almost equal.

======================================================================
FAIL: test_negativeWithPlaces (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/2c36bbd2c940a6c7b81a901ebfdd2ad8.py", line 14, in test_negativeWithPlaces
    self.assertAlmostEqual(first, second, decimalPlace, message)
AssertionError: 4.4555 != 4.4566 within 3 places : first and second are not almost equal.

----------------------------------------------------------------------
Ran 4 tests in 0.001s

FAILED (failures=2)


Reference: https://docs.python.org/3/library/unittest.html
Python unittest-library
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Create a Pandas DataFrame from Lists
Reading and Writing to text files in Python