Skip to content

Commit

Permalink
Merge pull request #216 from dnephin/pkg-elapsed
Browse files Browse the repository at this point in the history
Use the event elapsed time for package elapsed
  • Loading branch information
dnephin authored Sep 4, 2021
2 parents b08915a + 5ec6ef6 commit e5de87c
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 146 deletions.
8 changes: 4 additions & 4 deletions internal/junitxml/testdata/junitxml-report.golden
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite tests="0" failures="0" time="0.000000" name="github.com/gotestyourself/gotestyourself/testjson/internal/badmain" timestamp="0001-01-01T00:00:00Z">
<testsuite tests="0" failures="0" time="0.010000" name="github.com/gotestyourself/gotestyourself/testjson/internal/badmain" timestamp="0001-01-01T00:00:00Z">
<properties>
<property name="go.version" value="go7.7.7"></property>
</properties>
<testcase classname="" name="TestMain" time="0.000000">
<failure message="Failed" type="">sometimes main can exit 2&#xA;FAIL&#x9;github.com/gotestyourself/gotestyourself/testjson/internal/badmain&#x9;0.010s&#xA;</failure>
</testcase>
</testsuite>
<testsuite tests="18" failures="0" time="0.020000" name="github.com/gotestyourself/gotestyourself/testjson/internal/good" timestamp="0001-01-01T00:00:00Z">
<testsuite tests="18" failures="0" time="0.000000" name="github.com/gotestyourself/gotestyourself/testjson/internal/good" timestamp="0001-01-01T00:00:00Z">
<properties>
<property name="go.version" value="go7.7.7"></property>
</properties>
Expand All @@ -35,7 +35,7 @@
<testcase classname="github.com/gotestyourself/gotestyourself/testjson/internal/good" name="TestParallelTheSecond" time="0.010000"></testcase>
<testcase classname="github.com/gotestyourself/gotestyourself/testjson/internal/good" name="TestParallelTheFirst" time="0.010000"></testcase>
</testsuite>
<testsuite tests="28" failures="4" time="0.020000" name="github.com/gotestyourself/gotestyourself/testjson/internal/stub" timestamp="0001-01-01T00:00:00Z">
<testsuite tests="28" failures="4" time="0.011000" name="github.com/gotestyourself/gotestyourself/testjson/internal/stub" timestamp="0001-01-01T00:00:00Z">
<properties>
<property name="go.version" value="go7.7.7"></property>
</properties>
Expand Down Expand Up @@ -80,7 +80,7 @@
<testcase classname="github.com/gotestyourself/gotestyourself/testjson/internal/stub" name="TestParallelTheSecond" time="0.010000"></testcase>
<testcase classname="github.com/gotestyourself/gotestyourself/testjson/internal/stub" name="TestParallelTheFirst" time="0.010000"></testcase>
</testsuite>
<testsuite tests="0" failures="0" time="0.000000" name="gotest.tools/gotestsum/internal/empty" timestamp="0001-01-01T00:00:00Z">
<testsuite tests="0" failures="0" time="0.004000" name="gotest.tools/gotestsum/internal/empty" timestamp="0001-01-01T00:00:00Z">
<properties>
<property name="go.version" value="go7.7.7"></property>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions testjson/dotformat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func TestFmtDotElapsed(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.expected, func(t *testing.T) {
pkg := &Package{
cached: tc.cached,
Passed: []TestCase{{Elapsed: tc.elapsed}},
cached: tc.cached,
elapsed: tc.elapsed,
}
actual := fmtDotElapsed(pkg)
assert.Check(t, cmp.Equal(utf8.RuneCountInString(actual), 7))
Expand Down
13 changes: 7 additions & 6 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ type Package struct {
Skipped []TestCase
Passed []TestCase

// elapsed time reported by the pass or fail event for the package.
elapsed time.Duration

// mapping of root TestCase ID to all sub test IDs. Used to mitigate
// github.com/golang/go/issues/29755, and github.com/golang/go/issues/40771.
// In the future when those bug are fixed this mapping can likely be removed.
Expand Down Expand Up @@ -112,13 +115,10 @@ func (p *Package) Result() Action {
return p.action
}

// Elapsed returns the sum of the elapsed time for all tests in the package.
// Elapsed returns the elapsed time of the package, as reported by the
// pass or fail event for the package.
func (p *Package) Elapsed() time.Duration {
elapsed := time.Duration(0)
for _, testcase := range p.TestCases() {
elapsed += testcase.Elapsed
}
return elapsed
return p.elapsed
}

// TestCases returns all the test cases.
Expand Down Expand Up @@ -339,6 +339,7 @@ func (p *Package) addEvent(event TestEvent) {
switch event.Action {
case ActionPass, ActionFail:
p.action = event.Action
p.elapsed = elapsedDuration(event.Elapsed)
case ActionOutput:
if isCoverageOutput(event.Output) {
p.coverage = strings.TrimRight(event.Output, "\n")
Expand Down
20 changes: 2 additions & 18 deletions testjson/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@ import (
"gotest.tools/v3/golden"
)

func TestPackage_Elapsed(t *testing.T) {
pkg := &Package{
Failed: []TestCase{
{Elapsed: 300 * time.Millisecond},
},
Passed: []TestCase{
{Elapsed: 200 * time.Millisecond},
{Elapsed: 2500 * time.Millisecond},
},
Skipped: []TestCase{
{Elapsed: 100 * time.Millisecond},
},
}
assert.Equal(t, pkg.Elapsed(), 3100*time.Millisecond)
}

func TestExecution_Add_PackageCoverage(t *testing.T) {
exec := newExecution()
exec.add(TestEvent{
Expand Down Expand Up @@ -144,7 +128,7 @@ func TestPackage_AddEvent(t *testing.T) {
{
name: "package failed",
event: `{"Action":"fail","Package":"gotest.tools/testing","Elapsed":0.012}`,
expected: Package{action: ActionFail},
expected: Package{action: ActionFail, elapsed: 12 * time.Millisecond},
},
{
name: "package is cached",
Expand All @@ -157,7 +141,7 @@ func TestPackage_AddEvent(t *testing.T) {
{
name: "package pass",
event: `{"Action":"pass","Package":"gotest.tools/testing","Elapsed":0.012}`,
expected: Package{action: ActionPass},
expected: Package{action: ActionPass, elapsed: 12 * time.Millisecond},
},
}

Expand Down
8 changes: 7 additions & 1 deletion testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,18 @@ var expectedExecution = &Execution{
{Test: "TestSkipped"},
{Test: "TestSkippedWitLog"},
},
elapsed: 11 * time.Millisecond,
action: ActionFail,
running: map[string]TestCase{},
},
"github.com/gotestyourself/gotestyourself/testjson/internal/badmain": {
action: ActionFail,
running: map[string]TestCase{},
elapsed: 10 * time.Millisecond,
},
"gotest.tools/gotestsum/internal/empty": {
action: ActionPass,
action: ActionPass,
elapsed: 4 * time.Millisecond,
},
},
}
Expand Down Expand Up @@ -224,6 +227,7 @@ var expectedCoverageExecution = &Execution{
{Test: "TestSkipped"},
{Test: "TestSkippedWitLog"},
},
elapsed: 12 * time.Millisecond,
action: ActionPass,
coverage: "coverage: 0.0% of statements",
running: map[string]TestCase{},
Expand All @@ -240,13 +244,15 @@ var expectedCoverageExecution = &Execution{
{Test: "TestSkipped"},
{Test: "TestSkippedWitLog"},
},
elapsed: 11 * time.Millisecond,
action: ActionFail,
coverage: "coverage: 0.0% of statements",
running: map[string]TestCase{},
},
"gotest.tools/gotestsum/testjson/internal/badmain": {
action: ActionFail,
running: map[string]TestCase{},
elapsed: time.Millisecond,
},
},
}
Loading

0 comments on commit e5de87c

Please sign in to comment.