Improve this page


Running Tests

The Buffalo test runner will make sure your test environment is correct, and run your tests.

For example, if using Pop (database), it will first try to setup your test database by using the schema from the development database. If that doesn't exist (CI, for example), it will run the migrations against the test database.

The test runner will also make sure to ignore the dreaded vendor directory.

$ buffalo test

dropped database authrecipe_test
created database authrecipe_test
dumped schema for authrecipe_development
loaded schema for authrecipe_test
go test -p 1
?	[no test files]
ok	0.640s
?	[no test files]
ok	0.327s

Execute a single test

since v0.10.2

Debugging a specific test is a difficult task, if you must execute all existing tests. You can use the -m flag to execute a single test method:

$ buffalo test -m "FooMethod"

Test Suites

Buffalo uses the package to create test suites.

When running a test that is part of the test suite, the following is available to the test:

Test Example

func (as *ActionSuite) Test_WidgetsResource_Create() {
  // setup a Widget model
  w := &models.Widget{Name: "My Widget"} // make a POST /widgets request
  res := as.HTML("/widgets").Post(w)
  // assert that the response status code was 302 as.Equal(302, res.Code)
  // retreive the first Widget from the database
  err := as.DB.First(w)
  // assert the Widget title was saved correctly
  as.Equal("My Widget", w.Name)
  // assert the redirect was sent to the place expected
  as.Equal(fmt.Sprintf("/widgets/%s", w.ID), res.Location())

Accessing the Session

Being able to manipulate the session for testing is very important. Test suites in Buffalo give you access to a testing session that you can use.

See for a more in-depth example.

func (as *ActionSuite) Test_HomeHandler_LoggedIn() {
  // get a user from the DB

  // set the user ID onto the session
  as.Session.Set("current_user_id", user.ID)

  res := as.HTML("/").Get()
  as.Equal(200, res.Code)

  // now the user is "logged in"
  as.Contains(res.Body.String(), "Sign Out")

  // clear the session
  res = as.HTML("/").Get()
  as.Equal(200, res.Code)

  // now the user is "logged out"
  as.Contains(res.Body.String(), "Sign In")