Standard library / testing

testing.B.Run()

Run a named subbenchmark. The return value reports whether the subbenchmark succeeded; benchmark timing is machine-dependent. Expected process exit: 0; output contains ['PASS', 'child passed true', 'BenchmarkReference/child']. Run as a _test.go file using the Go test harness.

Formal Syntax & Structure

go syntaxgo
func (b *B) Run(name string, f func(b *B)) bool

Parameters & Attributes

NameTypeDescription
namestringName of the nested test or benchmark.
ffunc(b *B)Function under test or callback to run.

Return Value

bool

Result of the operation described above.

Working Example

Example Snippetgo
package main
import (  "fmt"  "testing")
func BenchmarkReference(b *testing.B) {  ok := b.Run("child", func(b *testing.B) {    for i := 0; i < b.N; i++ {    }  })  fmt.Println("child passed", ok)}
Go Reference