低价网站建设教程,赣州企业网站建设,广州黄埔区网站建设,毕节金海湖新区城乡建设局网站unittest_生成测试报告
测试报告为测试结果的统计即展示#xff0c;是自动化测试不可或缺的一部分#xff0c;利用unittest 可以生成测试报告
方式一、使用第三方 HTMLTestRunner 执行测试用例集#xff0c;生成网页版测试报告#xff08;推荐#xff09;
HTMLTestRunn…unittest_生成测试报告
测试报告为测试结果的统计即展示是自动化测试不可或缺的一部分利用unittest 可以生成测试报告
方式一、使用第三方 HTMLTestRunner 执行测试用例集生成网页版测试报告推荐
HTMLTestRunner是Python标准库的unittest模块的一个扩展在使用该模块之前 要下载HTMLTestRunner.py文件并将该文件保存在python安装路径下的lib文件夹或者 是项目的子包中在python代码中通过 import HTMLTestRunner导入即可使用。
HTMLTestRunner常用参数介绍stream: 配置测试报告要保存的文件路径title测试报告标题description: 测试报告的描述信
代码示例
# 使用discover()来实现添加执行整个目录下所有的测试用例
import os
import unittest
import HTMLTestRunner
import time# 获取当前路径
curren_path os.path.dirname(__file__)
# 获取测试用例目录的路径
case_path os.path.join(curren_path,all_case)# 从当前路径回到上一层路径
# case_path os.path.abspath(os.path.dirname(os.path.dirname(__file__)))# 匹配测试用例路径下的所有的测试方法
discover unittest.defaultTestLoader.discover(start_dircase_path, # 用例路径pattern*_case*.py,top_level_dirNone) # 文件类型
# 创建主套件
main_suite unittest.TestSuite()
# 把测试用例路径添加到主套件中
main_suite.addTest(discover)
# 执行主套件里的测试用例如果要生成测试报告则不通过unittest.main()方法执行
# unittest.main(defaultTestmain_suite,verbosity2)# 执行并生成测试报告
# 加个时间戳
now time.strftime(%y-%m_%d_%H_%M_%S_,time.localtime(time.time()))
# 创建html类型测试报告对象将执行的过程写入到file_obj中
file_obj open(D:/pythonProject/P7_P8_Requests/unittest框架/test_report/{}test_report.html.format(now),w,encodingutf-8)
# 创建配置html测试报告的相关信息的对象
runner HTMLTestRunner.HTMLTestRunner(streamfile_obj,title第一次的测试报告,description我是测试报告的描述信息)
# 生成html测试报告如果要生成测试报告则不通过unittest.main()方法执行
runner.run(main_suite)
查看执行结果 配置详细的html测试报告
在测试用例中加上注释信息即可在html报告中体现能更好的展示每个用例的信息
一、通过添加 3对双引号内容
通过在类的下面添加 3对双引号 测试类01的详细信息在测试方法下面添加 3对双引号 test_aaa方法的详细信息;如下图 在生成的测试报告中可更好的展示每条用例的作用 二、只在测试方法中添加
self._testMethodName设置测试用例名称self._testMethodDoc设置测试用例详情
注意上方下划线只有一个不要搞错
在测试用例方法中添加注释信息如下图 查看生成的测试报告 方式二、通过TextTestRunner()方法生成text类型的测试报告
代码示例
# 使用discover()来实现添加执行整个目录下所有的测试用例
import os
import unittest
import HTMLTestRunner
import time# 获取当前路径
curren_path os.path.dirname(__file__)
# 获取测试用例目录的路径
case_path os.path.join(curren_path,all_case)# 从当前路径回到上一层路径
# case_path os.path.abspath(os.path.dirname(os.path.dirname(__file__)))# 匹配测试用例路径下的所有的测试方法
discover unittest.defaultTestLoader.discover(start_dircase_path, # 用例路径pattern*_case*.py,top_level_dirNone) # 文件类型
# 创建主套件
main_suite unittest.TestSuite()
# 把测试用例路径添加到主套件中
main_suite.addTest(discover)
# 执行主套件里的测试用例如果要生成测试报告则不通过unittest.main()方法执行
# unittest.main(defaultTestmain_suite,verbosity2)# 生成text类型的测试报告
with open(./report/test_result.txt,w,encodingutf-8) as file:test_runner unittest.TextTestRunner(streamfile,descriptions执行用例的测试报告, verbosity2)test_runner.run(main_suite)
查看执行结果 实战案例
光学理论是没用的要学会跟着一起敲要动手实操才能将自己的所学运用到实际当中去这时候可以搞点实战案例来学习。 如果对你有帮助的话点个赞收个藏给作者一个鼓励。也方便你下次能够快速查找。
如有不懂还要咨询下方小卡片博主也希望和志同道合的测试人员一起学习进步
在适当的年龄选择适当的岗位尽量去发挥好自己的优势。
我的自动化测试开发之路一路走来都离不每个阶段的计划因为自己喜欢规划和总结
测试开发视频教程、学习笔记领取传送门 文章转载自: http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.hphrz.cn.gov.cn.hphrz.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.qkzdc.cn.gov.cn.qkzdc.cn http://www.morning.phechi.com.gov.cn.phechi.com http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.clzly.cn.gov.cn.clzly.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.qwdlj.cn.gov.cn.qwdlj.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.fkdts.cn.gov.cn.fkdts.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.wcgfy.cn.gov.cn.wcgfy.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.divocn.com.gov.cn.divocn.com http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.yunease.com.gov.cn.yunease.com http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn http://www.morning.xwgbr.cn.gov.cn.xwgbr.cn http://www.morning.rwjfs.cn.gov.cn.rwjfs.cn http://www.morning.frfpx.cn.gov.cn.frfpx.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.kgxyd.cn.gov.cn.kgxyd.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.qcfgd.cn.gov.cn.qcfgd.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.ylpl.cn.gov.cn.ylpl.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.fswml.cn.gov.cn.fswml.cn http://www.morning.tzzxs.cn.gov.cn.tzzxs.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.ngqty.cn.gov.cn.ngqty.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.tlfmr.cn.gov.cn.tlfmr.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.ggcjf.cn.gov.cn.ggcjf.cn http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.rjrnx.cn.gov.cn.rjrnx.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn