新聞中心
Revel提供了一個(gè)測試框架,可以很容易地編寫和運(yùn)行針對您的應(yīng)用程序的功能測試。

應(yīng)用程序帶有一個(gè)簡單的測試骨架以便快速上手測試。
Revel 測試框架概要
測試代碼保存在測試目錄中:
corp/myapp
app/
conf/
public/
tests/ <----一個(gè)簡單的測試如下所示:
type AppTest struct {
revel.TestSuite
}
func (t *AppTest) Before() {
println("Set up")
}
func (t *AppTest) TestThatIndexPageWorks() {
t.Get("/")
t.AssertOk()
t.AssertContentType("text/html")
}
func (t *AppTest) After() {
println("Tear down")
}上面的例子中展示了:
- 測試套件是嵌入
revel.TestSuite的一個(gè)struct Before()、After()在每個(gè)測試方法之前和之后被調(diào)用,如果有的話。revel.TestSuite幫助發(fā)出請求到你的應(yīng)用程序,和對響應(yīng)的斷言。- 如果一個(gè)斷言失敗,產(chǎn)生了恐慌,將被測試工具捕獲。
你可以用兩種方式運(yùn)行這個(gè)測試:
- 交互方式,Web瀏覽器,開發(fā)過程中非常有用。
- 非交互方式,命令行,對于持續(xù)構(gòu)建整合有用。
開發(fā)一個(gè)測試套件
要?jiǎng)?chuàng)建自己的測試套件,需要定義一個(gè)嵌入了revel.TestSuite類型的struct, 它提供了一個(gè)HTTP客戶端和一些輔助方法發(fā)出請求到應(yīng)用程序。
type TestSuite struct {
Client *http.Client
Response *http.Response
ResponseBody []byte
}
// 一些請求方法
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)
// 一些斷言方法
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{})參考godoc
所有的請求方法類似:
- 接受一個(gè)路徑 (比如
/users/) - 向應(yīng)用程序服務(wù)器發(fā)出一個(gè)請求
- 存儲
Response中的成員 - 讀取完整的響應(yīng)到
ResponseBody成員中
如果開發(fā)人員希望使用一個(gè)定制的HTTP客戶端,而不是默認(rèn)的http.DefaultClient,應(yīng)當(dāng)在Before() 方法之前替換它。
斷言失敗后,會拋出恐慌并被測試工具捕獲,并將錯(cuò)誤列出。
運(yùn)行測試套件
為了運(yùn)行測試,testrunner 模塊必須被激活。需要在 app.conf文件中配置:
module.testrunner = github.com/revel/revel/modules/testrunner您還必須導(dǎo)入測試模塊的路由,在你的 routes 文件中加入下面的內(nèi)容:
module:testrunner配置完后,測試就可以交互或非交互方式運(yùn)行。
運(yùn)行交互式測試
要利用 Revel 的熱編譯功能,交互式測試運(yùn)行提供了快速編輯刷新周期。
例如,開發(fā)人員從瀏覽器中訪問 /@tests:
然后,增加一個(gè)測試方法:
func (t AppTest) TestSomethingImportant() {
t.Get("/")
t.AssertOk()
t.AssertContentType("text/xml")
}然后,刷新瀏覽器,看看新的測試:
運(yùn)行測試:
嗯哼,,,行不通哦,,,修改代碼使用“text/html” 替換 “text/xml”類型。
t.AssertContentType("text/html")然后,重新運(yùn)行測試:
成功啦!
運(yùn)行非交互式測試
Revel 命令行工具 提供了一個(gè) test 命令,允許在命令行中運(yùn)行測試。
下面是一個(gè)示例會話:
$ revel test github.com/revel/revel/samples/booking dev
~
~ revel! http://revel.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/revel/revel/samples/booking) in dev mode
Go to /@tests to run the tests.
1 test suite to run.
AppTest PASSED 0s
All Tests Passed.您還可以運(yùn)行單個(gè)測試套件,或套件內(nèi)的方法,用句點(diǎn)分隔參數(shù):
$ revel test github.com/revel/revel/samples/booking dev ApplicationTest
$ revel test github.com/revel/revel/samples/booking dev ApplicationTest.TestThatIndexPageWorks在控制臺測試套件只有一個(gè)簡單的合格/不合格顯示。更詳細(xì)的結(jié)果寫入到文件系統(tǒng):
$ cd src/github.com/revel/revel/samples/booking
$ find test-results
test-results
test-results/app.log
test-results/AppTest.passed.html
test-results/result.passed它寫三點(diǎn)不同:
- 應(yīng)用程序的標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤重定向到
app.log - 每個(gè)測試套件有一個(gè)HTML文件被寫入,說明測試通過或失敗。
- 無論
result.passed或result.failed被寫入, 這取決于整體的成功。
對于整合持續(xù)構(gòu)建測試,有兩點(diǎn)建議:
- 檢查返回碼,0代表測試成功,否則為非0值。
- 測試運(yùn)行后要求存在
result.success, 或禁止result.failed存在。
注意事項(xiàng)
Revel 做了什么:
- 掃描嵌入TestSuite類型 (transitively) 的源代碼
- 在生成的 main.go 文件中,為
revel.TestSuites類型的變量設(shè)置一個(gè)列表 - 按要求,使用反射來查找所有以“Test”開頭的TestSuite類型的方法,并調(diào)用它們來運(yùn)行測試。
- 從錯(cuò)誤或失敗的斷言捕獲恐慌,顯示錯(cuò)誤。
當(dāng) testrunner 模塊激活后,測試代碼才會被構(gòu)建。
開發(fā)計(jì)劃
改進(jìn)測試框架:
- 固定存儲測試數(shù)據(jù)。
- 日志寫入到一個(gè)文件中(而不是 stderr / stdout)也應(yīng)該被重定向到
test-results/app.log
當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)Revel教程:Revel 測試
URL地址:http://www.dlmjj.cn/article/coddpdi.html


咨詢
建站咨詢
