Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Revel test


May 15, 2021 Revel


Table of contents


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.

Revel Test Framework Summary

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:

  • The test suite is 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.
  • If an assertion fails, causing panic, it will be captured by the test tool.

You can run this test in two ways:

  • Interactive, web browser, very useful during development.
  • Non-interactive, command-line, useful for continuously building consolidation.

Develop a test suite

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{})

Refer to godoc

All request methods are similar:

  1. Accept a path (such as /users/ )
  2. A request is made to the application server
  3. Stores members in Response
  4. Read the full 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.

Run the test suite

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.

Run interactive tests

To take advantage of Revel's hot compilation capabilities, interactive test runs provide a fast edit refresh cycle.

For example, a developer accesses /@tests :

Revel test

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:

Revel test

Run the test:

Revel 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:

Revel test

It's a success!

Run non-interactive tests

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:

  1. The application's standard output and standard errors are app.log
  2. Each test suite has an HTML file written to indicate that the test passed or failed.
  3. Whether result.passed result.failed written, it depends on overall success.

There are two recommendations for consolidating ongoing build tests:

  1. Check the return code, 0 represents a successful test, otherwise it is a non-0 value.
  2. After the test runs, the result.success or result.failed

Precautions

What Revel did:

  • Scans the source code embedded in the TestSuite type
  • In the generated main.go file, it is revel.TestSuites of the TestSuites type is set
  • As required, use reflection to find all TestSuite-type methods that start with "Test" and call them to run the test.
  • Capture panic from an incorrect or failed assertion and display an error.

The test code is not built until the testrunner module is activated.

Development plan

Improve the testing framework:

  • Fixed storage test data.
  • Logs written to a file (not stderr /stdout) should also be test-results/app.log