Assert in testcases

 

Unit Testing in Python – Unittest

What is Unit Testing?
Unit Testing is the first level of software testing where the smallest testable parts of a software are tested. This is used to validate that each unit of the software performs as designed.
 
import unittest
  
class SimpleTest(unittest.TestCase):
  
    # Returns True or False. 
    def test(self):        
        self.assertTrue(True)
  
if __name__ == '__main__':
    unittest.main()
 

assertEqual() function

 assertEqual() in Python is a unittest library function that is used in unit testing to check the equality of two values. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both input values are equal assertEqual() will return true else return false.
 
 
 Syntax: assertEqual(firstValue, secondValue, message)
 
 
 
import unittest
  
class TestStringMethods(unittest.TestCase):

    def test_negative(self):
        V1 = "hello"
        V2 = "hello"
      
        message = "First value and second value are equal !"
   
        self.assertEqual(V1,V2, message)
  
if __name__ == '__main__':
    unittest.main()
 
 

assertTrue() function

assertTrue() in Python is a unittest library function that is used in unit testing to compare test value with true. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is true then assertTrue() will return true else return false.

 
 
import unittest
 
class TestStringMethods(unittest.TestCase):

    def test_positive(self):
        testValue = True
        message = "Test value is not true."
 
        self.assertTrue( testValue, message)
 
if __name__ == '__main__':
    unittest.main()
 
 
 

assertNotEqual() function

assertNotEqual() in Python is a unittest library function that is used in unit testing to check the inequality of two values. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both input values are unequal assertNotEqual() will return true else return false.

 
 
import unittest
  
class TestStringMethods(unittest.TestCase):

    def test_positive(self):
        first_Value = "geeks"
        second_Value = "geeks"

        message = "First value and second value are not unequal !"

        self.assertNotEqual(first_Value, second_Value, message)
  
if __name__ == '__main__':
    unittest.main()
 
 
 
 

assertFalse() function

assertFalse() in Python is a unittest library function that is used in unit testing to compare test value with false. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is false then assertFalse() will return true else return false.

import unittest
  
class TestStringMethods(unittest.TestCase):

    def test_positive(self):
        test_Value = False
    
        message = "Test value is not false."
     
        self.assertFalse( test_Value, message)
  
if __name__ == '__main__':
    unittest.main()

 

assertAlmostEqual() function

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.

 

import unittest
  
  
class TestStringMethods(unittest.TestCase):
 
    def test_positiveWithPlaces(self):
        first = 4.4555
        second = 4.4566
        decimal_Place = 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, decimal_Place, message)
  

    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()

Comments

Popular posts from this blog

Queue in Java

Using lists , tuples, sets, dictionaries

Blog on Pytest