Selenium2+python automation 54-unittest generates test reports (HTMLTestRunner) Selenium2+python automation 53-unittest batch execution (discover)

foreword

After batch execution of use cases, the generated test report is in text form, which is not intuitive enough. In order to better display the test report, it is better to generate HTML format.

It is not possible to generate reports in html format in unittest, and you need to import a third-party module: HTMLTestRunner

 

1. Import HTMLTestRunner

1. The download of this module cannot be installed through pip. It can only be imported manually after downloading. Download address: http://tungwaiyip.info/software/HTMLTestRunner.html

2. The HTMLTestRunner.py file under Download is the package we need to download.

3. After downloading, manually drag it to the Lib directory of the python installation file

 

Second, demo analysis

1. Download the second file test_HTMLTestRunner.py under Download. This is an official test demo. From this file, you can find the usage of the module.

2. Find the paragraph below, which is a demo given by the official. The upper part of test_main() is to load the test case, we don't need to make it so complicated.

Refer to the previous content on the line Selenium2+python automation 53-unittest batch execution (discover)

3. The core code is the red area below, which is the focus of this article.

 

3. Generate html report

1. We only need to copy the above red area code to the previous article and make some modifications. There are three main parameters here:

--stream: The storage area where the test report is written to the file

--title: The title of the test report

--description: the description of the test report

2. report_path is the address where the test report is stored

 

4. Details of the test report

1. Find the test report file, open it with a browser, and click Detail in the View to view the detailed description.

2. In order to generate test cases with Chinese descriptions, you can add comments to the case, such as adding the following comments to the script of test_01:

class Test(unittest.TestCase):
    def setUp(self):
        print "start!"

    def tearDown(self):
        time.sleep(1)
        print "end!"

    def test01(self):
        u'''Test login case, Account: xx password xx'''
        print "execute test case 01"

    def test03(self):
        u'''test login search case, keyword: xxx'''
        print "execute test case 03"

3. View the test report after re-run

 

5. Reference code:

1. I use the relative path for the code file path below, so as to avoid the situation where the code cannot find the path by changing the address

# coding:utf-8
import unittest
import os
import HTMLTestRunner

# If python2.7 reports encoding problems, add these three lines, python3 does not need to add

import sys
reload(sys)
sys.setdefaultencoding('utf8')

# 用例路径
case_path = os.path.join(os.getcwd(), "case")
# 报告存放路径
report_path = os.path.join(os.getcwd(), "report")
def all_case():
    discover = unittest.defaultTestLoader.discover(case_path,
                                                    pattern="test*.py",
                                                    top_level_dir=None)
    print(discover)
    return discover

if __name__ == "__main__":
    # runner = unittest.TextTestRunner()
    # runner.run(all_case())

    # html报告文件路径
    report_abspath = os.path.join(report_path, "result.html")
    fp = open(report_abspath, "wb")
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
                                           title=u'Automated test report, the test results are as follows:',
                                           description=u'Use case execution:')

    # Call the add_case function to return the value
    runner.run(all_case())
    fp.close()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339247&siteId=291194637