Testing
Run tests
Search recursively and run the ALL tests:
go test ./...Search and run tests on a specific folder:
go test <path>Run specific test suite:
go test <path> -run TestSuitName
# or
go test ./... -run TestSuitNameThe biggest third-part testing package that helps writing assertions, mocks and testing suite interfaces and functions.
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.
If writing multiple assets, declare it at the top, to avoid having to pass t at each one of them.
Require
The require package provides the same global functions as the assert, but they terminate the tests at the first assertion fail.
This package provides a mechanism for easily writing mock objects that can be used in place of real objects.
Have in mind that mocks that are created in SetupSuit are shared between tests.
Instead recreate mocks for each test with SetupTest.
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.
Lifecycle methods
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.
Example
Last updated