Standard library / testing
testing.T.Cleanup()
Register a callback executed after the test and subtests complete; callbacks run last-in-first-out. Expected stdout: 'body\nsecond\nfirst\nPASS\n'. Run as a _test.go file using the Go test harness.
Formal Syntax & Structure
go syntaxgo
func (c *T) Cleanup(f func())Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| f | func() | Function under test or callback to run. |
Working Example
Example Snippetgo
package main
import ( "fmt" "testing")
func TestReference(t *testing.T) { t.Cleanup(func() { fmt.Println("first") }) t.Cleanup(func() { fmt.Println("second") }) fmt.Println("body")}