Python unittest - assertNotAlmostEqual() function - GeeksforGeeks

29 Aug, 2020
assertNotAlmostEqual() in Python is a unittest library function that is used in unit testing to check whether two given values are not approximately. 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 not 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:
assertNotAlmostEqual(first, second, places=7, message=None, delta=None)
Parameters: assertNotAlmostEqual() 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 not-equal with place    def test_negativeWithPlaces(self):        first = 4.4555        second = 4.4566        decimalPlace = 2        # error message in case if test case got failed        message = "first and second are almost equal."        # assert function() to check if values are almost not-equal        self.assertNotAlmostEqual(first, second, decimalPlace, message)      # positive test function to test if values are almost not-equal with place    def test_positiveWithPlaces(self):        first = 4.4555        second = 4.4566        decimalPlace = 3        # error message in case if test case got failed        message = "first and second are almost equal."        # assert function() to check if values are almost not-equal        self.assertNotAlmostEqual(first, second, decimalPlace, message)      # negative test function to test if values are almost not-equal with delta    def test_negativeWithDelta(self):        first = 4.4555        second = 4.4566        delta = 0.002        # error message in case if test case got failed        message = "first and second are almost equal."        # assert function() to check if values are almost equal        self.assertNotAlmostEqual(first, second, None, message, delta)      # positive test function to test if values are almost not-equal with delta    def test_positiveWithDelta(self):        first = 4.4555        second = 4.4566        delta = 0.0001        # error message in case if test case got failed        message = "first and second are almost equal."        # assert function() to check if values are almost not-equal        self.assertNotAlmostEqual(first, second, None, message, delta)    if __name__ == '__main__':    unittest.main()
Output:
FF..
======================================================================
FAIL: test_negativeWithDelta (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ebb259873a4edc722a83da1a694e0ce1.py", line 34, in test_negativeWithDelta
    self.assertNotAlmostEqual(first, second, None, message, delta)
AssertionError: 4.4555 == 4.4566 within 0.002 delta : first and second are almost equal.

======================================================================
FAIL: test_negativeWithPlaces (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ebb259873a4edc722a83da1a694e0ce1.py", line 14, in test_negativeWithPlaces
    self.assertNotAlmostEqual(first, second, decimalPlace, message)
AssertionError: 4.4555 == 4.4566 within 2 places : first and second are 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
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Python OOPs Concepts
Python | Get unique values from a list
Check if element exists in list in Python
Python Classes and Objects
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Python | Pandas dataframe.groupby()
Create a directory in Python