自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Golang Web開發(fā)之Revel測(cè)試

開發(fā) 測(cè)試
Revel提供了一個(gè)測(cè)試框架,這使得在應(yīng)用程序中寫和運(yùn)行測(cè)試函數(shù)變得很容易.skeleton應(yīng)用程序帶有一個(gè)簡(jiǎn)單的測(cè)試來(lái)幫助我們測(cè)試.

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

skeleton應(yīng)用程序帶有一個(gè)簡(jiǎn)單的測(cè)試來(lái)幫助我們測(cè)試.

概要

測(cè)試保存在tests目錄

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

一個(gè)簡(jiǎn)單的測(cè)試看起來(lái)像下面這樣:

  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")  

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

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

你可以已兩種方式運(yùn)行測(cè)試:

  • 交互式的, 從你的瀏覽器運(yùn)行在測(cè)試部署時(shí)很有幫助
  • 非交互式的, 從命令行運(yùn)行對(duì)結(jié)合一個(gè)持續(xù)集成很有幫助

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

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

  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{}) 

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

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

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

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

運(yùn)行一個(gè)測(cè)試工具

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

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

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

運(yùn)行交互式的測(cè)試

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

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

 

 然后他們添加一個(gè)測(cè)試方法

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

刷新頁(yè)面將看到新的測(cè)試方法

運(yùn)行這個(gè)測(cè)試 

它沒有正常工作. 我們來(lái)修復(fù)這個(gè)問題替換 “text/xml” 為 “text/html”, 刷新瀏覽器:

成功.

運(yùn)行非交互式的測(cè)試

Revel 命令行工具 提供了一個(gè) test 命令, 它運(yùn)行全部的應(yīng)用程序在命令行工具中運(yùn)行測(cè)試.

示例如下:

  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. 

在控制臺(tái)只有一個(gè)簡(jiǎn)單的 PASSED/FAILED 概要通過(guò)測(cè)試工具來(lái)顯示. 這個(gè)工具寫入更多的結(jié)果到文件系統(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個(gè)不同的東西:

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

這里有兩個(gè)集成這個(gè)到持續(xù)構(gòu)建的建議機(jī)制

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

實(shí)現(xiàn)說(shuō)明

Revel做了什么:

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

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

可以使用以下方式改進(jìn)測(cè)試框架

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

至此結(jié)束

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

責(zé)任編輯:林師授 來(lái)源: Danny.Tian的技術(shù)宅
相關(guān)推薦

2011-04-07 13:53:25

Web工具

2009-11-25 10:57:17

2012-04-04 11:34:57

iPad

2023-10-22 20:20:37

FiberGo

2011-04-18 10:16:30

WEB高性能

2009-07-09 17:33:39

2012-07-30 09:32:00

Web

2011-07-27 09:33:14

iPhone 網(wǎng)絡(luò) Web

2009-08-17 14:47:31

2009-08-17 16:00:14

2016-12-01 14:51:03

2011-04-19 11:06:03

JavaScriptweb

2021-10-10 23:02:49

Golang語(yǔ)言代碼

2023-07-31 09:13:13

ValidatorGolang

2011-10-18 13:58:32

高性能web

2010-01-11 21:19:57

2020-08-14 10:54:56

NodejsGolang開發(fā)

2021-05-13 20:38:30

2009-08-26 10:49:54

2024-10-21 15:39:24

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)