Objective
The previous article has introduced the report generation method, this small section optimizes the test report, makes the test report facilitates most reading. While we added comments for each use case while we were developing the test case, the test report was generally read to non-testers, and if you could add a description for each test case in the report, it would make the report easier to read.
The following still take Baidu as an example:
First, open our test case file and add a comment below each test case (method), as follows:
#coding =utf-8
From selenium import Webdriver
From selenium.webdriver.common.by Import by
From Selenium.webdriver.common.keys import keys
From Selenium.webdriver.support.ui import Select
From selenium.common.exceptions import nosuchattributeexception
Import Unittest,time,re
Import Htmltestrunner
Class Baidu (UnitTest. TestCase):
def setUp (self):
Self.driver = Webdriver. Firefox ()
Self.driver.implicitly_wait (30)
Self.base_url = "http://www.baidu.com/"
Self.verificationerrors = []
Self.accept_next_alert = True
def test_baidu_search (self):
U "" "Baidu Search" ""
Driver = Self.driver
Driver.get (Self.base_url + '/')
driver.find_element_by_id ("kw"). Send_keys ("Selenium Webdriver")
driver.find_element_by_id ("su"). Click ()
Time.sleep (2)
Driver.close ()
def test_baidu_set (self):
U "" "Baidu Set" ""
Driver = Self.driver
#进入搜索设置页
Driver.get (Self.base_url + '/gaoji/preferences.html ')
#设置每页搜索结果为 20 Records
M=driver.find_element_by_name ("NR")
M.find_element_by_xpath ("//option[@value = ']"). Click ()
Time.sleep (2)
#保存设置的信息
Driver.find_element_by_xpath ("/html/body/form/div/input"). Click ()
Time.sleep (2)
Driver.switch_to_alert (). Accept ()
Driver.close ()
if __name__ = = "__main__":
Unittest.main ()
To open a test report:
Small u is to avoid the garbled problem caused by Chinese.
Is it very readable to add Chinese notes? Everyone is free to toss it.
Second, the report file name takes the current time
Each time you run the test before you manually modify the name of the report, if there are changes to the previous report will be overwritten, this will show a lot of trouble, then there is no way to make the report name of each generation is different, in order to better score report can add the current time in the report, This makes it very easy for us to find the report generated at a certain time of day.
Here's a change to the script that executes the test case, as follows:
#coding =utf-8
Import UnitTest
Import time
#这里需要导入测试文件
Import Baidu
Import Htmltestrunner
Testunit=unittest. TestSuite ()
Testunit.addtest (Unittest.makesuite (Baidu). Baidu)) #将测试用例加入到测试容器 (Kit)
Now=time.strftime ("%y-%m-%d-%h_%m_%s", Time.localtime (Time.time ())) #获取当前时间
Filename= "d:\\python\\report\\" + Now + "result.html" #定义个报告存放路径, supporting relative paths. Add the current time to the report
fp = open (filename, "WB")
Runner =htmltestrunner.htmltestrunner (stream=fp,title=u ' Test report ', Description=u ' use case execution: ')
Runner.run (Testunit) #执行测试用例
Time.time () gets the current timestamp
Time.localtime () struct_time form of the current time
Rerun all test cases to view the file name of the generated test report,
The file name is named after the previous run time, so it's easy to find the report you want, and you don't have to modify the name of the report manually before you run the test.
Selenium2+python Automation 32-readability of test reports "reproduced"