视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
unittest+coverage怎么进行单元测试覆盖
2020-11-27 19:56:10 责编:小采
文档
这次给大家带来unittest+coverage怎么进行单元测试覆盖,unittest+coverage进行单元测试覆盖的注意事项有哪些,下面就是实战案例,一起来看一下。

基于上一篇文章,这篇文章是关于使用coverage来实现代码覆盖的操作实例,源代码在上一篇已经给出相应链接。

本篇文章字用来实现代码覆盖的源代码,整个项目的测试框架如下:

就是在源代码的基础上加了一个CodeCover.py文件,执行该文件会在目录CoverageReport生成相应的覆盖报告。如下是CodeCover.py的源码:

#coding=utf8 
import os 
import time 
 
def findTestWithPath(): 
 current_dir=os.getcwd() 
 folderName=os.listdir(current_dir) 
 #print folderName 
 #获取到测试文件所在目录 
 TestSuit=[suite for suite in folderName if not suite.find("TestSuit")] 
 #用来保存测试文件 
 testfile=[] 
 withPathFile=[] 
 for suite in TestSuit: 
 #获取测试目录下的所有测试文件 
 testfile=testfile+os.listdir(".\\"+suite) 
 for withPath in testfile: 
 withPath=current_dir+"\\"+suite+"\\"+withPath 
 withPathFile.append(withPath) 
 del testfile 
 #把testfile中的py文件挑选出来 
 withPathFile=[name for name in withPathFile if not "pyc" in name] 
 #print testfile 
 print withPathFile 
 return withPathFile 
 
def codeCoverage(): 
 now = time.strftime("%Y%m%d%H%M") 
 htmlReport=os.getcwd()+"\\"+"CoverageReport" 
 htmlCmd="coverage html -d " + htmlReport +"\\"+now 
 for pyfile in findTestWithPath(): 
 runPyCmd="coverage run " + pyfile 
 if os.path.exists(htmlReport) : 
 os.system(runPyCmd) 
 os.system(htmlCmd) 
 else: 
 os.mkdir(htmlReport) 
 os.system(runPyCmd) 
 os.system(htmlCmd) 
 
 
if name=="main": 
 codeCoverage()

运行结果图:

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

python使用unittest测试接口步奏详解

Python字典的清单如何使用

下载本文
显示全文
专题