diff --git a/internal/junitxml/report.go b/internal/junitxml/report.go index 1b50e816..703ed619 100644 --- a/internal/junitxml/report.go +++ b/internal/junitxml/report.go @@ -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. @@ -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. @@ -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) } diff --git a/internal/junitxml/report_test.go b/internal/junitxml/report_test.go index d1240dd3..056588da 100644 --- a/internal/junitxml/report_test.go +++ b/internal/junitxml/report_test.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "runtime" "testing" + "time" "gotest.tools/gotestsum/testjson" "gotest.tools/v3/assert" @@ -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") } diff --git a/internal/junitxml/testdata/junitxml-report.golden b/internal/junitxml/testdata/junitxml-report.golden index de9a4f17..7f10842c 100644 --- a/internal/junitxml/testdata/junitxml-report.golden +++ b/internal/junitxml/testdata/junitxml-report.golden @@ -1,6 +1,6 @@ - + @@ -8,7 +8,7 @@ sometimes main can exit 2 FAIL github.com/gotestyourself/gotestyourself/testjson/internal/badmain 0.010s - + @@ -35,7 +35,7 @@ - + @@ -80,7 +80,7 @@ - + diff --git a/testjson/execution.go b/testjson/execution.go index 8be09395..a5c2f94c 100644 --- a/testjson/execution.go +++ b/testjson/execution.go @@ -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 {