Correctly parse test output of parallel tests

Fixes #10
This commit is contained in:
Joël Stemmer 2015-02-06 13:44:06 +01:00
parent e705d170a3
commit 672f4cd7e2
4 changed files with 100 additions and 47 deletions

View File

@ -27,7 +27,7 @@ var testCases = []TestCase{
{ {
Name: "package/name", Name: "package/name",
Time: 160, Time: 160,
Tests: []Test{ Tests: []*Test{
{ {
Name: "TestZ", Name: "TestZ",
Time: 60, Time: 60,
@ -53,7 +53,7 @@ var testCases = []TestCase{
{ {
Name: "package/name", Name: "package/name",
Time: 151, Time: 151,
Tests: []Test{ Tests: []*Test{
{ {
Name: "TestOne", Name: "TestOne",
Time: 20, Time: 20,
@ -84,7 +84,7 @@ var testCases = []TestCase{
{ {
Name: "package/name", Name: "package/name",
Time: 150, Time: 150,
Tests: []Test{ Tests: []*Test{
{ {
Name: "TestOne", Name: "TestOne",
Time: 20, Time: 20,
@ -112,7 +112,7 @@ var testCases = []TestCase{
{ {
Name: "package/name", Name: "package/name",
Time: 160, Time: 160,
Tests: []Test{ Tests: []*Test{
{ {
Name: "TestOne", Name: "TestOne",
Time: 60, Time: 60,
@ -138,7 +138,7 @@ var testCases = []TestCase{
{ {
Name: "package/name", Name: "package/name",
Time: 160, Time: 160,
Tests: []Test{ Tests: []*Test{
{ {
Name: "TestOne", Name: "TestOne",
Time: 60, Time: 60,
@ -165,7 +165,7 @@ var testCases = []TestCase{
{ {
Name: "package/name1", Name: "package/name1",
Time: 160, Time: 160,
Tests: []Test{ Tests: []*Test{
{ {
Name: "TestOne", Name: "TestOne",
Time: 60, Time: 60,
@ -183,7 +183,7 @@ var testCases = []TestCase{
{ {
Name: "package/name2", Name: "package/name2",
Time: 151, Time: 151,
Tests: []Test{ Tests: []*Test{
{ {
Name: "TestOne", Name: "TestOne",
Time: 20, Time: 20,
@ -215,7 +215,7 @@ var testCases = []TestCase{
{ {
Name: "test/package", Name: "test/package",
Time: 160, Time: 160,
Tests: []Test{ Tests: []*Test{
{ {
Name: "TestOne", Name: "TestOne",
Time: 60, Time: 60,
@ -234,6 +234,32 @@ var testCases = []TestCase{
}, },
packageName: "test/package", packageName: "test/package",
}, },
{
name: "08-parallel.txt",
reportName: "08-report.xml",
report: &Report{
Packages: []Package{
{
Name: "github.com/dmitris/test-go-junit-report",
Time: 440,
Tests: []*Test{
{
Name: "TestDoFoo",
Time: 270,
Result: PASS,
Output: []string{"cov_test.go:10: DoFoo log 1", "cov_test.go:10: DoFoo log 2"},
},
{
Name: "TestDoFoo2",
Time: 160,
Result: PASS,
Output: []string{"cov_test.go:21: DoFoo2 log 1", "cov_test.go:21: DoFoo2 log 2"},
},
},
},
},
},
},
} }
func TestParser(t *testing.T) { func TestParser(t *testing.T) {

View File

@ -27,7 +27,7 @@ type Report struct {
type Package struct { type Package struct {
Name string Name string
Time int Time int
Tests []Test Tests []*Test
} }
// Test contains the results of a single test. // Test contains the results of a single test.
@ -52,13 +52,13 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
report := &Report{make([]Package, 0)} report := &Report{make([]Package, 0)}
// keep track of tests we find // keep track of tests we find
var tests []Test var tests []*Test
// sum of tests' time, use this if current test has no result line (when it is compiled test) // sum of tests' time, use this if current test has no result line (when it is compiled test)
testsTime := 0 testsTime := 0
// current test // current test
var test *Test var cur string
// parse lines // parse lines
for { for {
@ -72,58 +72,56 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
line := string(l) line := string(l)
if strings.HasPrefix(line, "=== RUN ") { if strings.HasPrefix(line, "=== RUN ") {
// start of a new test // new test
if test != nil { cur = line[8:]
tests = append(tests, *test) tests = append(tests, &Test{
}
test = &Test{
Name: line[8:], Name: line[8:],
Result: FAIL, Result: FAIL,
Output: make([]string, 0), Output: make([]string, 0),
} })
} else if matches := regexResult.FindStringSubmatch(line); len(matches) == 4 { } else if matches := regexResult.FindStringSubmatch(line); len(matches) == 4 {
// all tests in this package are finished // all tests in this package are finished
if test != nil {
tests = append(tests, *test)
test = nil
}
report.Packages = append(report.Packages, Package{ report.Packages = append(report.Packages, Package{
Name: matches[2], Name: matches[2],
Time: parseTime(matches[3]), Time: parseTime(matches[3]),
Tests: tests, Tests: tests,
}) })
tests = make([]Test, 0) tests = make([]*Test, 0)
cur = ""
testsTime = 0 testsTime = 0
} else if test != nil { } else if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 {
if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 { cur = matches[2]
// test status test := findTest(tests, cur)
if matches[1] == "PASS" { if test == nil {
test.Result = PASS continue
} else if matches[1] == "SKIP" {
test.Result = SKIP
} else {
test.Result = FAIL
}
test.Name = matches[2]
testTime := parseTime(matches[3]) * 10
test.Time = testTime
testsTime += testTime
} else if strings.HasPrefix(line, "\t") {
// test output
test.Output = append(test.Output, line[1:])
} }
// test status
if matches[1] == "PASS" {
test.Result = PASS
} else if matches[1] == "SKIP" {
test.Result = SKIP
} else {
test.Result = FAIL
}
test.Name = matches[2]
testTime := parseTime(matches[3]) * 10
test.Time = testTime
testsTime += testTime
} else if strings.HasPrefix(line, "\t") {
// test output
test := findTest(tests, cur)
if test == nil {
continue
}
test.Output = append(test.Output, line[1:])
} }
} }
if test != nil { if len(tests) > 0 {
tests = append(tests, *test) // no result line found
}
if len(tests) > 0 { // no result line found
report.Packages = append(report.Packages, Package{ report.Packages = append(report.Packages, Package{
Name: pkgName, Name: pkgName,
Time: testsTime, Time: testsTime,
@ -141,3 +139,12 @@ func parseTime(time string) int {
} }
return t return t
} }
func findTest(tests []*Test, name string) *Test {
for i := 0; i < len(tests); i++ {
if tests[i].Name == name {
return tests[i]
}
}
return nil
}

10
tests/08-parallel.txt Normal file
View File

@ -0,0 +1,10 @@
=== RUN TestDoFoo
=== RUN TestDoFoo2
--- PASS: TestDoFoo (0.27s)
cov_test.go:10: DoFoo log 1
cov_test.go:10: DoFoo log 2
--- PASS: TestDoFoo2 (0.16s)
cov_test.go:21: DoFoo2 log 1
cov_test.go:21: DoFoo2 log 2
PASS
ok github.com/dmitris/test-go-junit-report 0.440s

10
tests/08-report.xml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite tests="2" failures="0" time="0.440" name="github.com/dmitris/test-go-junit-report">
<properties>
<property name="go.version" value="1.0"></property>
</properties>
<testcase classname="test-go-junit-report" name="TestDoFoo" time="0.270"></testcase>
<testcase classname="test-go-junit-report" name="TestDoFoo2" time="0.160"></testcase>
</testsuite>
</testsuites>