mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-08 06:28:08 -05:00
Parse test failure messages
This commit is contained in:
parent
bcc3b7b2d7
commit
a6dab641a1
@ -30,13 +30,13 @@ var testCases []TestCase = []TestCase{
|
|||||||
Name: "TestOne",
|
Name: "TestOne",
|
||||||
Time: 60,
|
Time: 60,
|
||||||
Result: PASS,
|
Result: PASS,
|
||||||
Output: "",
|
Output: []string{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "TestTwo",
|
Name: "TestTwo",
|
||||||
Time: 100,
|
Time: 100,
|
||||||
Result: PASS,
|
Result: PASS,
|
||||||
Output: "",
|
Output: []string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -56,13 +56,18 @@ var testCases []TestCase = []TestCase{
|
|||||||
Name: "TestOne",
|
Name: "TestOne",
|
||||||
Time: 20,
|
Time: 20,
|
||||||
Result: FAIL,
|
Result: FAIL,
|
||||||
Output: "",
|
Output: []string{
|
||||||
|
"file_test.go:11: Error message",
|
||||||
|
"file_test.go:11: Longer",
|
||||||
|
"error",
|
||||||
|
"message.",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "TestTwo",
|
Name: "TestTwo",
|
||||||
Time: 130,
|
Time: 130,
|
||||||
Result: PASS,
|
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)
|
t.Errorf("Test.Result == %d, want %d", test.Result, expTest.Result)
|
||||||
}
|
}
|
||||||
|
|
||||||
if test.Output != expTest.Output {
|
testOutput := strings.Join(test.Output, "\n")
|
||||||
t.Errorf("Test.Output == %s, want %s", test.Output, expTest.Output)
|
expTestOutput := strings.Join(expTest.Output, "\n")
|
||||||
|
if testOutput != expTestOutput {
|
||||||
|
t.Errorf("Test.Output ==\n%s, want\n%s", testOutput, expTestOutput)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,11 +20,11 @@ type JUnitTestSuite struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type JUnitTestCase struct {
|
type JUnitTestCase struct {
|
||||||
XMLName xml.Name `xml:"testcase"`
|
XMLName xml.Name `xml:"testcase"`
|
||||||
Classname string `xml:"classname,attr"`
|
Classname string `xml:"classname,attr"`
|
||||||
Name string `xml:"name,attr"`
|
Name string `xml:"name,attr"`
|
||||||
Time string `xml:"time,attr"`
|
Time string `xml:"time,attr"`
|
||||||
Failure string `xml:"failure,omitempty"`
|
Failure *JUnitFailure `xml:"failure,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type JUnitProperty struct {
|
type JUnitProperty struct {
|
||||||
@ -32,6 +32,12 @@ type JUnitProperty struct {
|
|||||||
Value string `xml:"value,attr"`
|
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 {
|
func NewJUnitProperty(name, value string) JUnitProperty {
|
||||||
return JUnitProperty{
|
return JUnitProperty{
|
||||||
Name: name,
|
Name: name,
|
||||||
@ -69,14 +75,17 @@ func JUnitReportXML(report *Report, w io.Writer) error {
|
|||||||
Classname: classname,
|
Classname: classname,
|
||||||
Name: test.Name,
|
Name: test.Name,
|
||||||
Time: formatTime(test.Time),
|
Time: formatTime(test.Time),
|
||||||
Failure: "",
|
Failure: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
if test.Result == FAIL {
|
if test.Result == FAIL {
|
||||||
ts.Failures += 1
|
ts.Failures += 1
|
||||||
|
|
||||||
// TODO: set error message
|
testCase.Failure = &JUnitFailure{
|
||||||
testCase.Failure = "Failed"
|
Message: "Failed",
|
||||||
|
Type: "",
|
||||||
|
Contents: strings.Join(test.Output, "\n"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ts.TestCases = append(ts.TestCases, testCase)
|
ts.TestCases = append(ts.TestCases, testCase)
|
||||||
|
49
parser.go
49
parser.go
@ -29,7 +29,7 @@ type Test struct {
|
|||||||
Name string
|
Name string
|
||||||
Time int
|
Time int
|
||||||
Result Result
|
Result Result
|
||||||
Output string
|
Output []string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -58,24 +58,35 @@ func Parse(r io.Reader) (*Report, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
line := string(l)
|
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 {
|
if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 {
|
||||||
|
// test status
|
||||||
if matches[1] == "PASS" {
|
if matches[1] == "PASS" {
|
||||||
test.Result = PASS
|
test.Result = PASS
|
||||||
} else {
|
} else {
|
||||||
@ -84,9 +95,9 @@ func Parse(r io.Reader) (*Report, error) {
|
|||||||
|
|
||||||
test.Name = matches[2]
|
test.Name = matches[2]
|
||||||
test.Time = parseTime(matches[3]) * 10
|
test.Time = parseTime(matches[3]) * 10
|
||||||
|
} else if strings.HasPrefix(line, "\t") {
|
||||||
tests = append(tests, *test)
|
// test output
|
||||||
test = nil
|
test.Output = append(test.Output, strings.TrimLeft(line, "\t"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,10 @@
|
|||||||
<property name="go.version" value="1.0"></property>
|
<property name="go.version" value="1.0"></property>
|
||||||
</properties>
|
</properties>
|
||||||
<testcase classname="name" name="TestOne" time="0.020">
|
<testcase classname="name" name="TestOne" time="0.020">
|
||||||
<failure>Failed</failure>
|
<failure message="Failed" type="">file_test.go:11: Error message
|
||||||
|
file_test.go:11: Longer
|
||||||
|
error
|
||||||
|
message.</failure>
|
||||||
</testcase>
|
</testcase>
|
||||||
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
|
<testcase classname="name" name="TestTwo" time="0.130"></testcase>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user