Revel提供了一個測試框架,可以很容易地編寫和運行針對您的應(yīng)用程序的功能測試。
應(yīng)用程序帶有一個簡單的測試骨架以便快速上手測試。
測試代碼保存在測試目錄中:
corp/myapp
app/
conf/
public/
tests/ <----
一個簡單的測試如下所示:
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
的一個structBefore()
、 After()
在每個測試方法之前和之后被調(diào)用,如果有的話。revel.TestSuite
幫助發(fā)出請求到你的應(yīng)用程序,和對響應(yīng)的斷言。你可以用兩種方式運行這個測試:
要創(chuàng)建自己的測試套件,需要定義一個嵌入了revel.TestSuite
類型的struct, 它提供了一個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{})
所有的請求方法類似:
/users/
)Response
中的成員ResponseBody
成員中如果開發(fā)人員希望使用一個定制的HTTP客戶端,而不是默認(rèn)的http.DefaultClient,應(yīng)當(dāng)在Before()
方法之前替換它。
斷言失敗后,會拋出恐慌并被測試工具捕獲,并將錯誤列出。
為了運行測試,testrunner
模塊必須被激活。需要在 app.conf
文件中配置:
module.testrunner = github.com/revel/revel/modules/testrunner
您還必須導(dǎo)入測試模塊的路由,在你的 routes
文件中加入下面的內(nèi)容:
module:testrunner
配置完后,測試就可以交互或非交互方式運行。
要利用 Revel 的熱編譯功能,交互式測試運行提供了快速編輯刷新周期。
例如,開發(fā)人員從瀏覽器中訪問 /@tests
:
然后,增加一個測試方法:
func (t AppTest) TestSomethingImportant() {
t.Get("/")
t.AssertOk()
t.AssertContentType("text/xml")
}
然后,刷新瀏覽器,看看新的測試:
運行測試:
嗯哼,,,行不通哦,,,修改代碼使用“text/html” 替換 “text/xml”類型。
t.AssertContentType("text/html")
然后,重新運行測試:
成功啦!
Revel 命令行工具 提供了一個 test
命令,允許在命令行中運行測試。
下面是一個示例會話:
$ 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.
您還可以運行單個測試套件,或套件內(nèi)的方法,用句點分隔參數(shù):
$ revel test github.com/revel/revel/samples/booking dev ApplicationTest
$ revel test github.com/revel/revel/samples/booking dev ApplicationTest.TestThatIndexPageWorks
在控制臺測試套件只有一個簡單的合格/不合格顯示。更詳細(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
它寫三點不同:
app.log
result.passed
或 result.failed
被寫入, 這取決于整體的成功。對于整合持續(xù)構(gòu)建測試,有兩點建議:
result.success
, 或禁止 result.failed
存在。Revel 做了什么:
revel.TestSuites
類型的變量設(shè)置一個列表當(dāng) testrunner
模塊激活后,測試代碼才會被構(gòu)建。
改進(jìn)測試框架:
test-results/app.log
更多建議: