Unit Test Example Code With Python:

I have done unit testing for the triangle function which finds if it is Scalene, Isoscalene or Equilateral. I have used Python here. IDLE is best IDE here to run this efficiently.

#in the result . dot means pass testcases and fail means failed testcases
import unittest

def triangle(lenA,lenB,lenC): #function unit which will be tested
    if(lenA and lenB and lenC):
        if(lenA>0 and lenB>0 and lenC>0):
             if (isinstance(lenA,int) and isinstance(lenB,int) and isinstance(lenC,int)):
                if lenA == lenB == lenC:
                    return "Equilateral"
                elif lenA==lenB or lenB==lenC or lenC == lenA:
                    return "Isoscalene"
                else:
                    return 'Scalene'
             else:
                return "Not integer"
        else:
            return "Error, values must be >0"
    else:
        return "Three values must be given"
        
class testingTriangle(unittest.TestCase):

    
    def test_trianglefunc_1(self):
        print("\nTest-1 called..")
        #Arrange= arrange the prerequisite
        a=10
        b=10
        c=10
            
        #Act= run the test
        result=triangle(a,b,c)

        #Assert= verify the test
        self.assertEqual('Equilateral', result)

    def test_trianglefunc_2(self):
        print("\nTest-2 called..")
        #Arrange= arrange the prerequisite
        a=3
        b=3
        c=4
        
        #Act= run the test
        result=triangle(a,b,c)

        #Assert= verify the test
        self.assertEqual('Scalene', result)

    def test_trianglefunc_3(self):
        print("\nTest-3 called..")
        #Arrange= arrange the prerequisite
        a=2.5
        b=7.5
        c=8.7
        
        #Act= run the test
        result=triangle(a,b,c)

        #Assert= verify the test
        self.assertEqual('Integer', result)

    def test_trianglefunc_4(self):
        print("\nTest-4 called..\n")
        #Arrange= arrange the prerequisite
        a=-1
        b=-1
        c=-1
        
        #Act= run the test
        result=triangle(a,b,c)

        #Assert= verify the test
        self.assertEqual('Triangle', result)

    def test_trianglefunc_5(self):
        print("\nTest-5 called..")
        #Arrange= arrange the prerequisite
        a=0
        b=0
        c=0
        
        #Act= run the test
        result=triangle(a,b,c)

        #Assert= verify the test
        self.assertEqual('Triangle', result)

    def test_trianglefunc_6(self):
        print("\nTest-6 called..")
        #Arrange= arrange the prerequisite
        a=1
        b=1
        c=1
        
        #Act= run the test
        result=triangle(a,b,c)

        #Assert= verify the test
        self.assertEqual('Equilateral', result)

    def test_trianglefunc_7(self):
        print("\nTest-7 called..")
        #Arrange= arrange the prerequisite
        a=12
        b=NULL
        c=NULL
        
        #Act= run the test
        result=triangle(a,b,c)

        #Assert= verify the test
        self.assertRaises('Errors', result)


if __name__== "__main__":
    unittest.main()



Some references might be helpful:
Ref:https://www.youtube.com/watch?v=DX989F80MnU
https://docs.python.org/3/library/unittest.html

Very short but precise:https://www.youtube.com/watch?v=HKTyOUx9Wf4
Regarding assertions: https://www.youtube.com/watch?v=TKDnkQSyg2M
Regarding exceptions: https://www.youtube.com/watch?v=LxbiAHGkPhk
It is also good: https://www.javatpoint.com/python-unit-testing

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *