When automated testing is complete, we need a beautiful and easy-to-understand test report to demonstrate the results of automated testing, just a simple log file is not enough
Htmltestrunner is an extension of the Python standard library UnitTest unit Testing framework, which generates an easy-to-use HTML test report that is downloaded and copied to the Python installation directory.
For example, Windows, placed under the \python27\lib directory
Additional knowledge:
1. Python annotations
Common annotations are denoted by #
Text comments, placed below the class or method: "" "" "" "" "or" ""
2. The test report is named after the test time to prevent the report from being overwritten.
Time.time (): Gets the current timestamp
Time.ctime (): String form of the current time
Time.localtime (): The Struct_time form of the current time
Time.strftime (): Used to get the current time, can format the instructor time as a string
Here is the project integration generate test report source code:
Two test cases under the Test_case file
test_baidu.py
#-*-Coding:utf-8-*-from Selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.c Ommon.keys Import keysfrom selenium.webdriver.support.ui import selectfrom selenium.common.exceptions Import Nosuchelementexceptionfrom selenium.common.exceptions Import Noalertpresentexceptionimport unittest, time, Reclass Baidu (unittest. TestCase): def setUp (self): Self.driver = Webdriver. Firefox () self.driver.implicitly_wait () Self.base_url = "https://www.baidu.com/" Self.verificatione Rrors = [] Self.accept_next_alert = True def test_baidu (self): U "" "" "Baidu Search Use Case" "" Driver = Self.dri Ver driver.get (self.base_url + "/?tn=98012088_5_dg&ch=12") driver.find_element_by_id ("kw"). Clear () driver.find_element_by_id ("kw"). Send_keys ("select") driver.find_element_by_id ("su"). Click () time.s Leep (3) def tearDown (self): Self.driver.quit () if __name__ = = "__main__ ": Unittest.main ()
test_firefox.py
#-*-Coding:utf-8-*-from Selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.c Ommon.keys Import keysfrom selenium.webdriver.support.ui import selectfrom selenium.common.exceptions Import Nosuchelementexceptionfrom selenium.common.exceptions Import Noalertpresentexceptionimport unittest, time, Reclass FireFox (unittest. TestCase): def setUp (self): Self.driver = Webdriver. Firefox () self.driver.implicitly_wait () Self.base_url = "http://start.firefoxchina.cn/" Self.verifi Cationerrors = [] Self.accept_next_alert = True def test_firefox (self): U "" "" Firefox Search use case "" "D river = Self.driver Driver.get (self.base_url + "/") driver.find_element_by_id ("Search-key"). Clear () d river.find_element_by_id ("Search-key"). Send_keys ("Selenium Webdriver") driver.find_element_by_id ("Search-submit") ). Click () Time.sleep (5) def tearDown (self): Self.driver.quit () if __name__ = = "__main__": Unittest.main ()
Execute the run_test.py of the test case
#coding =utf-8import unittest, doctestimport htmltestrunnerimport time# relative path define the path and report storage path of the use Case test_dir= './test_case ' Test_dir1= './report '
#查找对应路径下的测试用例放到discover中discover =unittest.defaulttestloader.discover (test_dir,pattern= ' test*.py ') # Define a report with the current test time to prevent previous reports from being overwritten now=time.strftime ("%y-%m-%d%h_%m_%s") filename=test_dir1+ '/' +now+ ' result.html ' #二进制打开, Ready to write to file fp = file (filename, ' WB ') #定义测试报告runner =htmltestrunner.htmltestrunner (stream=fp,title=u ' Search test report ', description= U ' use case execution ') Runner.run (Discover)
The final generated HTML test report is as follows:
Selenium+python (Generate HTML test report)