Golang Web開發(fā)之Revel測(cè)試
Revel提供了一個(gè)測(cè)試框架,這使得在應(yīng)用程序中寫和運(yùn)行測(cè)試函數(shù)變得很容易.
skeleton應(yīng)用程序帶有一個(gè)簡(jiǎn)單的測(cè)試來(lái)幫助我們測(cè)試.
概要
測(cè)試保存在tests目錄
- corp/myapp
- app/
- conf/
- public/
- tests/ <----
一個(gè)簡(jiǎn)單的測(cè)試看起來(lái)像下面這樣:
- type ApplicationTest struct {
- rev.TestSuite
- }
- func (t ApplicationTest) Before() {
- println("Set up")
- }
- func (t ApplicationTest) TestThatIndexPageWorks() {
- t.Get("/")
- t.AssertOk()
- t.AssertContentType("text/html")
- }
- func (t ApplicationTest) After() {
- 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)用程序.
- type TestSuite struct {
- Client *http.Client
- Response *http.Response
- ResponseBody []byte
- }
- // Some request methods
- func (t *TestSuite) Get(path string)
- func (t *TestSuite) Post(path string, contentType string, reader io.Reader)
- func (t *TestSuite) PostForm(path string, data url.Values)
- func (t *TestSuite) MakeRequest(req *http.Request)
- // Some assertion methods
- func (t *TestSuite) AssertOk()
- func (t *TestSuite) AssertContentType(contentType string)
- func (t *TestSuite) Assert(exp bool)
- func (t *TestSuite) Assertf(exp bool, formatStr string, args ...interface{})
全部的請(qǐng)求方法表現(xiàn)相似:
- 它們接收一個(gè)路徑(例如: /users/)
- 它們發(fā)出請(qǐng)求到應(yīng)用程序服務(wù)器
- 它們把響應(yīng)存儲(chǔ)了Response屬性中
- 它們讀取全部的響應(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 以保證激活它
- 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è)試方法
- func (t ApplicationTest) TestSomethingImportant() {
- t.Get("/")
- t.AssertOk()
- t.AssertContentType("text/xml")
- }
刷新頁(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è)試.
示例如下:
- $ revel test github.com/robfig/revel/samples/booking dev
- ~
- ~ revel! http://robfig.github.com/revel
- ~
- INFO 2012/11/09 19:21:02 revel.go:237: Loaded module testrunner
- Open DB
- Listening on port 9000...
- INFO 2012/11/09 19:21:06 test.go:95: Testing Booking example (github.com/robfig/revel/samples/booking) in dev mode
- Go to /@tests to run the tests.
- test suite to run.
- ApplicationTest PASSED 0s
- All Tests Passed.
在控制臺(tái)只有一個(gè)簡(jiǎn)單的 PASSED/FAILED 概要通過(guò)測(cè)試工具來(lái)顯示. 這個(gè)工具寫入更多的結(jié)果到文件系統(tǒng):
- $ cd src/github.com/robfig/revel/samples/booking
- $ find test-results
- test-results
- test-results/app.log
- test-results/ApplicationTest.passed.html
- test-results/result.passed
它寫入了3個(gè)不同的東西:
- 應(yīng)用程序的stdout和stderr被重定向到 app.log
- 一個(gè)HTML文件每個(gè)測(cè)試工具都寫入描述測(cè)試的通過(guò)和失敗的信息
- 要么result.passed要么result.failed被寫入, 依賴于總體是否成功
這里有兩個(gè)集成這個(gè)到持續(xù)構(gòu)建的建議機(jī)制
- 檢查返回代碼, 0表示成功非0另外
- 運(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