Skip to content

Commit

Permalink
Merge pull request #201 from cpuguy83/add_suite_timestamp
Browse files Browse the repository at this point in the history
junit: Add timestamp for suite start time
  • Loading branch information
dnephin authored Jun 16, 2021
2 parents bdcb1a9 + f2b0a2e commit 012a85e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
7 changes: 7 additions & 0 deletions internal/junitxml/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type JUnitTestSuite struct {
Name string `xml:"name,attr"`
Properties []JUnitProperty `xml:"properties>property,omitempty"`
TestCases []JUnitTestCase
Timestamp string `xml:"timestamp,attr"`
}

// JUnitTestCase is a single test case with its result.
Expand Down Expand Up @@ -65,6 +66,8 @@ type JUnitFailure struct {
type Config struct {
FormatTestSuiteName FormatFunc
FormatTestCaseClassname FormatFunc
// This is used for tests to have a consistent timestamp
customTimestamp string
}

// FormatFunc converts a string from one format into another.
Expand Down Expand Up @@ -92,6 +95,10 @@ func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites {
Properties: packageProperties(version),
TestCases: packageTestCases(pkg, cfg.FormatTestCaseClassname),
Failures: len(pkg.Failed),
Timestamp: cfg.customTimestamp,
}
if cfg.customTimestamp == "" {
junitpkg.Timestamp = exec.Started().Format(time.RFC3339)
}
suites.Suites = append(suites.Suites, junitpkg)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/junitxml/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"runtime"
"testing"
"time"

"gotest.tools/gotestsum/testjson"
"gotest.tools/v3/assert"
Expand All @@ -19,7 +20,7 @@ func TestWrite(t *testing.T) {
exec := createExecution(t)

defer env.Patch(t, "GOVERSION", "go7.7.7")()
err := Write(out, exec, Config{})
err := Write(out, exec, Config{customTimestamp: new(time.Time).Format(time.RFC3339)})
assert.NilError(t, err)
golden.Assert(t, out.String(), "junitxml-report.golden")
}
Expand Down
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">
<testsuite tests="0" failures="0" time="0.000000" 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">
<testsuite tests="18" failures="0" time="0.020000" 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">
<testsuite tests="28" failures="4" time="0.020000" 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">
<testsuite tests="0" failures="0" time="0.000000" 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: 4 additions & 0 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ func (e *Execution) end() []TestEvent {
return result
}

func (e *Execution) Started() time.Time {
return e.started
}

// newExecution returns a new Execution and records the current time as the
// time the test execution started.
func newExecution() *Execution {
Expand Down

0 comments on commit 012a85e

Please sign in to comment.