Objective
After executing the use case in bulk, the generated test report is text-less intuitive and, in order to better demonstrate the test report, it is best to generate HTML format.
UnitTest inside is unable to generate HTML format report, need to import a third-party module: Htmltestrunner
First, import Htmltestrunner
1. This module download can not be installed through PIP, can only be downloaded and then manually imported: http://tungwaiyip.info/software/HTMLTestRunner.html
2.Download The htmltestrunner.py file is the package we need to download.
3. Manually drag to the Lib directory of the Python installation file after downloading
Second, demo analysis
1. Download the second file under Download test_htmltestrunner.py, this is the official test demo, from which you can find the usage of the module.
2. Find this paragraph, is the official demo, Test_main () in the upper part of the load test case, we do not need to complicate.
Refer to the previous article on the line Selenium2+python automated 53-unittest batch execution (Discover)
3. The core code is the following red area, this is the focus of this article.
Iii. Generating HTML reports
1. We only need to copy the above red area code to the previous article on the basis of a slight modification can be, here are mainly three parameters:
--stream: The storage area where the test report is written to the file
--title: The subject of the test report
--description: Description of the test report
2.report_path is the address where the test report is stored
Iv. details of the test report
1. Locate the test report file, open it in a browser, and click on the detail in the view to view the details description.
2. In order to generate test cases with a Chinese description, you can add comments in the case, such as the following comments in the test_01 script:
Class Test (UnitTest. TestCase):
def setUp (self):
Print "start!"
def tearDown (self):
Time.sleep (1)
Print "end!"
def test01 (self):
U ' Test login use case, account: xx password xx ' '
Print "Execute test Case 01"
def test03 (self):
U ' Test log search use case, keyword: xxx '
Print "Execute test case 03"
3. Review the test report after re-running
Five, reference code:
1. I use the relative path of the following code file path, so as to prevent the code from changing the address can not find the path of the case
# Coding:utf-8
Import UnitTest
Import OS
Import Htmltestrunner
# python2.7 If the coding problem, add these three lines, python3 do not add
Import Sys
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ')
# Use Case Path
Case_path = Os.path.join (OS.GETCWD (), "case")
# Report Storage Path
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 File path
Report_abspath = Os.path.join (Report_path, "result.html")
fp = open (Report_abspath, "WB")
Runner = Htmltestrunner.htmltestrunner (STREAM=FP,
Title=u ' Automated test Report, test results are as follows: ',
Description=u ' use case execution: ')
# call Add_case function return value
Runner.run (All_case ())
Fp.close ()
Selenium2+python Automated 54-unittest Generation Test report (Htmltestrunner)