From 6d2ab46d4f2ec7f19cdbf496470d30832a10f53d Mon Sep 17 00:00:00 2001 From: Nick Palmer Date: Tue, 13 Jan 2015 18:35:34 -0800 Subject: [PATCH 1/3] Add surrounding tag. * If a test log contains more than one package we need to wrap it in a element in order to be valid xml. Bamboo cannot read the file if it is not valid xml. --- go-junit-report_test.go | 2 +- junit-formatter.go | 10 ++++++++-- tests/01-report.xml | 16 +++++++++------- tests/02-report.xml | 20 +++++++++++--------- tests/03-report.xml | 20 +++++++++++--------- tests/04-report.xml | 16 +++++++++------- tests/05-report.xml | 16 +++++++++------- 7 files changed, 58 insertions(+), 42 deletions(-) diff --git a/go-junit-report_test.go b/go-junit-report_test.go index c268f0a..1d5e349 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -233,7 +233,7 @@ func TestJUnitFormatter(t *testing.T) { } if string(junitReport.Bytes()) != report { - t.Fatalf("Report xml ==\n%s, want\n%s", string(junitReport.Bytes()), report) + t.Fatalf("Fail: %s Report xml ==\n%s, want\n%s", testCase.name, string(junitReport.Bytes()), report) } } } diff --git a/junit-formatter.go b/junit-formatter.go index 2f1f887..03cff30 100644 --- a/junit-formatter.go +++ b/junit-formatter.go @@ -9,6 +9,11 @@ import ( "strings" ) +type JUnitTestSuites struct { + XMLName xml.Name `xml:"testsuites"` + Suites []JUnitTestSuite +} + type JUnitTestSuite struct { XMLName xml.Name `xml:"testsuite"` Tests int `xml:"tests,attr"` @@ -53,7 +58,7 @@ func NewJUnitProperty(name, value string) JUnitProperty { // JUnitReportXML writes a junit xml representation of the given report to w // in the format described at http://windyroad.org/dl/Open%20Source/JUnit.xsd func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error { - suites := []JUnitTestSuite{} + suites := JUnitTestSuites{} // convert Report to JUnit test suites for _, pkg := range report.Packages { @@ -100,7 +105,7 @@ func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error { ts.TestCases = append(ts.TestCases, testCase) } - suites = append(suites, ts) + suites.Suites = append(suites.Suites, ts) } // to xml @@ -114,6 +119,7 @@ func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error { if !noXmlHeader { writer.WriteString(xml.Header) } + writer.Write(bytes) writer.WriteByte('\n') writer.Flush() diff --git a/tests/01-report.xml b/tests/01-report.xml index 2585b80..f8f1038 100644 --- a/tests/01-report.xml +++ b/tests/01-report.xml @@ -1,8 +1,10 @@ - - - - - - - + + + + + + + + + diff --git a/tests/02-report.xml b/tests/02-report.xml index bfa33d7..23a4f0f 100644 --- a/tests/02-report.xml +++ b/tests/02-report.xml @@ -1,10 +1,12 @@ - - - - - - file_test.go:11: Error message file_test.go:11: Longer error message. - - - + + + + + + + file_test.go:11: Error message file_test.go:11: Longer error message. + + + + diff --git a/tests/03-report.xml b/tests/03-report.xml index 71f287d..e67e5ef 100644 --- a/tests/03-report.xml +++ b/tests/03-report.xml @@ -1,10 +1,12 @@ - - - - - - - - - + + + + + + + + + + + diff --git a/tests/04-report.xml b/tests/04-report.xml index 2585b80..f8f1038 100644 --- a/tests/04-report.xml +++ b/tests/04-report.xml @@ -1,8 +1,10 @@ - - - - - - - + + + + + + + + + diff --git a/tests/05-report.xml b/tests/05-report.xml index 5a372aa..125307a 100644 --- a/tests/05-report.xml +++ b/tests/05-report.xml @@ -1,7 +1,9 @@ - - - - - - - + + + + + + + + + From fc2896020a1b7b3283fdb5ce12a7a73d33a1a244 Mon Sep 17 00:00:00 2001 From: Nick Palmer Date: Tue, 13 Jan 2015 18:45:30 -0800 Subject: [PATCH 2/3] Add a test case which has more than one suite in it. --- go-junit-report_test.go | 52 +++++++++++++++++++++++++++++++++++++++++ tests/06-mixed.txt | 17 ++++++++++++++ tests/06-report.xml | 18 ++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 tests/06-mixed.txt create mode 100644 tests/06-report.xml diff --git a/go-junit-report_test.go b/go-junit-report_test.go index 1d5e349..5bcf2b4 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -156,10 +156,62 @@ var testCases []TestCase = []TestCase{ }, noXmlHeader: true, }, + { + name: "06-mixed.txt", + reportName: "06-report.xml", + report: &Report{ + Packages: []Package{ + { + Name: "package/name1", + Time: 160, + Tests: []Test{ + { + Name: "TestOne", + Time: 60, + Result: PASS, + Output: []string{}, + }, + { + Name: "TestTwo", + Time: 100, + Result: PASS, + Output: []string{}, + }, + }, + }, + { + Name: "package/name2", + Time: 151, + Tests: []Test{ + { + Name: "TestOne", + Time: 20, + Result: FAIL, + Output: []string{ + "file_test.go:11: Error message", + "file_test.go:11: Longer", + "\terror", + "\tmessage.", + }, + }, + { + Name: "TestTwo", + Time: 130, + Result: PASS, + Output: []string{}, + }, + }, + }, + }, + }, + noXmlHeader: true, + }, } func TestParser(t *testing.T) { for _, testCase := range testCases { + t.Logf("Running: %s", testCase.name) + file, err := os.Open("tests/" + testCase.name) if err != nil { t.Fatal(err) diff --git a/tests/06-mixed.txt b/tests/06-mixed.txt new file mode 100644 index 0000000..a2a744d --- /dev/null +++ b/tests/06-mixed.txt @@ -0,0 +1,17 @@ +=== RUN TestOne +--- PASS: TestOne (0.06 seconds) +=== RUN TestTwo +--- PASS: TestTwo (0.10 seconds) +PASS +ok package/name1 0.160s +=== RUN TestOne +--- FAIL: TestOne (0.02 seconds) + file_test.go:11: Error message + file_test.go:11: Longer + error + message. +=== RUN TestTwo +--- PASS: TestTwo (0.13 seconds) +FAIL +exit status 1 +FAIL package/name2 0.151s diff --git a/tests/06-report.xml b/tests/06-report.xml new file mode 100644 index 0000000..bb872f2 --- /dev/null +++ b/tests/06-report.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + file_test.go:11: Error message file_test.go:11: Longer error message. + + + + From 2110a4a63bd18ae2ba000e1c55c170aeaea562f8 Mon Sep 17 00:00:00 2001 From: Nick Palmer Date: Tue, 13 Jan 2015 19:24:08 -0800 Subject: [PATCH 3/3] Replace all go version attributes. --- go-junit-report_test.go | 2 +- tests/06-report.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go-junit-report_test.go b/go-junit-report_test.go index 5bcf2b4..42201de 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -297,7 +297,7 @@ func loadTestReport(name string) (string, error) { } // replace value="1.0" With actual version - report := strings.Replace(string(contents), `value="1.0"`, fmt.Sprintf(`value="%s"`, runtime.Version()), 1) + report := strings.Replace(string(contents), `value="1.0"`, fmt.Sprintf(`value="%s"`, runtime.Version()), -1) return report, nil } diff --git a/tests/06-report.xml b/tests/06-report.xml index bb872f2..8b2ab23 100644 --- a/tests/06-report.xml +++ b/tests/06-report.xml @@ -1,14 +1,14 @@ - + - + file_test.go:11: Error message file_test.go:11: Longer error message.