はじめに
RevelでのTestについて試したメモです。公式の資料に大体まとまってるので、その内容に従って実施しました。
* Revel Testing
試した事を随時追加してきます。(なので今はとりあえず実行する方法を確認しただけ)
サンプルプロジェクトの作成
revel new myapp
を実行し $GOPATH/src/myapp にサンプルプロジェクトを作成する。
$ tree -L 1 myapp/
myapp/
├── README.md
├── app
├── conf
├── messages
├── public
└── tests
myappに含まれるtestsディレクトリにテストを実装する。myapp生成時にサンプルのテストファイルが作成されている
apptest.go
package tests
import "github.com/revel/revel"
type AppTest struct {
revel.TestSuite
}
func (t *AppTest) Before() {
println("Set up")
}
func (t *AppTest) TestThatIndexPageWorks() {
t.Get("/")
t.AssertOk()
t.AssertContentType("text/html; charset=utf-8")
}
func (t *AppTest) After() {
println("Tear down")
}
Testの実行
go testとかではなく、revelが持っているtest機構を利用する。
revelのテストの実行方法には二種類あり、一つはインタラクティブに実行する方法、もうひとつは主導で実行する方法がある。
Interactiveな実行方法
revel run myapp
でアプリケーションを実行する。
http://localhost:9000/@tests
にアクセスするとtest一覧が表示される。
Run All TestsをクリックするとTestが実行される。
Testを追加するとこの画面に追加されていく。
GUIで操作出来るので最初はコレが良いかも。
手動実行
revel test myapp dev
ですべてのテストが実行される
$ revel test myapp dev [~/go/src/myapp/tests]
~
~ revel! http://revel.github.io
~
INFO 2015/01/19 11:31:49 revel.go:326: Loaded module testrunner
INFO 2015/01/19 11:31:49 revel.go:326: Loaded module static
INFO 2015/01/19 11:31:51 revel.go:326: Loaded module testrunner
INFO 2015/01/19 11:31:51 revel.go:326: Loaded module static
INFO 2015/01/19 11:31:51 main.go:29: Running revel server
Go to /@tests to run the tests.
Listening on :9000...
INFO 2015/01/19 11:31:51 test.go:108: Testing myapp (myapp) in dev mode
1 test suite to run.
AppTest Set up
Tear down
PASSED 0s
All Tests Passed.
Test Suite単位の実行は
revel test myapp dev AppTest
Method単位の実行は
revel test myapp dev AppTest.TestThatIndexPageWorks