Skip to content

Commit 012a85e

Browse files
authored
Merge pull request #201 from cpuguy83/add_suite_timestamp
junit: Add timestamp for suite start time
2 parents bdcb1a9 + f2b0a2e commit 012a85e

4 files changed

Lines changed: 17 additions & 5 deletions

File tree

internal/junitxml/report.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type JUnitTestSuite struct {
3131
Name string `xml:"name,attr"`
3232
Properties []JUnitProperty `xml:"properties>property,omitempty"`
3333
TestCases []JUnitTestCase
34+
Timestamp string `xml:"timestamp,attr"`
3435
}
3536

3637
// JUnitTestCase is a single test case with its result.
@@ -65,6 +66,8 @@ type JUnitFailure struct {
6566
type Config struct {
6667
FormatTestSuiteName FormatFunc
6768
FormatTestCaseClassname FormatFunc
69+
// This is used for tests to have a consistent timestamp
70+
customTimestamp string
6871
}
6972

7073
// FormatFunc converts a string from one format into another.
@@ -92,6 +95,10 @@ func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites {
9295
Properties: packageProperties(version),
9396
TestCases: packageTestCases(pkg, cfg.FormatTestCaseClassname),
9497
Failures: len(pkg.Failed),
98+
Timestamp: cfg.customTimestamp,
99+
}
100+
if cfg.customTimestamp == "" {
101+
junitpkg.Timestamp = exec.Started().Format(time.RFC3339)
95102
}
96103
suites.Suites = append(suites.Suites, junitpkg)
97104
}

internal/junitxml/report_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"runtime"
99
"testing"
10+
"time"
1011

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

2122
defer env.Patch(t, "GOVERSION", "go7.7.7")()
22-
err := Write(out, exec, Config{})
23+
err := Write(out, exec, Config{customTimestamp: new(time.Time).Format(time.RFC3339)})
2324
assert.NilError(t, err)
2425
golden.Assert(t, out.String(), "junitxml-report.golden")
2526
}

internal/junitxml/testdata/junitxml-report.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<testsuites>
3-
<testsuite tests="0" failures="0" time="0.000000" name="github.com/gotestyourself/gotestyourself/testjson/internal/badmain">
3+
<testsuite tests="0" failures="0" time="0.000000" name="github.com/gotestyourself/gotestyourself/testjson/internal/badmain" timestamp="0001-01-01T00:00:00Z">
44
<properties>
55
<property name="go.version" value="go7.7.7"></property>
66
</properties>
77
<testcase classname="" name="TestMain" time="0.000000">
88
<failure message="Failed" type="">sometimes main can exit 2&#xA;FAIL&#x9;github.com/gotestyourself/gotestyourself/testjson/internal/badmain&#x9;0.010s&#xA;</failure>
99
</testcase>
1010
</testsuite>
11-
<testsuite tests="18" failures="0" time="0.020000" name="github.com/gotestyourself/gotestyourself/testjson/internal/good">
11+
<testsuite tests="18" failures="0" time="0.020000" name="github.com/gotestyourself/gotestyourself/testjson/internal/good" timestamp="0001-01-01T00:00:00Z">
1212
<properties>
1313
<property name="go.version" value="go7.7.7"></property>
1414
</properties>
@@ -35,7 +35,7 @@
3535
<testcase classname="github.com/gotestyourself/gotestyourself/testjson/internal/good" name="TestParallelTheSecond" time="0.010000"></testcase>
3636
<testcase classname="github.com/gotestyourself/gotestyourself/testjson/internal/good" name="TestParallelTheFirst" time="0.010000"></testcase>
3737
</testsuite>
38-
<testsuite tests="28" failures="4" time="0.020000" name="github.com/gotestyourself/gotestyourself/testjson/internal/stub">
38+
<testsuite tests="28" failures="4" time="0.020000" name="github.com/gotestyourself/gotestyourself/testjson/internal/stub" timestamp="0001-01-01T00:00:00Z">
3939
<properties>
4040
<property name="go.version" value="go7.7.7"></property>
4141
</properties>
@@ -80,7 +80,7 @@
8080
<testcase classname="github.com/gotestyourself/gotestyourself/testjson/internal/stub" name="TestParallelTheSecond" time="0.010000"></testcase>
8181
<testcase classname="github.com/gotestyourself/gotestyourself/testjson/internal/stub" name="TestParallelTheFirst" time="0.010000"></testcase>
8282
</testsuite>
83-
<testsuite tests="0" failures="0" time="0.000000" name="gotest.tools/gotestsum/internal/empty">
83+
<testsuite tests="0" failures="0" time="0.000000" name="gotest.tools/gotestsum/internal/empty" timestamp="0001-01-01T00:00:00Z">
8484
<properties>
8585
<property name="go.version" value="go7.7.7"></property>
8686
</properties>

testjson/execution.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ func (e *Execution) end() []TestEvent {
564564
return result
565565
}
566566

567+
func (e *Execution) Started() time.Time {
568+
return e.started
569+
}
570+
567571
// newExecution returns a new Execution and records the current time as the
568572
// time the test execution started.
569573
func newExecution() *Execution {

0 commit comments

Comments
 (0)