av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

GolangWeb開發(fā)之Revel測試

Revel提供了一個測試框架,這使得在應用程序中寫和運行測試函數(shù)變得很容易.

專注于為中小企業(yè)提供成都網(wǎng)站設計、網(wǎng)站制作服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)臨滄免費做網(wǎng)站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了數(shù)千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉變。

skeleton應用程序帶有一個簡單的測試來幫助我們測試.

概要

測試保存在tests目錄

 
 
 
 
  1. corp/myapp  
  2.     app/  
  3.     conf/  
  4.     public/  
  5.     tests/    <---- 

一個簡單的測試看起來像下面這樣:

 
 
 
 
  1. type ApplicationTest struct {  
  2.   rev.TestSuite  
  3. }  
  4.  
  5. func (t ApplicationTest) Before() {  
  6.     println("Set up")  
  7. }  
  8.  
  9. func (t ApplicationTest) TestThatIndexPageWorks() {  
  10.     t.Get("/")  
  11.     t.AssertOk()  
  12.     t.AssertContentType("text/html")  
  13. }  
  14.  
  15. func (t ApplicationTest) After() {  
  16.     println("Tear down")  

上面的示例代碼展示了幾件事:

  • 一個測試工具是任意嵌入rev.TestSuite的struct
  • 如果存在 Before() 和 After() 方法, 它們將在每一個測試方法的前后被調用
  • rev.TestSuite 為發(fā)布請求到應用程序和斷言響應信息提供幫助
  • 一個斷言失敗產(chǎn)生一個panic, 它將被harness捕獲

你可以已兩種方式運行測試:

  • 交互式的, 從你的瀏覽器運行在測試部署時很有幫助
  • 非交互式的, 從命令行運行對結合一個持續(xù)集成很有幫助

開發(fā)一個測試工具

 創(chuàng)建一個你自己的測試工具, 定義一個嵌入 rev.Testsuite的struct, 它提供一個HTTP客戶端和許多幫助方法來發(fā)出請求到你的應用程序.

 
 
 
 
  1. type TestSuite struct {  
  2.     Client       *http.Client  
  3.     Response     *http.Response  
  4.     ResponseBody []byte 
  5. }  
  6.  
  7. // Some request methods  
  8. func (t *TestSuite) Get(path string)  
  9. func (t *TestSuite) Post(path string, contentType string, reader io.Reader)   
  10. func (t *TestSuite) PostForm(path string, data url.Values)   
  11. func (t *TestSuite) MakeRequest(req *http.Request)  
  12.  
  13. // Some assertion methods  
  14. func (t *TestSuite) AssertOk()  
  15. func (t *TestSuite) AssertContentType(contentType string)  
  16. func (t *TestSuite) Assert(exp bool)  
  17. func (t *TestSuite) Assertf(exp bool, formatStr string, args ...interface{}) 

全部的請求方法表現(xiàn)相似:

  1. 它們接收一個路徑(例如: /users/)
  2. 它們發(fā)出請求到應用程序服務器
  3. 它們把響應存儲了Response屬性中
  4. 它們讀取全部的響應body到ResponseBody屬性

如果開發(fā)人員希望使用自定義的HTTP Client代替默認的 http.DefaultClient, 它們應該在Before()方法里面替換它.

如果它們沒有滿足條件全部斷言都將產(chǎn)生一個panic. 全部的panic被測試harness捕獲并展示為錯誤.

運行一個測試工具

 為了運行任何測試, testrunner模塊必須被激活. 添加下面一行代碼到 app.conf 以保證激活它

 
 
 
 
  1. module.testrunner = github.com/robfig/revel/modules/testrunner 

完成上面之后測試就被運行了(交互式或非交互式)

運行交互式的測試

利用Revel的熱編譯功能, 一個交互式的測試運行器用來提供給快速編輯刷新的循環(huán)工作.

例如, 開發(fā)人員在他們的瀏覽器加載 /@tests

 然后他們添加一個測試方法

 
 
 
 
  1. func (t ApplicationTest) TestSomethingImportant() {  
  2.     t.Get("/")  
  3.     t.AssertOk()  
  4.     t.AssertContentType("text/xml")  
  5. }  
  6.  

刷新頁面將看到新的測試方法

運行這個測試

它沒有正常工作. 我們來修復這個問題替換 “text/xml” 為 “text/html”, 刷新瀏覽器:

成功.

運行非交互式的測試

Revel 命令行工具 提供了一個 test 命令, 它運行全部的應用程序在命令行工具中運行測試.

示例如下:

 
 
 
 
  1. $ revel test github.com/robfig/revel/samples/booking dev  
  2. ~  
  3. ~ revel! http://robfig.github.com/revel  
  4. ~  
  5. INFO  2012/11/09 19:21:02 revel.go:237: Loaded module testrunner  
  6. Open DB  
  7. Listening on port 9000...  
  8. INFO  2012/11/09 19:21:06 test.go:95: Testing Booking example (github.com/robfig/revel/samples/booking) in dev mode  
  9. Go to /@tests to run the tests.  
  10.  test suite to run.  
  11.  
  12. ApplicationTest         PASSED        0s  
  13.  
  14. All Tests Passed. 

在控制臺只有一個簡單的 PASSED/FAILED 概要通過測試工具來顯示. 這個工具寫入更多的結果到文件系統(tǒng):

 
 
 
 
  1. $ cd src/github.com/robfig/revel/samples/booking  
  2. $ find test-results  
  3. test-results  
  4. test-results/app.log  
  5. test-results/ApplicationTest.passed.html  
  6. test-results/result.passed 

它寫入了3個不同的東西:

  1. 應用程序的stdout和stderr被重定向到 app.log
  2. 一個HTML文件每個測試工具都寫入描述測試的通過和失敗的信息
  3. 要么result.passed要么result.failed被寫入, 依賴于總體是否成功

這里有兩個集成這個到持續(xù)構建的建議機制

  1. 檢查返回代碼, 0表示成功非0另外
  2. 運行后需要result.success或者不允許result.failed.

實現(xiàn)說明

Revel做了什么:

  • 為嵌套TestSuite類型掃描測試源代碼
  • 在生成main.go時設置rev.TestSuites變量到那些類型的列表
  • 使用反射在TestSuite類型上查找全部的以Test開頭的方法并調用它們來運行測試
  • 從bugs或失敗的斷言中捕獲panics并顯示有幫助的錯誤信息

開發(fā)區(qū)域

可以使用以下方式改進測試框架

  • Fixtures來填充測試數(shù)據(jù)
  • 記錄器寫入一個文件(替換 stderr / stdout )也應該被重定向到 test-results/app.log

至此結束

原文鏈接:http://www.cnblogs.com/ztiandan/archive/2013/01/09/2846073.html


網(wǎng)站題目:GolangWeb開發(fā)之Revel測試
URL網(wǎng)址:http://uogjgqi.cn/article/cogojic.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流