...
1# Test that -benchtime 1x only runs a total of 1 loop iteration.
2# See golang.org/issue/32051.
3
4go test -run ^$ -bench . -benchtime 1x
5
6-- go.mod --
7module bench
8
9go 1.16
10-- x_test.go --
11package bench
12
13import (
14 "fmt"
15 "os"
16 "testing"
17)
18
19var called = false
20
21func TestMain(m *testing.M) {
22 m.Run()
23 if !called {
24 fmt.Println("benchmark never called")
25 os.Exit(1)
26 }
27}
28
29func Benchmark(b *testing.B) {
30 if b.N > 1 {
31 b.Fatalf("called with b.N=%d; want b.N=1 only", b.N)
32 }
33 if called {
34 b.Fatal("called twice")
35 }
36 called = true
37}
View as plain text