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

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

View as plain text