Source file src/testing/helper_test.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     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  		// Check that calling Helper from inside a top-level test function
    20  		// has no effect.
    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  	// We expect to see one "--- FAIL" line at the start
    75  	// of the log, five lines of "parallel" logging,
    76  	// and a final "FAIL" line at the end of the test.
    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  func BenchmarkTBHelper(b *testing.B) {
    89  	f1 := func() {
    90  		b.Helper()
    91  	}
    92  	f2 := func() {
    93  		b.Helper()
    94  	}
    95  	b.ResetTimer()
    96  	b.ReportAllocs()
    97  	for i := 0; i < b.N; i++ {
    98  		if i&1 == 0 {
    99  			f1()
   100  		} else {
   101  			f2()
   102  		}
   103  	}
   104  }
   105  

View as plain text