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

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

View as plain text