Testing

Run tests

Search recursively and run the ALL tests:

go test ./...
circle-info

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.

circle-info

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.

circle-exclamation

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.

circle-info

You can use Mockeryarrow-up-right to auto generate mock functions from Structs.

triangle-exclamation

circle-info

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.

circle-exclamation

Lifecycle methods

circle-info

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 after SetupTest.

  • AfterTest is called before TearDownTest.

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