unittest+coverage单元测试代码覆盖操作实例详解
作者:坏蛋是我 时间:2021-10-09 09:32:24
基于上一篇文章,这篇文章是关于使用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()
运行结果图:
来源:https://blog.csdn.net/henni_719/article/details/59056763
标签:unittest,coverage,单元测试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用Python快乐学数学Github万星神器Manim简介
2022-07-03 04:28:00
![](https://img.aspxhome.com/file/2023/4/101104_0s.gif)
MDB格式网站文件如何解压
2008-07-02 12:48:00
python 实现查询Neo4j多节点的多层关系
2023-04-05 19:41:38
![](https://img.aspxhome.com/file/2023/0/99330_0s.jpg)
python 生成器协程运算实例
2021-11-22 05:27:51
python3下实现搜狗AI API的代码示例
2022-04-11 09:30:58
Python 利用切片从列表中取出一部分使用的方法
2021-04-05 14:14:19
基于python实现音乐播放器代码实例
2022-07-17 21:55:53
PHP开启opcache提升代码性能
2023-06-17 04:39:55
Python爬虫通过替换http request header来欺骗浏览器实现登录功能
2021-04-05 21:55:26
![](https://img.aspxhome.com/file/2023/9/102939_0s.png)
浅谈JupyterNotebook导出pdf解决中文的问题
2022-11-26 11:02:26
![](https://img.aspxhome.com/file/2023/6/103786_0s.jpg)
使用cx_freeze把python打包exe示例
2021-06-10 05:36:20
用Python实现定时备份Mongodb数据并上传到FTP服务器
2021-09-09 03:40:02
![](https://img.aspxhome.com/file/2023/2/80472_0s.png)
在ASP中改善动态分页的性能
2008-05-08 14:27:00
Numpy 多维数据数组的实现
2022-12-22 11:26:03
![](https://img.aspxhome.com/file/2023/3/97113_0s.png)
Python3自动安装第三方库,跟pip说再见
2022-03-12 04:34:15
![](https://img.aspxhome.com/file/2023/6/76896_0s.png)
WEB2.0网页制作标准教程(12)XHTML校验及常见错误
2008-02-19 19:59:00
![](https://img.aspxhome.com/file/UploadPic/20082/19/2008219201419762s.gif)
Python实现读取机器硬件信息的方法示例
2021-02-04 16:28:55
windows 下python+numpy安装实用教程
2022-06-26 09:52:26
.NET中书写XML的一种简单方法
2007-08-23 12:55:00
关于Python dict存中文字符dumps()的问题
2022-08-22 17:53:17
![](https://img.aspxhome.com/file/2023/7/79507_0s.png)