From 31cc95bdd32f6d94f69dc91d16fc22f76dcfce98 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Tue, 15 Jun 2021 21:47:24 +0000 Subject: [PATCH 1/2] junit: Add timestamp for suite start time The junit schema defines this attribute on the testsuite. In my case I need to import test results totally separate from the testing pipeline since these are in two different systems. This timestamp ensures the result processor puts the results into the right time frame instead of assuming it just ran. Signed-off-by: Brian Goff --- internal/junitxml/report.go | 2 ++ testjson/execution.go | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/internal/junitxml/report.go b/internal/junitxml/report.go index 1b50e816..d679a8cc 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. @@ -92,6 +93,7 @@ func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites { Properties: packageProperties(version), TestCases: packageTestCases(pkg, cfg.FormatTestCaseClassname), Failures: len(pkg.Failed), + Timestamp: exec.Started().Format(time.RFC3339), } suites.Suites = append(suites.Suites, junitpkg) } 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 { From f2b0a2ea4f1b2002bfcbb83148e116fb1957aae7 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Tue, 15 Jun 2021 21:59:11 +0000 Subject: [PATCH 2/2] Use zero timestamp for tests and update golden Signed-off-by: Brian Goff --- internal/junitxml/report.go | 7 ++++++- internal/junitxml/report_test.go | 3 ++- internal/junitxml/testdata/junitxml-report.golden | 8 ++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/internal/junitxml/report.go b/internal/junitxml/report.go index d679a8cc..703ed619 100644 --- a/internal/junitxml/report.go +++ b/internal/junitxml/report.go @@ -66,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. @@ -93,7 +95,10 @@ func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites { Properties: packageProperties(version), TestCases: packageTestCases(pkg, cfg.FormatTestCaseClassname), Failures: len(pkg.Failed), - Timestamp: exec.Started().Format(time.RFC3339), + 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 @@ - +