Ads

Python 9: Save test results in Html files, for human readable format

Hello Friends,

Good morning, afternoon and evening what ever zone you are in.

Myself chakrapani upadhyaya, a full stack engineer.

In this article we will learn that how to Save test results in Html files, for human readable format.


We use HtmlTestRunner to generate the report in human readable format.

What is HtmlTest runner?

HtmlTest runner is a unittest test runner that save test results in Html files, for human readable presentation of results.

 First of all we need to install the library to generate the report for our unit test cases

pip install html-testRunner



Open Visual Studio code, create the file called mathOperation.py

lets create the class MathOperation and add method Addition in it.

class MathOperation:

    def Addition(self,num1, num2):
        return num1 + num2

Next we will create the test file "math_test.py"

Then, We need to import HtmlTestRunner from package, then pass it to unittest.main with the testRunner keyword. Tests will be saved under a reports/ directory by default (the output kwarg controls this.).

unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner())

Complete Source Code here..

#importing the unittest library
import unittest
#import the math file
import mathOperation
#import HtmlTestRunner Library
import HtmlTestRunner

#create the class file called MathOperationTest
#and inherit the unit test case class in it
class MathOperationTest(unittest.TestCase):

    #test case for addition
    def test_addition(self):

        #creating the object for MathOperation class, 
        #access all the methods and properties in it
        mathObj = mathOperation.MathOperation()

        num1=10
        num2=20

        expectedResult=20

        actualResult=mathObj.Addition(num1,num2)

        self.assertEqual(actualResult,expectedResult)

if __name__=="__main__":
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner())

Lets run the test file

python .\math_test.py

Output is


Go to root directory of your application, then you will find the report folder created in it. Their you will see the html report generated


Here is the output of HTML file



That's it for now, hope you enjoyed this article.

See you in next article until then take care bye....

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !