Skip to content

Commit

Permalink
Merge pull request #261 from nikolaigut/main
Browse files Browse the repository at this point in the history
Add project name to junit.xml output
  • Loading branch information
dnephin authored Aug 6, 2022
2 parents de44b55 + 20f51e0 commit 9fea242
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand Down
4 changes: 4 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -152,6 +155,7 @@ type options struct {
hideSummary *hideSummaryValue
junitTestSuiteNameFormat *junitFieldFormatValue
junitTestCaseClassnameFormat *junitFieldFormatValue
junitProjectName string
rerunFailsMaxAttempts int
rerunFailsMaxInitialFailures int
rerunFailsReportFile string
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/gotestsum-help-text
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions internal/junitxml/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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{
Expand Down
6 changes: 5 additions & 1 deletion internal/junitxml/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/junitxml/testdata/junitxml-report.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuites name="test" tests="59" failures="13" errors="1" time="2.1">
<testsuite tests="0" failures="0" time="0.001000" name="gotest.tools/gotestsum/testjson/internal/badmain" timestamp="0001-01-01T00:00:00Z">
<properties>
<property name="go.version" value="go7.7.7"></property>
Expand Down

0 comments on commit 9fea242

Please sign in to comment.