Running only go test will look for tests only at the current folder.
Search and run tests on a specific folder:
go test <path>
Run specific test suite:
go test <path> -run TestSuitName
# or
go test ./... -run TestSuitName
The biggest third-part testing package that helps writing assertions, mocks and testing suite interfaces and functions.
Using only the native testing package of Go considerably increases boilerplate, and test code, since you will have to manually write errors, and manually fail tests.
Assertions with assert package will always run ALL the assertions when return all the ones that failed.
If you want a behavior that stops the tests at the first fail use Require.
func TestSomething(t *testing.T) {
// Opcionally pass an error message if the test fails
assert.Equal(t, 12, 12, "they should be equal")
// If error message not passed, testify will generate one very detailed
assert.Nil(t, nil)
}
If writing multiple assets, declare it at the top, to avoid having to pass t at each one of them.
The suite package does not support parallel tests.
This package provides functionality that you might be used to from more commom OO languages.
With it, you can build a testing suite as a struct, build setup/teardown methods and testing methods on your struct, and run them with go test as per normal.
For instance, create a complete test with lifecycles, separating unit tests from integration tests by putting them into different suites.
Member functions of the test suite, should start with the Test prefix, or they will NOT be executed.
import "github.com/stretchr/testify/suite"
type ExampleSuite struct {
suite.Suite
VariableAvailableToSuite int
}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleSuite(t *testing.T) {
suite.Run(t, &ExampleSuite{})
}
// Will run before EACH test, and set the variable to "5"
func (suite *ExampleSuite) SetupTest() {
suite.VariableAvailableToSuite = 5
}
func (suite *ExampleSuite) TearDownTest() {
suite.T().Log("Test finishes")
}
// All methods that begin with "Test" are run as tests within a suite
func (suite *ExampleSuite) TestExample() {
// If using `assert` you must pass the `testing.T`
assert.Equal(suite.T(), 5, suite.VariableAvailableToSuite)
// Or call assert directly from suite
suite.Equal(5, suite.VariableAvailableToSuite)
}
Lifecycle methods
AfterTest and BeforeTest are useful because they have access to the test name being runned, so if you must do additional logic to only specific tests, use them.
BeforeTest is called afterSetupTest.
AfterTest is called beforeTearDownTest.
Here the lifecycle methods in the order they are called:
SetupSuite(): Executes before all the tests in the suite are run.
SetupTest(): Executes before each tests starts.
BeforeTest(suiteName, testName string): Execute right before each test starts.
AfterTest(suiteName, testName string): Execute right after the each test finishes.
TearDownTest(): Executes after each test in the suite.
TearDownSuite(): Executes after all the tests in the suite have been run.