...

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  	// 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  
		70  func parallelTestHelper(t *T) {
		71  	var wg sync.WaitGroup
		72  	for i := 0; i < 5; i++ {
		73  		wg.Add(1)
		74  		go func() {
		75  			notHelperCallingHelper(t, "parallel")
		76  			wg.Done()
		77  		}()
		78  	}
		79  	wg.Wait()
		80  }
		81  

View as plain text