May 15, 2021 Revel
Revel provides a testing framework that makes it easy to write and run functional tests for your application.
The application comes with a simple test skeleton for quick hands-on testing.
The test code is saved in the test directory:
corp/myapp
app/
conf/
public/
tests/ <----
A simple test looks like this:
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")
}
The example above shows:
revel.TestSuite
is a struct
Before()
After()
called before and after each test method, if any.
revel.TestSuite
make requests to your application, and assertions about responses.
You can run this test in two ways:
To create your own test suite, you need to define an
revel.TestSuite
type struct, which provides an HTTP client and some secondary methods to make requests to the application.
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{})
All request methods are similar:
/users/
)
Response
ResponseBody
member
If the developer wants to use a custom HTTP client instead of the default
http.
DefaultClient,
which should be replaced before the
Before()
method.
When an assertion fails, panic is thrown and captured by the test tool, and errors are listed.
In order to run the test,
testrunner
module must be activated.
Need to
app.conf
file:
module.testrunner = github.com/revel/revel/modules/testrunner
You must also import the route of the test module and include the following in your
routes
file:
module:testrunner
When configured, the tests can run interactively or non-interactively.
To take advantage of Revel's hot compilation capabilities, interactive test runs provide a fast edit refresh cycle.
For example, a developer accesses
/@tests
:
Then, add a test method:
func (t AppTest) TestSomethingImportant() {
t.Get("/")
t.AssertOk()
t.AssertContentType("text/xml")
}
Then, refresh your browser and see the new tests:
Run the test:
Uh-huh,,,s not going to work oh,,, modify the code to replace the "text/xml" type with "text/html".
t.AssertContentType("text/html")
Then, re-run the test:
It's a success!
The Revel
command-line tool
provides a
test
that allows tests to run on the command line.
Here's a sample session:
$ 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.
You can also run a single test suite, or a method within a suite that separates parameters by a period:
$ revel test github.com/revel/revel/samples/booking dev ApplicationTest
$ revel test github.com/revel/revel/samples/booking dev ApplicationTest.TestThatIndexPageWorks
There is only one simple pass/nonconformity display in the console test suite. More detailed results are written to the file system:
$ 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
It writes three different points:
app.log
result.passed
result.failed
written, it depends on overall success.
There are two recommendations for consolidating ongoing build tests:
result.success
or
result.failed
What Revel did:
revel.TestSuites
of the TestSuites type is set
The test code is not built until the
testrunner
module is activated.
Improve the testing framework:
test-results/app.log