Skip to content

Commit

Permalink
Add tests for running with -cover
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Jun 26, 2019
1 parent 48f45d7 commit 4445661
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 0 deletions.
21 changes: 21 additions & 0 deletions testjson/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"gotest.tools/assert"
)

Expand All @@ -22,3 +23,23 @@ func TestPackage_Elapsed(t *testing.T) {
}
assert.Equal(t, pkg.Elapsed(), 3100*time.Millisecond)
}

func TestExecution_Add_PackageCoverage(t *testing.T) {
exec := NewExecution()
exec.add(TestEvent{
Package: "mytestpkg",
Action: ActionOutput,
Output: "coverage: 33.1% of statements\n",
})

pkg := exec.Package("mytestpkg")
expected := &Package{
coverage: "coverage: 33.1% of statements",
output: map[string][]string{
"": {"coverage: 33.1% of statements\n"},
},
}
assert.DeepEqual(t, pkg, expected, cmpPackage)
}

var cmpPackage = cmp.AllowUnexported(Package{})
58 changes: 58 additions & 0 deletions testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ func TestScanTestOutputWithShortFormat(t *testing.T) {
assert.DeepEqual(t, exec, expectedExecution, cmpExecutionShallow)
}

func TestScanTestOutputWithShortFormat_WithCoverage(t *testing.T) {
defer patchPkgPathPrefix("gotest.tools")()

shim := newFakeHandler(shortFormat, "go-test-json-with-cover")
exec, err := ScanTestOutput(shim.Config(t))

assert.NilError(t, err)
golden.Assert(t, shim.out.String(), "short-format-coverage.out")
golden.Assert(t, shim.err.String(), "short-format-coverage.err")
assert.DeepEqual(t, exec, expectedCoverageExecution, cmpExecutionShallow)
}

func TestScanTestOutputWithStandardVerboseFormat(t *testing.T) {
defer patchPkgPathPrefix("github.com/gotestyourself/gotestyourself")()

Expand All @@ -166,3 +178,49 @@ func TestScanTestOutputWithStandardQuietFormat(t *testing.T) {
golden.Assert(t, shim.err.String(), "standard-quiet-format.err")
assert.DeepEqual(t, exec, expectedExecution, cmpExecutionShallow)
}

func TestScanTestOutputWithStandardQuietFormat_WithCoverage(t *testing.T) {
defer patchPkgPathPrefix("gotest.tools")()

shim := newFakeHandler(standardQuietFormat, "go-test-json-with-cover")
exec, err := ScanTestOutput(shim.Config(t))

assert.NilError(t, err)
golden.Assert(t, shim.out.String(), "standard-quiet-format-coverage.out")
golden.Assert(t, shim.err.String(), "standard-quiet-format-coverage.err")
assert.DeepEqual(t, exec, expectedCoverageExecution, cmpExecutionShallow)
}

var expectedCoverageExecution = &Execution{
started: time.Now(),
errors: []string{"internal/broken/broken.go:5:21: undefined: somepackage"},
packages: map[string]*Package{
"gotest.tools/gotestsum/testjson/internal/good": {
Total: 18,
Skipped: []TestCase{
{Test: "TestSkipped"},
{Test: "TestSkippedWitLog"},
},
action: ActionPass,
coverage: "coverage: 0.0% of statements",
},
"gotest.tools/gotestsum/testjson/internal/stub": {
Total: 28,
Failed: []TestCase{
{Test: "TestFailed"},
{Test: "TestFailedWithStderr"},
{Test: "TestNestedWithFailure/c"},
{Test: "TestNestedWithFailure"},
},
Skipped: []TestCase{
{Test: "TestSkipped"},
{Test: "TestSkippedWitLog"},
},
action: ActionFail,
coverage: "coverage: 0.0% of statements",
},
"gotest.tools/gotestsum/testjson/internal/badmain": {
action: ActionFail,
},
},
}
6 changes: 6 additions & 0 deletions testjson/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ go test -p 1 -json -timeout 10ms -tags 'stubpkg timeout' ./internal/... \
go test -p 1 -json -tags 'stubpkg panic' ./internal/... \
> testdata/go-test-json-with-panic.out \
2> testdata/go-test-json-with-panic.err \
| true


go test -p 1 -json -tags stubpkg -cover ./internal/... \
> testdata/go-test-json-with-cover.out \
2> testdata/go-test-json-with-cover.err \
| true
2 changes: 2 additions & 0 deletions testjson/testdata/go-test-json-with-cover.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gotest.tools/gotestsum/testjson/internal/broken
internal/broken/broken.go:5:21: undefined: somepackage
233 changes: 233 additions & 0 deletions testjson/testdata/go-test-json-with-cover.out

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions testjson/testdata/short-format-coverage.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gotest.tools/gotestsum/testjson/internal/broken
internal/broken/broken.go:5:21: undefined: somepackage
3 changes: 3 additions & 0 deletions testjson/testdata/short-format-coverage.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
✖ gotestsum/testjson/internal/badmain (1ms)
✓ gotestsum/testjson/internal/good (12ms) (coverage: 0.0% of statements)
✖ gotestsum/testjson/internal/stub (11ms) (coverage: 0.0% of statements)
2 changes: 2 additions & 0 deletions testjson/testdata/standard-quiet-format-coverage.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gotest.tools/gotestsum/testjson/internal/broken
internal/broken/broken.go:5:21: undefined: somepackage
5 changes: 5 additions & 0 deletions testjson/testdata/standard-quiet-format-coverage.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sometimes main can exit 2
FAIL gotest.tools/gotestsum/testjson/internal/badmain 0.001s
ok gotest.tools/gotestsum/testjson/internal/good 0.011s coverage: 0.0% of statements
FAIL
FAIL gotest.tools/gotestsum/testjson/internal/stub 0.011s

0 comments on commit 4445661

Please sign in to comment.