Test suites
Test suites organize multiple related tests into a cohesive unit. The testing
package in Go supports test suites that use the TestMain
function and the testing.M
type. TestMain
is a special function that can be used to perform setup and teardown logic for the entire test suite. This is particularly useful when you need to set up resources or configurations that are shared across multiple test suites.
Exercise 19.04 – using TestMain to execute several test functions
Consider a scenario where you have multiple test functions that you want to be grouped into a test suite. Let’s see how that can be done using the native Go test framework:
- Create a new folder in your filesystem, and, inside it, create a
main_test.go
file and write the following:package main import ( "log" "testing" )
- Define a setup and teardown function:
func setup() { log.Println("setup() running") } func teardown...