Build testing suites with the suite package
masterThe suite package allows you to organize tests into structs, enabling setup/teardown logic and shared state.
Key features:
- Embed
suite.Suitein your test struct. - Implement
SetupTest()to run code before every test method. - All methods starting with
Testare automatically run as tests. - The
suite.Suiteobject provides built-in assertion methods (e.g.,suite.Equal) so you don't need to passtmanually. - Note: The
suitepackage does not support parallel tests.
To run a suite, you must call suite.Run(t, new(YourSuiteStruct)) from a standard TestXxx(t *testing.T) function.
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int
}
func (suite *ExampleTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5
}
func (suite *ExampleTestSuite) TestExample() {
// Use built-in assertion methods on the suite object
suite.Equal(5, suite.VariableThatShouldStartAtFive)
}
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(ExampleTestSuite))
}