unittest+coverage單元測試代碼覆蓋操作實例詳解
更新時間:2018年04月04日 14:58:43 作者:壞蛋是我
這篇文章主要為大家詳細介紹了unittest+coverage單元測試代碼覆蓋操作的實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
基于上一篇文章,這篇文章是關于使用coverage來實現(xiàn)代碼覆蓋的操作實例,源代碼在上一篇已經(jīng)給出相應鏈接。
本篇文章字用來實現(xiàn)代碼覆蓋的源代碼,整個項目的測試框架如下:

就是在源代碼的基礎上加了一個CodeCover.py文件,執(zhí)行該文件會在目錄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()
運行結果圖:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
pyqt5數(shù)據(jù)庫使用詳細教程(打包解決方案)
這篇文章主要介紹了pyqt5數(shù)據(jù)庫使用教程(打包解決方案),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
手把手教你打造個性化全棧應用Python?Reflex框架全面攻略
Reflex框架是為了解決傳統(tǒng)全棧開發(fā)中的一些挑戰(zhàn)而誕生的,它充分利用了現(xiàn)代前端框架(如React)的優(yōu)勢,與后端技術(如Node.js)深度集成,使得開發(fā)者能夠更加流暢地構建整個應用,Reflex的設計理念包括簡化、響應性和一致性,旨在提高全棧開發(fā)的效率和可維護性2023-12-12
Pycharm如何設置默認請求頭和切換python環(huán)境
這篇文章主要介紹了Pycharm如何設置默認請求頭和切換python環(huán)境問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

