Standard library / testing

testing.M

Handle passed to TestMain for running the test suite. Perform teardown before os.Exit because os.Exit does not execute deferred calls. Expected process exit: 0; output contains ['setup', 'test body', 'PASS', 'teardown 0']. Run as a _test.go file using the Go test harness.

Formal Syntax & Structure

go syntaxgo
type M struct {}

Working Example

Example Snippetgo
package main
import (  "fmt"  "os"  "testing")
func TestMain(m *testing.M) {  fmt.Println("setup")  code := m.Run()  fmt.Println("teardown", code)  os.Exit(code)}func TestReference(t *testing.T) { fmt.Println("test body") }
Go Reference