Sitemap

Go Testing with testify/suite

4 min readFeb 20, 2023

--

Photo by Sahand Babali on Unsplash

The testify/suite package is a popular testing library for Go that provides a convenient way to group related test functions and set up common fixtures before running the tests. This package is part of the larger testify library, which also includes tools for making assertions and mocking objects in tests.

In this post, we'll take a closer look at the testify/suite package and how it can help simplify your test code.

Getting started

To get started with the testify/suite package, you'll need to install the testify library:

go get github.com/stretchr/testify

Once you have the testify library installed, you can import the testify/suite package and create a new test suite:

import (
"testing"

"github.com/stretchr/testify/suite"
)

type MySuite struct {
suite.Suite
}

func (suite *MySuite) TestSomething() {
// Test something here
}

func TestSuite(t *testing.T) {
suite.Run(t, new(MySuite))
}

In this example, we create a new test suite by defining a struct called MySuite that embeds the suite.Suite type from the testify/suite package. We then define a test function called TestSomething() on the MySuite struct. This test function should contain the test code that you want to run.

Finally, we define a TestSuite() function that uses the suite.Run() function to run the test suite. The new() function is used to create a new instance of the MySuite struct.

Setting up fixtures

One of the key benefits of the testify/suite package is the ability to set up common fixtures before running tests. You can do this by defining a SetupTest() method on your test suite:

type MySuite struct {
suite.Suite
Data []string
}

func (suite *MySuite) SetupTest() {
suite.Data = []string{"one", "two", "three"}
}

func (suite *MySuite) TestSomething() {
// Test something using the Data fixture here
}

In this example, we define a Data field on the MySuite struct to store our fixture data. We then define a SetupTest() method on the MySuite struct that sets up the fixture data by populating the Data field with an array of strings.

In our test function TestSomething(), we can now use the fixture data to run our tests.

Using assertions

The testify/suite package also includes the assert package, which provides a variety of assertion functions that you can use to make sure your tests are working as expected.

Here’s an example of how to use the assert package in a test function:

func (suite *MySuite) TestSomething() {
result := myFunction(suite.Data)
suite.Equal(expected, result)
}

In this example, we call myFunction() with our fixture data and store the result in a variable called result. We then use the suite.Equal() function from the assert package to make sure that result is equal to the expected value.

Setting up fixtures for each test

In addition to the SetupTest function, the testify/suite package also provides a BeforeTest function that allows you to run some code before each test function in your suite.

Here’s an example of how you can use the BeforeTest function:

import (
"testing"

"github.com/stretchr/testify/suite"
)

type MySuite struct {
suite.Suite
Data []string
}

func (suite *MySuite) SetupTest() {
suite.Data = []string{"one", "two", "three"}
}

func (suite *MySuite) BeforeTest(suiteName, testName string) {
// Run some code before each test
}

func (suite *MySuite) TestSomething() {
// Test something using the Data fixture here
}

In this example, we define a BeforeTest function on our test suite. This function takes two arguments: the name of the suite (suiteName) and the name of the test function (testName). This function will be called before each test function in the suite is run.

You can use the BeforeTest function to set up additional fixtures or perform other actions that need to be done before each test. For example, you might use this function to create temporary files, initialize a database connection, or set up some other kind of test-specific data.

It’s worth noting that if you define both a SetupTest function and a BeforeTest function on your test suite, the SetupTest function will be called first, followed by the BeforeTest function. This means you can use the SetupTest function to set up some basic fixtures that are shared across all test functions in the suite, and then use the BeforeTest function to set up additional fixtures that are specific to each individual test.

By using the BeforeTest function in conjunction with the SetupTest function, you can create a powerful testing framework that allows you to easily set up fixtures and run tests in a consistent, reliable way.

Conclusion

The testify/suite package is a powerful testing library for Go that can help you write more reliable and maintainable tests. By using the SetupTest() method to set up fixtures and the assert package to make assertions, you can easily write tests that are more focused and easier to understand.

--

--

No responses yet