Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – assertLess() function in unittest

  • Last Updated : 01 Nov, 2020

assertLess() in Python is an unittest library function that is used in unit testing to check whether the first given value is less than the second value or not. This function will take three parameters as input and return a boolean value depending upon the assert condition.

This function check that if the first given value is less than the second value and returns true if it is so, else returns false if the first value is not less than the second value.

 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

Syntax: assertLess(first, second, message=None) 



Parameters: assertLess() accept three parameters which are listed below with explanation:

  • first: first input value (integer)
  • second:  second input value (integer)
  • message: a string sentence as a message which got displayed when the test case got failed.

Listed below is an example illustrating the positive and negative test case for given assert function:

Example 1: If the first value is not less that second value.

Python3




# test suite
import unittest
  
class TestStringMethods(unittest.TestCase):
      
    # negative test function to test if
    # values1 is less than value2
    def test_negativeForLess(self):
        first = 6
        second = 5
          
        # error message in case if test case got failed
        message = "first value is not less that second value."
          
        # assert function() to check if values1 is
        # less than value2
        self.assertLess(first, second, message)
  
# main for function call
if __name__ == '__main__':
    unittest.main()

Output:

F
======================================================================
FAIL: test_negativeForLess (__main__.TestStringMethods)
———————————————————————-
Traceback (most recent call last):
 File “p1.py”, line 13, in test_negativeForLess
   self.assertLess(first, second, message)
AssertionError: 5 not less than 5 : first value is not less that second value.

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

FAILED (failures=1)

Example 2: If the first given value is less than the second value

Python




# test suite
import unittest
  
class TestStringMethods(unittest.TestCase):
      
    # positive test function to test if 
    # values are almost equal with place
    def test_positiveForLess(self):
        first = 2
        second = 3
          
        # error message in case if test case got failed
        message = "first value is not less that second value."
          
        # assert function() to check if values1 is
        # less than value2
        self.assertLess(first, second, message)
  
# main for function call
if __name__ == '__main__':
    unittest.main()

Output:

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

OK




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!