当前位置: 移动技术网 > IT编程>脚本编程>Python > unittest+coverage单元测试代码覆盖操作实例详解

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

2019年06月18日  | 移动技术网IT编程  | 我要评论

羊城通客服中心,血液循环机,上海市九龙男子医院

基于上一篇文章,这篇文章是关于使用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() 

运行结果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网