Standard library / testing

testing.F.Cleanup()

Register cleanup callbacks in last-in-first-out order after this scope finishes. Useful for temporary resources. Expected process exit: 0; output contains ['PASS', 'body reached', 'second cleanup\nfirst cleanup']. Run as a _test.go file using the Go test harness.

Formal Syntax & Structure

go syntaxgo
func (c *F) Cleanup(f func())

Parameters & Attributes

NameTypeDescription
ffunc()Function under test or callback to run.

Working Example

Example Snippetgo
package main
import (  "fmt"  "testing")
func FuzzReference(f *testing.F) {  f.Cleanup(func() { fmt.Println("first cleanup") })  f.Cleanup(func() { fmt.Println("second cleanup") })  fmt.Println("body reached")  f.Add("demo")  f.Fuzz(func(t *testing.T, s string) {    if s != "demo" {      t.Fatal(s)    }  })}
Go Reference