Testing
Run tests
Search recursively and run the ALL tests:
go test ./...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 TestSuitNameThe 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.
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.
You can use Mockery to auto generate mock functions from Structs.
Have in mind that mocks that are created in SetupSuit are shared between tests.
Instead recreate mocks for each test with SetupTest.
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.
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.
BeforeTestis called afterSetupTest.AfterTestis 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.
Example
Last updated