1.selenium+HtmlTestRunner模块安装导入
详情见:https://blog.csdn.net/ezreal_tao/article/details/80794045
2.对应版本 谷歌浏览器和谷歌驱动chromedriver.exe的下载(不适用对应版本会报错,无法调用浏览器)
详情见:https://blog.csdn.net/ezreal_tao/article/details/80808729
3.代码执行成功后,生成Html功能自动化测试报告
import os import sys import unittest import time from selenium import webdriver from HTMLTestRunner import HTMLTestRunner sys.path.append('E:/Py-workspace/test/testUI.py') ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__))) class MyTest(unittest.TestCase): driver = webdriver.Chrome() def setUp(self): pass def test_case1(self): """打开百度网页""" self.driver.get("https://www.baidu.com/") assert "百度一下,你就知道" in self.driver.title def test_case2(self): """搜索功能""" self.driver.get("https://www.baidu.com/") self.driver.find_element_by_id("kw").send_keys("taozhen") self.driver.find_element_by_id("su").click() def test_quit(self): """关闭浏览器""" self.driver.quit() def tearDown(self): # self.driver.quit() # self.driver.refresh() # 将退出浏览器的操作变成刷新浏览器,用于不同用例之间的接洽操作 pass if __name__ == '__main__': # 构造测试集 suite = unittest.TestSuite() # 构造测试集 suite.addTest(MyTest("test_case1")) # 加入测试用例 suite.addTest(MyTest("test_case2")) suite.addTest(MyTest("test_quit")) # 执行测试 date = time.strftime("%Y%m%d") # 定义date为日期,time为时间 time = time.strftime("%Y%m%d_%H%M%S") path = "./report/ui/" # 判断是否定义的路径目录存在,不能存在则创建 if not os.path.exists(path): os.makedirs(path) else: pass report_path = path+time+"UIreport.html" # 将运行结果保存到report,名字为定义的路径和文件名,运行脚本 report_title = u"测试报告" desc = u'功能自动化测试报告详情:' with open(report_path, 'wb') as report: runner = HTMLTestRunner(stream=report, title=report_title, description=desc) runner.run(suite) # 关闭report,脚本结束 report.close()
生成的Html测试报告如下