Open In App

Python – assertGreater() function in unittest

Improve
Improve
Like Article
Like
Save
Share
Report

assertGreater() in Python is an unittest library function that is used in unit testing to check whether the first given value is greater 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 greater than the second value and returns true if it is so, else returns false if the first value is not greater than the second value.

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

Parameters: assertGreater() 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 a given assert function:

Example 1:

Python3




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


Output:

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

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

FAILED (failures=1)

Example 2:

Python




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


Output:

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

OK



Last Updated : 24 Oct, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads