unittest+coverage单元测试代码覆盖操作实例
基于上⼀篇⽂章,这篇⽂章是关于使⽤coverage来实现代码覆盖的操作实例,源代码在上⼀篇已经给出相应链接。
创意之星本篇⽂章字⽤来实现代码覆盖的源代码,整个项⽬的测试框架如下:
就是在源代码的基础上加了⼀个CodeCover.py⽂件,执⾏该⽂件会在⽬录CoverageReport⽣成相应的覆盖报告。如下是CodeCover.py的源码:
#coding=utf8
import os
import time
三年级上册数学题def findTestWithPath():
current_wd()
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")
wd()+"\\"+"CoverageReport"
htmlCmd="coverage html -d " + htmlReport +"\\"+now
for pyfile in findTestWithPath():
心流理论
runPyCmd="coverage run " + pyfile
if ists(htmlReport) :
os.system(runPyCmd)
os.system(htmlCmd)
el:
os.mkdir(htmlReport)
os.system(runPyCmd)
体积密度
相爱到永远os.system(htmlCmd)
if __name__=="__main__":
codeCoverage()
化学九年级运⾏结果图:
转载于:my.oschina/u/2291665/blog/858568