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",
Time: 160,
Tests: []Test{
Tests: []*Test{
{
Name: "TestZ",
Time: 60,
@ -53,7 +53,7 @@ var testCases = []TestCase{
{
Name: "package/name",
Time: 151,
Tests: []Test{
Tests: []*Test{
{
Name: "TestOne",
Time: 20,
@ -84,7 +84,7 @@ var testCases = []TestCase{
{
Name: "package/name",
Time: 150,
Tests: []Test{
Tests: []*Test{
{
Name: "TestOne",
Time: 20,
@ -112,7 +112,7 @@ var testCases = []TestCase{
{
Name: "package/name",
Time: 160,
Tests: []Test{
Tests: []*Test{
{
Name: "TestOne",
Time: 60,
@ -138,7 +138,7 @@ var testCases = []TestCase{
{
Name: "package/name",
Time: 160,
Tests: []Test{
Tests: []*Test{
{
Name: "TestOne",
Time: 60,
@ -165,7 +165,7 @@ var testCases = []TestCase{
{
Name: "package/name1",
Time: 160,
Tests: []Test{
Tests: []*Test{
{
Name: "TestOne",
Time: 60,
@ -183,7 +183,7 @@ var testCases = []TestCase{
{
Name: "package/name2",
Time: 151,
Tests: []Test{
Tests: []*Test{
{
Name: "TestOne",
Time: 20,
@ -215,7 +215,7 @@ var testCases = []TestCase{
{
Name: "test/package",
Time: 160,
Tests: []Test{
Tests: []*Test{
{
Name: "TestOne",
Time: 60,
@ -234,6 +234,32 @@ var testCases = []TestCase{
},
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) {

View File

@ -27,7 +27,7 @@ type Report struct {
type Package struct {
Name string
Time int
Tests []Test
Tests []*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)}
// 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)
testsTime := 0
// current test
var test *Test
var cur string
// parse lines
for {
@ -72,58 +72,56 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
line := string(l)
if strings.HasPrefix(line, "=== RUN ") {
// start of a new test
if test != nil {
tests = append(tests, *test)
}
test = &Test{
// new test
cur = line[8:]
tests = append(tests, &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)
tests = make([]*Test, 0)
cur = ""
testsTime = 0
} else if test != nil {
if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 {
// 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.Output = append(test.Output, line[1:])
} else if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 {
cur = matches[2]
test := findTest(tests, cur)
if test == nil {
continue
}
// 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 {
tests = append(tests, *test)
}
if len(tests) > 0 { // no result line found
if len(tests) > 0 {
// no result line found
report.Packages = append(report.Packages, Package{
Name: pkgName,
Time: testsTime,
@ -141,3 +139,12 @@ func parseTime(time string) int {
}
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>