Source file
src/testing/helper_test.go
1
2
3
4
5 package testing_test
6
7 import (
8 "internal/testenv"
9 "os"
10 "regexp"
11 "strings"
12 "testing"
13 )
14
15 func TestTBHelper(t *testing.T) {
16 if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
17 testTestHelper(t)
18
19
20
21 t.Helper()
22 t.Error("8")
23 return
24 }
25
26 t.Parallel()
27
28 cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestTBHelper$")
29 cmd = testenv.CleanCmdEnv(cmd)
30 cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
31 out, _ := cmd.CombinedOutput()
32
33 want := `--- FAIL: TestTBHelper \([^)]+\)
34 helperfuncs_test.go:15: 0
35 helperfuncs_test.go:47: 1
36 helperfuncs_test.go:24: 2
37 helperfuncs_test.go:49: 3
38 helperfuncs_test.go:56: 4
39 --- FAIL: TestTBHelper/sub \([^)]+\)
40 helperfuncs_test.go:59: 5
41 helperfuncs_test.go:24: 6
42 helperfuncs_test.go:58: 7
43 --- FAIL: TestTBHelper/sub2 \([^)]+\)
44 helperfuncs_test.go:80: 11
45 helperfuncs_test.go:84: recover 12
46 helperfuncs_test.go:86: GenericFloat64
47 helperfuncs_test.go:87: GenericInt
48 helper_test.go:22: 8
49 helperfuncs_test.go:73: 9
50 helperfuncs_test.go:69: 10
51 `
52 if !regexp.MustCompile(want).Match(out) {
53 t.Errorf("got output:\n\n%s\nwant matching:\n\n%s", out, want)
54 }
55 }
56
57 func TestTBHelperParallel(t *testing.T) {
58 if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
59 parallelTestHelper(t)
60 return
61 }
62
63 t.Parallel()
64
65 cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestTBHelperParallel$")
66 cmd = testenv.CleanCmdEnv(cmd)
67 cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
68 out, _ := cmd.CombinedOutput()
69
70 t.Logf("output:\n%s", out)
71
72 lines := strings.Split(strings.TrimSpace(string(out)), "\n")
73
74
75
76
77 const wantLines = 7
78
79 if len(lines) != wantLines {
80 t.Fatalf("parallelTestHelper gave %d lines of output; want %d", len(lines), wantLines)
81 }
82 want := "helperfuncs_test.go:24: parallel"
83 if got := strings.TrimSpace(lines[1]); got != want {
84 t.Errorf("got second output line %q; want %q", got, want)
85 }
86 }
87
88
89 func TestHelperRange(t *testing.T) {
90 if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
91 rangeHelperHelper(t)
92 return
93 }
94
95 t.Parallel()
96
97 cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestHelperRange$")
98 cmd = testenv.CleanCmdEnv(cmd)
99 cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
100 out, _ := cmd.CombinedOutput()
101 want := `--- FAIL: TestHelperRange \([^)]+\)
102 helperfuncs_test.go:139: range
103 helperfuncs_test.go:139: range
104 `
105 if !regexp.MustCompile(want).Match(out) {
106 t.Errorf("got output:\n\n%s\nwant matching:\n\n%s", out, want)
107 }
108 }
109
110 func BenchmarkTBHelper(b *testing.B) {
111 f1 := func() {
112 b.Helper()
113 }
114 f2 := func() {
115 b.Helper()
116 }
117 b.ResetTimer()
118 b.ReportAllocs()
119 for i := 0; i < b.N; i++ {
120 if i&1 == 0 {
121 f1()
122 } else {
123 f2()
124 }
125 }
126 }
127
View as plain text