| 1 |
|
| 2 |
[short] skip |
| 3 |
|
| 4 |
# Baseline run. |
| 5 |
go test -cover example/foo |
| 6 |
stdout 'coverage: 50.0% of statements$' |
| 7 |
|
| 8 |
# Coverage percentage output should mention -coverpkg selection. |
| 9 |
go test -coverpkg=example/foo example/foo |
| 10 |
stdout 'coverage: 50.0% of statements in example/foo' |
| 11 |
|
| 12 |
# Try to ask for coverage of a package that doesn't exist. |
| 13 |
go test -coverpkg nonexistent example/bar |
| 14 |
stderr 'no packages being tested depend on matches for pattern nonexistent' |
| 15 |
stdout 'coverage: \[no statements\]' |
| 16 |
|
| 17 |
# Ask for foo coverage, but test bar. |
| 18 |
go test -coverpkg=example/foo example/bar |
| 19 |
stdout 'coverage: 50.0% of statements in example/foo' |
| 20 |
|
| 21 |
# end of test cmds, start of harness and related files. |
| 22 |
|
| 23 |
-- go.mod -- |
| 24 |
module example |
| 25 |
|
| 26 |
go 1.18 |
| 27 |
|
| 28 |
-- foo/foo.go -- |
| 29 |
package foo |
| 30 |
|
| 31 |
func FooFunc() int { |
| 32 |
return 42 |
| 33 |
} |
| 34 |
func FooFunc2() int { |
| 35 |
return 42 |
| 36 |
} |
| 37 |
|
| 38 |
-- foo/foo_test.go -- |
| 39 |
package foo |
| 40 |
|
| 41 |
import "testing" |
| 42 |
|
| 43 |
func TestFoo(t *testing.T) { |
| 44 |
if FooFunc() != 42 { |
| 45 |
t.Fatalf("bad") |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
-- bar/bar.go -- |
| 50 |
package bar |
| 51 |
|
| 52 |
import "example/foo" |
| 53 |
|
| 54 |
func BarFunc() int { |
| 55 |
return foo.FooFunc2() |
| 56 |
} |
| 57 |
|
| 58 |
-- bar/bar_test.go -- |
| 59 |
package bar_test |
| 60 |
|
| 61 |
import ( |
| 62 |
"example/bar" |
| 63 |
"testing" |
| 64 |
) |
| 65 |
|
| 66 |
func TestBar(t *testing.T) { |
| 67 |
if bar.BarFunc() != 42 { |
| 68 |
t.Fatalf("bad") |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
-- baz/baz.go -- |
| 73 |
package baz |
| 74 |
|
| 75 |
func BazFunc() int { |
| 76 |
return -42 |
| 77 |
} |