diff --git a/go-junit-report_test.go b/go-junit-report_test.go
index 75e43cb..d9b99c2 100644
--- a/go-junit-report_test.go
+++ b/go-junit-report_test.go
@@ -30,13 +30,13 @@ var testCases []TestCase = []TestCase{
Name: "TestOne",
Time: 60,
Result: PASS,
- Output: "",
+ Output: []string{},
},
{
Name: "TestTwo",
Time: 100,
Result: PASS,
- Output: "",
+ Output: []string{},
},
},
},
@@ -56,13 +56,18 @@ var testCases []TestCase = []TestCase{
Name: "TestOne",
Time: 20,
Result: FAIL,
- Output: "",
+ Output: []string{
+ "file_test.go:11: Error message",
+ "file_test.go:11: Longer",
+ "error",
+ "message.",
+ },
},
{
Name: "TestTwo",
Time: 130,
Result: PASS,
- Output: "",
+ Output: []string{},
},
},
},
@@ -122,8 +127,10 @@ func TestParser(t *testing.T) {
t.Errorf("Test.Result == %d, want %d", test.Result, expTest.Result)
}
- if test.Output != expTest.Output {
- t.Errorf("Test.Output == %s, want %s", test.Output, expTest.Output)
+ testOutput := strings.Join(test.Output, "\n")
+ expTestOutput := strings.Join(expTest.Output, "\n")
+ if testOutput != expTestOutput {
+ t.Errorf("Test.Output ==\n%s, want\n%s", testOutput, expTestOutput)
}
}
}
diff --git a/junit-formatter.go b/junit-formatter.go
index b47de29..f9ea0c1 100644
--- a/junit-formatter.go
+++ b/junit-formatter.go
@@ -20,11 +20,11 @@ type JUnitTestSuite struct {
}
type JUnitTestCase struct {
- XMLName xml.Name `xml:"testcase"`
- Classname string `xml:"classname,attr"`
- Name string `xml:"name,attr"`
- Time string `xml:"time,attr"`
- Failure string `xml:"failure,omitempty"`
+ XMLName xml.Name `xml:"testcase"`
+ Classname string `xml:"classname,attr"`
+ Name string `xml:"name,attr"`
+ Time string `xml:"time,attr"`
+ Failure *JUnitFailure `xml:"failure,omitempty"`
}
type JUnitProperty struct {
@@ -32,6 +32,12 @@ type JUnitProperty struct {
Value string `xml:"value,attr"`
}
+type JUnitFailure struct {
+ Message string `xml:"message,attr"`
+ Type string `xml:"type,attr"`
+ Contents string `xml:",chardata"`
+}
+
func NewJUnitProperty(name, value string) JUnitProperty {
return JUnitProperty{
Name: name,
@@ -69,14 +75,17 @@ func JUnitReportXML(report *Report, w io.Writer) error {
Classname: classname,
Name: test.Name,
Time: formatTime(test.Time),
- Failure: "",
+ Failure: nil,
}
if test.Result == FAIL {
ts.Failures += 1
- // TODO: set error message
- testCase.Failure = "Failed"
+ testCase.Failure = &JUnitFailure{
+ Message: "Failed",
+ Type: "",
+ Contents: strings.Join(test.Output, "\n"),
+ }
}
ts.TestCases = append(ts.TestCases, testCase)
diff --git a/parser.go b/parser.go
index 3c37ee0..a377f9c 100644
--- a/parser.go
+++ b/parser.go
@@ -29,7 +29,7 @@ type Test struct {
Name string
Time int
Result Result
- Output string
+ Output []string
}
var (
@@ -58,24 +58,35 @@ func Parse(r io.Reader) (*Report, error) {
}
line := string(l)
- if test == nil {
- // expecting new test or package result
- if strings.HasPrefix(line, "=== RUN ") {
- test = &Test{
- Name: line[8:],
- }
- } else if matches := regexResult.FindStringSubmatch(line); len(matches) == 4 {
- report.Packages = append(report.Packages, Package{
- Name: matches[2],
- Time: parseTime(matches[3]),
- Tests: tests,
- })
- tests = make([]Test, 0)
+ if strings.HasPrefix(line, "=== RUN ") {
+ // start of a new test
+ if test != nil {
+ tests = append(tests, *test)
}
- } else {
- // expecting test status
+
+ test = &Test{
+ Name: line[8:],
+ Result: FAIL,
+ Output: make([]string, 0),
+ }
+ } else if matches := regexResult.FindStringSubmatch(line); len(matches) == 4 {
+ // all tests in this package are finished
+ if test != nil {
+ tests = append(tests, *test)
+ test = nil
+ }
+
+ report.Packages = append(report.Packages, Package{
+ Name: matches[2],
+ Time: parseTime(matches[3]),
+ Tests: tests,
+ })
+
+ tests = make([]Test, 0)
+ } else if test != nil {
if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 {
+ // test status
if matches[1] == "PASS" {
test.Result = PASS
} else {
@@ -84,9 +95,9 @@ func Parse(r io.Reader) (*Report, error) {
test.Name = matches[2]
test.Time = parseTime(matches[3]) * 10
-
- tests = append(tests, *test)
- test = nil
+ } else if strings.HasPrefix(line, "\t") {
+ // test output
+ test.Output = append(test.Output, strings.TrimLeft(line, "\t"))
}
}
}
diff --git a/tests/02-report.xml b/tests/02-report.xml
index 48b40c5..3920ed0 100644
--- a/tests/02-report.xml
+++ b/tests/02-report.xml
@@ -4,7 +4,10 @@
- Failed
+ file_test.go:11: Error message
+file_test.go:11: Longer
+error
+message.