Python+selenium之测试报告(2)
1 # -*- coding: utf-8 -*- 2 import HTMLTestReport 3 import HTMLTestRunner 4 import os 5 import sys 6 import time 7 import unittest 8 from selenium import webdriver 9 10 11 class Baidu(unittest.TestCase): 12 def setUp(self): 13 self.driver = webdriver.Firefox() 14 self.driver.implicitly_wait(30) 15 self.driver.maximize_window() 16 # self.base_url = "https://www.baidu.com" 17 # self.driver.get(self.base_url) 18 self.driver.get("https://www.baidu.com") 19 20 def test_case1(self): 21 """设计测试失败case""" # *****效果是在测试报告中显示显示出测试名称***** 22 print("========【case_0001】打开百度搜索 =============") 23 # current_time = time.strftime("%Y-%M-%D-%H-%M-%S", time.localtime(time.time())) 24 # "."表示创建的路径为当.py文件所处的地址,\\是用\将“\”转义 25 # pic_path = '.\\result\\image\\' + current_time + '.png' 26 current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) 27 pic_path = '.\\result\\image\\' + '2017-07-17\\' + current_time + '.png' 28 print(pic_path) # 打印图片的地址 29 time.sleep(2) 30 self.driver.save_screenshot(pic_path) # 截图,获取测试结果 31 self.assertEqual('百度一下,你就知道', self.driver.title) # 断言判断测试是否成功,判断标题是否为百度(设计失败的case) 32 33 def test_case2(self): 34 """设计测试过程中报错的case""" 35 print("========【case_0002】搜索selenium =============") 36 self.driver.find_element_by_id("kw").clear() 37 self.driver.find_element_by_id("kw").send_keys(u"selenium") 38 self.driver.find_element_by_id('su').click() 39 time.sleep(2) 40 # current_time = time.strftime("%Y-%M-%D-%H-%M-%S", time.localtime(time.time())) 41 current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) 42 # "."表示创建的路径为当.py文件所处的地址,\\是用\将“\”转义 43 pic_path = '.\\result\\image\\'+'2017-07-17\\' + current_time + '.png' 44 print(pic_path) # 打印图片的地址 45 time.sleep(2) 46 self.driver.save_screenshot(pic_path) # 截图,获取测试结果 47 self.assertIn('selenium', self.driver.title) # 断言书写错误,导致case出错 48 49 def test_case3(self): 50 """设计测试成功的case""" 51 print("========【case_0003】 搜索梦雨情殇博客=============") 52 self.driver.find_element_by_id("kw").clear() 53 self.driver.find_element_by_id("kw").send_keys(u"梦雨情殇") 54 self.driver.find_element_by_id('su').click() 55 # current_time = time.strftime("%Y-%M-%D-%H-%M-%S", time.localtime(time.time())) 56 current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) 57 # "."表示创建的路径为当.py文件所处的地址,\\是用\将“\”转义 58 pic_path = '.\\result\\image\\2017-07-17\\' + current_time + '.png' 59 print(pic_path) # 打印图片的地址 60 time.sleep(2) 61 self.driver.save_screenshot(pic_path) # 截图,获取测试结果 62 63 self.assertIn('梦雨情殇', self.driver.title) 64 65 def tearDown(self): 66 self.driver.quit() 67 68 69 if __name__ == "__main__": 70 '''生成测试报告''' 71 current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) 72 testunit = unittest.TestSuite() # 定义一个单元测试容器 73 testunit.addTest(Baidu("test_case1")) #将测试用例加入到测试容器内 74 testunit.addTest(Baidu("test_case2")) 75 testunit.addTest(Baidu("test_case3")) 76 report_path = ".\\result\\SoftTestReport_" + current_time + '.html' # 生成测试报告的路径 77 fp = open(report_path, "wb") 78 runner = HTMLTestReport.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description='自动化测试演示报告', tester='fyr') 79 # runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description='自动化测试演示报告') 80 runner.run(testunit) 81 fp.close()
注意事项:
1.获取当前时间的格式为:
%Y-%m-%d-%H_%M_%S 而不是 %Y-%M-%D-%H-%M-%S
2.填写的截图存放地址“\”,要用转义字符“\”进行转义,变为“\\”
3.runner = HTMLTestReport.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description='自动化测试演示报告', tester='fyr')的测试报告如图所示:
4.runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description='自动化测试演示报告') 的效果图如图所示:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」