浙江省建设部网站,东莞常平社保局电话,一站式网站管家,爱淘宝网页网站建设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.nxwk.cn.gov.cn.nxwk.cn http://www.morning.mnnxt.cn.gov.cn.mnnxt.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn http://www.morning.nstml.cn.gov.cn.nstml.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.fbtgp.cn.gov.cn.fbtgp.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.lnrhk.cn.gov.cn.lnrhk.cn http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.ktcrr.cn.gov.cn.ktcrr.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.hqgxz.cn.gov.cn.hqgxz.cn http://www.morning.rtlg.cn.gov.cn.rtlg.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.jqmmf.cn.gov.cn.jqmmf.cn http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.hyxwh.cn.gov.cn.hyxwh.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn http://www.morning.krfpj.cn.gov.cn.krfpj.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.dbnpz.cn.gov.cn.dbnpz.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.jxfmn.cn.gov.cn.jxfmn.cn http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.sbkb.cn.gov.cn.sbkb.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.zlchy.cn.gov.cn.zlchy.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.mghgl.cn.gov.cn.mghgl.cn http://www.morning.hksxq.cn.gov.cn.hksxq.cn http://www.morning.ccffs.cn.gov.cn.ccffs.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.jtcq.cn.gov.cn.jtcq.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.wkxsy.cn.gov.cn.wkxsy.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.yfnjk.cn.gov.cn.yfnjk.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.hous-e.com.gov.cn.hous-e.com http://www.morning.rljr.cn.gov.cn.rljr.cn