Text file src/cmd/go/testdata/script/testing_coverage.txt

     1  
     2  # Rudimentary test of testing.Coverage().
     3  
     4  [short] skip
     5  [!GOEXPERIMENT:coverageredesign] skip
     6  
     7  # Simple test.
     8  go test -v -cover -count=1
     9  
    10  # Make sure test still passes when test executable is built and
    11  # run outside the go command.
    12  go test -c -o t.exe -cover
    13  exec ./t.exe
    14  
    15  -- go.mod --
    16  module hello
    17  
    18  go 1.20
    19  -- hello.go --
    20  package hello
    21  
    22  func Hello() {
    23  	println("hello")
    24  }
    25  
    26  // contents not especially interesting, just need some code
    27  func foo(n int) int {
    28  	t := 0
    29  	for i := 0; i < n; i++ {
    30  		for j := 0; j < i; j++ {
    31  			t += i ^ j
    32  			if t == 1010101 {
    33  				break
    34  			}
    35  		}
    36  	}
    37  	return t
    38  }
    39  
    40  -- hello_test.go --
    41  package hello
    42  
    43  import "testing"
    44  
    45  func TestTestCoverage(t *testing.T) {
    46  	Hello()
    47  	C1 := testing.Coverage()
    48  	foo(29)
    49  	C2 := testing.Coverage()
    50  	if C1 == 0.0 || C2 == 0.0 {
    51  		t.Errorf("unexpected zero values C1=%f C2=%f", C1, C2)
    52  	}
    53  	if C1 >= C2 {
    54  		t.Errorf("testing.Coverage() not monotonically increasing C1=%f C2=%f", C1, C2)
    55  	}
    56  }
    57  
    58  

View as plain text