Source file src/testing/helperfuncs_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
     6  
     7  import "sync"
     8  
     9  // The line numbering of this file is important for TestTBHelper.
    10  
    11  func notHelper(t *T, msg string) {
    12  	t.Error(msg)
    13  }
    14  
    15  func helper(t *T, msg string) {
    16  	t.Helper()
    17  	t.Error(msg)
    18  }
    19  
    20  func notHelperCallingHelper(t *T, msg string) {
    21  	helper(t, msg)
    22  }
    23  
    24  func helperCallingHelper(t *T, msg string) {
    25  	t.Helper()
    26  	helper(t, msg)
    27  }
    28  
    29  func testHelper(t *T) {
    30  	// Check combinations of directly and indirectly
    31  	// calling helper functions.
    32  	notHelper(t, "0")
    33  	helper(t, "1")
    34  	notHelperCallingHelper(t, "2")
    35  	helperCallingHelper(t, "3")
    36  
    37  	// Check a function literal closing over t that uses Helper.
    38  	fn := func(msg string) {
    39  		t.Helper()
    40  		t.Error(msg)
    41  	}
    42  	fn("4")
    43  
    44  	t.Run("sub", func(t *T) {
    45  		helper(t, "5")
    46  		notHelperCallingHelper(t, "6")
    47  		// Check that calling Helper from inside a subtest entry function
    48  		// works as if it were in an ordinary function call.
    49  		t.Helper()
    50  		t.Error("7")
    51  	})
    52  
    53  	// Check that calling Helper from inside a top-level test function
    54  	// has no effect.
    55  	t.Helper()
    56  	t.Error("8")
    57  
    58  	// Check that right caller is reported for func passed to Cleanup when
    59  	// multiple cleanup functions have been registered.
    60  	t.Cleanup(func() {
    61  		t.Helper()
    62  		t.Error("10")
    63  	})
    64  	t.Cleanup(func() {
    65  		t.Helper()
    66  		t.Error("9")
    67  	})
    68  
    69  	// Check that helper-ness propagates up through subtests
    70  	// to helpers above. See https://golang.org/issue/44887.
    71  	helperSubCallingHelper(t, "11")
    72  
    73  	// Check that helper-ness propagates up through panic/recover.
    74  	// See https://golang.org/issue/31154.
    75  	recoverHelper(t, "12")
    76  }
    77  
    78  func parallelTestHelper(t *T) {
    79  	var wg sync.WaitGroup
    80  	for i := 0; i < 5; i++ {
    81  		wg.Add(1)
    82  		go func() {
    83  			notHelperCallingHelper(t, "parallel")
    84  			wg.Done()
    85  		}()
    86  	}
    87  	wg.Wait()
    88  }
    89  
    90  func helperSubCallingHelper(t *T, msg string) {
    91  	t.Helper()
    92  	t.Run("sub2", func(t *T) {
    93  		t.Helper()
    94  		t.Fatal(msg)
    95  	})
    96  }
    97  
    98  func recoverHelper(t *T, msg string) {
    99  	t.Helper()
   100  	defer func() {
   101  		t.Helper()
   102  		if err := recover(); err != nil {
   103  			t.Errorf("recover %s", err)
   104  		}
   105  	}()
   106  	doPanic(t, msg)
   107  }
   108  
   109  func doPanic(t *T, msg string) {
   110  	t.Helper()
   111  	panic(msg)
   112  }
   113  

View as plain text