...
Run Format

Source file src/testing/helperfuncs_test.go

Documentation: testing

     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  
    59  func parallelTestHelper(t *T) {
    60  	var wg sync.WaitGroup
    61  	for i := 0; i < 5; i++ {
    62  		wg.Add(1)
    63  		go func() {
    64  			notHelperCallingHelper(t, "parallel")
    65  			wg.Done()
    66  		}()
    67  	}
    68  	wg.Wait()
    69  }
    70  

View as plain text