unittest+coverage單元測試代碼覆蓋操作執行個體詳解_python

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了unittest+coverage單元測試代碼覆蓋操作的執行個體,具有一定的參考價值,感興趣的小夥伴們可以參考一下

基於上一篇文章,這篇文章是關於使用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()

運行結果圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.