unittest+coverage单元测试代码覆盖操作实例详解


Posted in Python onApril 04, 2018

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

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

unittest+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()

运行结果图:

unittest+coverage单元测试代码覆盖操作实例详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python调用cmd命令行制作刷博器
Jan 13 Python
Python中的字典与成员运算符初步探究
Oct 13 Python
pandas 按照特定顺序输出的实现代码
Jul 10 Python
Python输出\u编码将其转换成中文的实例
Dec 15 Python
python 处理数字,把大于上限的数字置零实现方法
Jan 28 Python
解决python文件双击运行秒退的问题
Jun 24 Python
详解Python的三种拷贝方式
Feb 11 Python
使用Python三角函数公式计算三角形的夹角案例
Apr 15 Python
pytorch  网络参数 weight bias 初始化详解
Jun 24 Python
Python matplotlib读取excel数据并用for循环画多个子图subplot操作
Jul 14 Python
Python中可变和不可变对象的深入讲解
Aug 02 Python
python字符串拼接.join()和拆分.split()详解
Nov 23 Python
python unittest实现api自动化测试
Apr 04 #Python
Python Unittest自动化单元测试框架详解
Apr 04 #Python
python如何使用unittest测试接口
Apr 04 #Python
Python统计单词出现的次数
Apr 04 #Python
高效使用Python字典的清单
Apr 04 #Python
Python的numpy库中将矩阵转换为列表等函数的方法
Apr 04 #Python
python 列表,数组,矩阵两两转换tolist()的实例
Apr 04 #Python
You might like
在服务端进行目录建立、删除,文件上传、删除的过程的php代码
2008/09/10 PHP
精通php的十大要点(上)
2009/02/04 PHP
php smarty模版引擎中的缓存应用
2009/12/11 PHP
jQuery 源码分析笔记
2011/05/25 PHP
ThinkPHP空模块和空操作详解
2014/06/30 PHP
php结合md5的加密解密算法实例
2016/09/30 PHP
详解Yii2高级版引入bootstrap.js的一个办法
2017/03/21 PHP
PHP实现在数据库百万条数据中随机获取20条记录的方法
2017/04/19 PHP
ImageFlow可鼠标控制图片滚动
2008/01/30 Javascript
jQuery 1.3 和 Validation 验证插件1.5.1
2009/07/09 Javascript
JavaScript Sort 表格排序
2009/10/31 Javascript
JavaScript在网页中画圆的函数arc使用方法
2015/11/13 Javascript
JavaScript实现简单的tab选项卡切换
2016/01/05 Javascript
纯js实现倒计时功能
2017/01/06 Javascript
JS动态生成年份和月份实例代码
2017/02/04 Javascript
前端页面文件拖拽上传模块js代码示例
2017/05/19 Javascript
angular项目中bootstrap-datetimepicker时间插件的使用示例
2018/03/15 Javascript
Javascript三种字符串连接方式及性能比较
2019/05/28 Javascript
使用原生JS实现火锅点餐小程序(面向对象思想)
2019/12/10 Javascript
[00:32]2018DOTA2亚洲邀请赛Liquid出场
2018/04/03 DOTA
Python中itertools模块用法详解
2014/09/25 Python
python3实现短网址和数字相互转换的方法
2015/04/28 Python
python实现在windows服务中新建进程的方法
2015/06/30 Python
Python网络爬虫实例讲解
2016/04/28 Python
Python实现邮件的批量发送的示例代码
2018/01/23 Python
From CSV to SQLite3 by python 导入csv到sqlite实例
2020/02/14 Python
django rest framework serializers序列化实例
2020/05/13 Python
CSS3动画之利用requestAnimationFrame触发重新播放功能
2019/09/11 HTML / CSS
美体小铺波兰官方网站:The Body Shop波兰
2019/09/03 全球购物
亚洲在线旅行门户网站:Expedia.com.hk(智游网)
2020/04/14 全球购物
制药工程专业个人求职自荐信
2014/01/25 职场文书
外贸专业求职信
2014/03/09 职场文书
授权委托书怎么写
2014/09/25 职场文书
2015元旦晚会主持词(开场白+结束语)
2014/12/14 职场文书
博士导师推荐信
2015/03/25 职场文书
高老头读书笔记
2015/06/30 职场文书