Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

junit: Add timestamp for suite start time #201

Merged
merged 2 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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