aboutsummaryrefslogtreecommitdiff
path: root/src/testing/helper_test.go
blob: da5622f85f6bbdef99138af912bee97517be35cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package testing_test

import (
	"internal/testenv"
	"os"
	"regexp"
	"strings"
	"testing"
)

func TestTBHelper(t *testing.T) {
	if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
		testTestHelper(t)

		// Check that calling Helper from inside a top-level test function
		// has no effect.
		t.Helper()
		t.Error("8")
		return
	}

	testenv.MustHaveExec(t)
	t.Parallel()

	exe, err := os.Executable()
	if err != nil {
		t.Fatal(err)
	}

	cmd := testenv.Command(t, exe, "-test.run=^TestTBHelper$")
	cmd = testenv.CleanCmdEnv(cmd)
	cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
	out, _ := cmd.CombinedOutput()

	want := `--- FAIL: TestTBHelper \([^)]+\)
    helperfuncs_test.go:15: 0
    helperfuncs_test.go:47: 1
    helperfuncs_test.go:24: 2
    helperfuncs_test.go:49: 3
    helperfuncs_test.go:56: 4
    --- FAIL: TestTBHelper/sub \([^)]+\)
        helperfuncs_test.go:59: 5
        helperfuncs_test.go:24: 6
        helperfuncs_test.go:58: 7
    --- FAIL: TestTBHelper/sub2 \([^)]+\)
        helperfuncs_test.go:80: 11
    helperfuncs_test.go:84: recover 12
    helperfuncs_test.go:86: GenericFloat64
    helperfuncs_test.go:87: GenericInt
    helper_test.go:22: 8
    helperfuncs_test.go:73: 9
    helperfuncs_test.go:69: 10
`
	if !regexp.MustCompile(want).Match(out) {
		t.Errorf("got output:\n\n%s\nwant matching:\n\n%s", out, want)
	}
}

func TestTBHelperParallel(t *testing.T) {
	if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
		parallelTestHelper(t)
		return
	}

	testenv.MustHaveExec(t)
	t.Parallel()

	exe, err := os.Executable()
	if err != nil {
		t.Fatal(err)
	}

	cmd := testenv.Command(t, exe, "-test.run=^TestTBHelperParallel$")
	cmd = testenv.CleanCmdEnv(cmd)
	cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
	out, _ := cmd.CombinedOutput()

	t.Logf("output:\n%s", out)

	lines := strings.Split(strings.TrimSpace(string(out)), "\n")

	// We expect to see one "--- FAIL" line at the start
	// of the log, five lines of "parallel" logging,
	// and a final "FAIL" line at the end of the test.
	const wantLines = 7

	if len(lines) != wantLines {
		t.Fatalf("parallelTestHelper gave %d lines of output; want %d", len(lines), wantLines)
	}
	want := "helperfuncs_test.go:24: parallel"
	if got := strings.TrimSpace(lines[1]); got != want {
		t.Errorf("got second output line %q; want %q", got, want)
	}
}

func BenchmarkTBHelper(b *testing.B) {
	f1 := func() {
		b.Helper()
	}
	f2 := func() {
		b.Helper()
	}
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		if i&1 == 0 {
			f1()
		} else {
			f2()
		}
	}
}