基于上一篇文章,這篇文章是關于使用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字典的清單如何使用
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com