diff --git a/cmd/handler.go b/cmd/handler.go index 773ceac0..d67284f2 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -93,6 +93,7 @@ func writeJUnitFile(opts *options, execution *testjson.Execution) error { }() return junitxml.Write(junitFile, execution, junitxml.Config{ + ProjectName: opts.junitProjectName, FormatTestSuiteName: opts.junitTestSuiteNameFormat.Value(), FormatTestCaseClassname: opts.junitTestCaseClassnameFormat.Value(), }) diff --git a/cmd/main.go b/cmd/main.go index 020b1cef..553cad5c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -88,6 +88,9 @@ func setupFlags(name string) (*pflag.FlagSet, *options) { "format the testsuite name field as: "+junitFieldFormatValues) flags.Var(opts.junitTestCaseClassnameFormat, "junitfile-testcase-classname", "format the testcase classname field as: "+junitFieldFormatValues) + flags.StringVar(&opts.junitProjectName, "junitfile-project-name", + lookEnvWithDefault("GOTESTSUM_JUNITFILE_PROJECT_NAME", ""), + "name of the project used in the junit.xml file") flags.IntVar(&opts.rerunFailsMaxAttempts, "rerun-fails", 0, "rerun failed tests until they all pass, or attempts exceeds maximum. Defaults to max 2 reruns when enabled.") @@ -152,6 +155,7 @@ type options struct { hideSummary *hideSummaryValue junitTestSuiteNameFormat *junitFieldFormatValue junitTestCaseClassnameFormat *junitFieldFormatValue + junitProjectName string rerunFailsMaxAttempts int rerunFailsMaxInitialFailures int rerunFailsReportFile string diff --git a/cmd/testdata/gotestsum-help-text b/cmd/testdata/gotestsum-help-text index 430f9755..9e468823 100644 --- a/cmd/testdata/gotestsum-help-text +++ b/cmd/testdata/gotestsum-help-text @@ -8,6 +8,7 @@ Flags: --hide-summary summary hide sections of the summary: skipped,failed,errors,output (default none) --jsonfile string write all TestEvents to file --junitfile string write a JUnit XML file + --junitfile-project-name string name of the project used in the junit.xml file --junitfile-testcase-classname field-format format the testcase classname field as: full, relative, short (default full) --junitfile-testsuite-name field-format format the testsuite name field as: full, relative, short (default full) --max-fails int end the test run after this number of failures diff --git a/internal/junitxml/report.go b/internal/junitxml/report.go index 8520969f..cd23b1f4 100644 --- a/internal/junitxml/report.go +++ b/internal/junitxml/report.go @@ -17,8 +17,13 @@ import ( // JUnitTestSuites is a collection of JUnit test suites. type JUnitTestSuites struct { - XMLName xml.Name `xml:"testsuites"` - Suites []JUnitTestSuite + XMLName xml.Name `xml:"testsuites"` + Name string `xml:"name,attr,omitempty"` + Tests int `xml:"tests,attr"` + Failures int `xml:"failures,attr"` + Errors int `xml:"errors,attr"` + Time string `xml:"time,attr"` + Suites []JUnitTestSuite } // JUnitTestSuite is a single JUnit test suite which may contain many @@ -64,10 +69,12 @@ type JUnitFailure struct { // Config used to write a junit XML document. type Config struct { + ProjectName string FormatTestSuiteName FormatFunc FormatTestCaseClassname FormatFunc // This is used for tests to have a consistent timestamp customTimestamp string + customElapsed string } // FormatFunc converts a string from one format into another. @@ -84,8 +91,17 @@ func Write(out io.Writer, exec *testjson.Execution, cfg Config) error { func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites { cfg = configWithDefaults(cfg) version := goVersion() - suites := JUnitTestSuites{} + suites := JUnitTestSuites{ + Name: cfg.ProjectName, + Tests: exec.Total(), + Failures: len(exec.Failed()), + Errors: len(exec.Errors()), + Time: formatDurationAsSeconds(time.Since(exec.Started())), + } + if cfg.customElapsed != "" { + suites.Time = cfg.customElapsed + } for _, pkgname := range exec.Packages() { pkg := exec.Package(pkgname) junitpkg := JUnitTestSuite{ diff --git a/internal/junitxml/report_test.go b/internal/junitxml/report_test.go index 295a247e..14e8afb3 100644 --- a/internal/junitxml/report_test.go +++ b/internal/junitxml/report_test.go @@ -20,7 +20,11 @@ func TestWrite(t *testing.T) { exec := createExecution(t) env.Patch(t, "GOVERSION", "go7.7.7") - err := Write(out, exec, Config{customTimestamp: new(time.Time).Format(time.RFC3339)}) + err := Write(out, exec, Config{ + ProjectName: "test", + customTimestamp: new(time.Time).Format(time.RFC3339), + customElapsed: "2.1", + }) 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 0ca2d0e3..9988d8b0 100644 --- a/internal/junitxml/testdata/junitxml-report.golden +++ b/internal/junitxml/testdata/junitxml-report.golden @@ -1,5 +1,5 @@ - +