...

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

Documentation: cmd/go/testdata/script

     1
     2# Rudimentary test of testing.Coverage().
     3
     4[short] skip
     5[!GOEXPERIMENT:coverageredesign] skip
     6
     7# Simple test.
     8go test -v -cover -count=1
     9
    10# Make sure test still passes when test executable is built and
    11# run outside the go command.
    12go test -c -o t.exe -cover
    13exec ./t.exe
    14
    15-- go.mod --
    16module hello
    17
    18go 1.20
    19-- hello.go --
    20package hello
    21
    22func Hello() {
    23	println("hello")
    24}
    25
    26// contents not especially interesting, just need some code
    27func 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 --
    41package hello
    42
    43import "testing"
    44
    45func 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

View as plain text