首页 > 代码库 > python自动化--批量执行测试之生成报告
python自动化--批量执行测试之生成报告
一、生成报告
1.先执行一个用例,并生成该用例的报告
# -*- 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 NoSuchElementException from selenium.common.exceptions import NoAlertPresentException import unittest, time, re,HTMLTestRunner class Baidy(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = "https://www.baidu.com" self.verificationErrors = [] self.accept_next_alert = True def test_baidy(self): u"""百度搜索""" driver = self.driver driver.get(self.base_url + "/") driver.find_element_by_id("kw").clear() 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"""百度设置""" driver = self.driver # 进入搜索设置页面 driver.get(self.base_url+‘/gaoji/preferences.html‘) menu = driver.find_element_by_id("nr") menu.find_element_by_xpath(".//option[@value=http://www.mamicode.com/‘20‘]").click() driver.find_element_by_id("save").click() driver.switch_to_alert().accept() time.sleep(2) driver.close() def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": # 定义一个单元测试容器 testunit = unittest.TestSuite() testunit.addTest(Baidy.test_baidy) testunit.addTest(Baidy.test_baidu_set) # 定义报告存放路径 filename = r‘E:\abc\web_testing\result.html‘ fp = file(filename, ‘wb‘) # 定义测试报告 runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title=u‘百度搜索测试报告‘, description=u‘用例执行情况:‘) # 运行测试用例 runner.run(testunit) fp.close()
2.批量执行测试用例,先构造两个用例
# -*- 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 NoSuchElementException from selenium.common.exceptions import NoAlertPresentException import unittest, time, re,HTMLTestRunner class Baidy(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = "https://www.baidu.com" self.verificationErrors = [] self.accept_next_alert = True def test_baidy(self): u"""百度搜索""" driver = self.driver driver.get(self.base_url + "/") driver.find_element_by_id("kw").clear() 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"""百度设置""" driver = self.driver # 进入搜索设置页面 driver.get(self.base_url+‘/gaoji/preferences.html‘) menu = driver.find_element_by_id("nr") menu.find_element_by_xpath(".//option[@value=http://www.mamicode.com/‘20‘]").click() driver.find_element_by_id("save").click() driver.switch_to_alert().accept() time.sleep(2) driver.close() def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": unittest.main()
# -*- 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 NoSuchElementException from selenium.common.exceptions import NoAlertPresentException import unittest, time, re class Youdao(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = "http://www.youdao.com" self.verificationErrors = [] self.accept_next_alert = True def test_youdao_search(self): u"""有道搜索""" driver = self.driver driver.get(self.base_url + "/") # driver.find_element_by_id("query").clear() # driver.find_element_by_id("query").send_keys(u"你好") # driver.find_element_by_id("qb").click() driver.find_element_by_id("translateContent").clear() driver.find_element_by_id("translateContent").send_keys(u"你好") driver.find_element_by_xpath(".//*[@id=‘form‘]/button").click() time.sleep(2) driver.close() def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": unittest.main()
并构造测试套件:根据套件来执行这两个文件的几条用例(baidu.py两条用例,youdao.py三条用例)
代码:
# -*- coding:utf-8 -*- import unittest import baidy,youdao import HTMLTestRunner import time testunit = unittest.TestSuite() # 将测试用例加入到测试容器(套件)中 testunit.addTest(unittest.makeSuite(baidy.Baidy)) testunit.addTest(unittest.makeSuite(youdao.Youdao)) # 执行测试套件 # runner = unittest.TextTestRunner() # runner.run(testunit) # 定义报告存放路径,支持相对路径 filename = r"E:\abc\web_testing\test_case\all_test.html" fp = file(filename, ‘wb‘) runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title=u‘百度搜索测试报告‘, description=u‘用例执行情况‘) # 执行测试用例 runner.run(testunit) fp.close()
注意,报告的基本格式如下:
# 定义报告存放路径,支持相对路径 filename = r"E:\abc\web_testing\test_case\all_test.html" fp = file(filename, ‘wb‘) runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title=u‘百度搜索测试报告‘, description=u‘用例执行情况‘) # 执行测试用例 runner.run(testunit) fp.close()
生成的报告如下:
二、报告文件取当前的时间
在all_test.py中引入time,即import time,几个time的常用函数:
time.time() 获取当前时间戳
time.localtime() 当前时间的 struct_time 形式
time.ctime() 当前时间的字符串形式
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
将all_test.py文件改为:
... # 取前面时间 now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) # 定义报告存放路径,支持相对路径 filename = r"E:\abc\web_testing\test_case"+ now +‘all_test.html‘ fp = file(filename, ‘wb‘) runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title=u‘百度搜索测试报告‘, description=u‘用例执行情况‘) ...
执行用例,即报错:
IOError: [Errno 22] invalid mode (‘wb‘) or filename: ‘E:\\abc\\web_testing\\test_case2016-12-21 19:43:20all_test.html‘
这是因为时间不能用“:”冒号
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
将它改成:
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
即可
以上内容感谢虫师的相关博客和书籍
python自动化--批量执行测试之生成报告
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。