Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python unittest – assertNotAlmostEqual() function

  • Last Updated : 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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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 suite
import 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!