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