Deprecated Time field, use a time.Duration instead.

The parser.Package.Time and parser.Test.Time fields are currently still
supported, but will be removed in the future.
This commit is contained in:
Joël Stemmer 2018-04-21 17:09:34 +01:00
parent c1eb342963
commit 1ce4b93a20
4 changed files with 441 additions and 305 deletions

View File

@ -7,6 +7,7 @@ import (
"io" "io"
"runtime" "runtime"
"strings" "strings"
"time"
"github.com/jstemmer/go-junit-report/parser" "github.com/jstemmer/go-junit-report/parser"
) )
@ -67,7 +68,7 @@ func JUnitReportXML(report *parser.Report, noXMLHeader bool, goVersion string, w
ts := JUnitTestSuite{ ts := JUnitTestSuite{
Tests: len(pkg.Tests), Tests: len(pkg.Tests),
Failures: 0, Failures: 0,
Time: formatTime(pkg.Time), Time: formatTime(pkg.Duration),
Name: pkg.Name, Name: pkg.Name,
Properties: []JUnitProperty{}, Properties: []JUnitProperty{},
TestCases: []JUnitTestCase{}, TestCases: []JUnitTestCase{},
@ -93,7 +94,7 @@ func JUnitReportXML(report *parser.Report, noXMLHeader bool, goVersion string, w
testCase := JUnitTestCase{ testCase := JUnitTestCase{
Classname: classname, Classname: classname,
Name: test.Name, Name: test.Name,
Time: formatTime(test.Time), Time: formatTime(test.Duration),
Failure: nil, Failure: nil,
} }
@ -135,6 +136,6 @@ func JUnitReportXML(report *parser.Report, noXMLHeader bool, goVersion string, w
return nil return nil
} }
func formatTime(time int) string { func formatTime(d time.Duration) string {
return fmt.Sprintf("%.3f", float64(time)/1000.0) return fmt.Sprintf("%.3f", d.Seconds())
} }

View File

@ -10,6 +10,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
"time"
"github.com/jstemmer/go-junit-report/formatter" "github.com/jstemmer/go-junit-report/formatter"
"github.com/jstemmer/go-junit-report/parser" "github.com/jstemmer/go-junit-report/parser"
@ -33,16 +34,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Duration: 160 * time.Millisecond,
Time: 160, Time: 160,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestZ", Name: "TestZ",
Duration: 60 * time.Millisecond,
Time: 60, Time: 60,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestA", Name: "TestA",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -59,10 +63,12 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Duration: 151 * time.Millisecond,
Time: 151, Time: 151,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 20 * time.Millisecond,
Time: 20, Time: 20,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -74,6 +80,7 @@ var testCases = []TestCase{
}, },
{ {
Name: "TestTwo", Name: "TestTwo",
Duration: 130 * time.Millisecond,
Time: 130, Time: 130,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -90,10 +97,12 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Duration: 150 * time.Millisecond,
Time: 150, Time: 150,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 20 * time.Millisecond,
Time: 20, Time: 20,
Result: parser.SKIP, Result: parser.SKIP,
Output: []string{ Output: []string{
@ -102,6 +111,7 @@ var testCases = []TestCase{
}, },
{ {
Name: "TestTwo", Name: "TestTwo",
Duration: 130 * time.Millisecond,
Time: 130, Time: 130,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -118,16 +128,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Duration: 160 * time.Millisecond,
Time: 160, Time: 160,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 60 * time.Millisecond,
Time: 60, Time: 60,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestTwo", Name: "TestTwo",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -144,16 +157,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Duration: 160 * time.Millisecond,
Time: 160, Time: 160,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 60 * time.Millisecond,
Time: 60, Time: 60,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestTwo", Name: "TestTwo",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -171,16 +187,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name1", Name: "package/name1",
Duration: 160 * time.Millisecond,
Time: 160, Time: 160,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 60 * time.Millisecond,
Time: 60, Time: 60,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestTwo", Name: "TestTwo",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -189,10 +208,12 @@ var testCases = []TestCase{
}, },
{ {
Name: "package/name2", Name: "package/name2",
Duration: 151 * time.Millisecond,
Time: 151, Time: 151,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 20 * time.Millisecond,
Time: 20, Time: 20,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -204,6 +225,7 @@ var testCases = []TestCase{
}, },
{ {
Name: "TestTwo", Name: "TestTwo",
Duration: 130 * time.Millisecond,
Time: 130, Time: 130,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -221,16 +243,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "test/package", Name: "test/package",
Duration: 160 * time.Millisecond,
Time: 160, Time: 160,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 60 * time.Millisecond,
Time: 60, Time: 60,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestTwo", Name: "TestTwo",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -248,16 +273,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "github.com/dmitris/test-go-junit-report", Name: "github.com/dmitris/test-go-junit-report",
Duration: 440 * time.Millisecond,
Time: 440, Time: 440,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestDoFoo", Name: "TestDoFoo",
Duration: 270 * time.Millisecond,
Time: 270, Time: 270,
Result: parser.PASS, Result: parser.PASS,
Output: []string{"cov_test.go:10: DoFoo log 1", "cov_test.go:10: DoFoo log 2"}, Output: []string{"cov_test.go:10: DoFoo log 1", "cov_test.go:10: DoFoo log 2"},
}, },
{ {
Name: "TestDoFoo2", Name: "TestDoFoo2",
Duration: 160 * time.Millisecond,
Time: 160, Time: 160,
Result: parser.PASS, Result: parser.PASS,
Output: []string{"cov_test.go:21: DoFoo2 log 1", "cov_test.go:21: DoFoo2 log 2"}, Output: []string{"cov_test.go:21: DoFoo2 log 1", "cov_test.go:21: DoFoo2 log 2"},
@ -274,16 +302,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Duration: 160 * time.Millisecond,
Time: 160, Time: 160,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestZ", Name: "TestZ",
Duration: 60 * time.Millisecond,
Time: 60, Time: 60,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestA", Name: "TestA",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -301,16 +332,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package1/foo", Name: "package1/foo",
Duration: 400 * time.Millisecond,
Time: 400, Time: 400,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestA", Name: "TestA",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestB", Name: "TestB",
Duration: 300 * time.Millisecond,
Time: 300, Time: 300,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -320,10 +354,12 @@ var testCases = []TestCase{
}, },
{ {
Name: "package2/bar", Name: "package2/bar",
Duration: 4200 * time.Millisecond,
Time: 4200, Time: 4200,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestC", Name: "TestC",
Duration: 4200 * time.Millisecond,
Time: 4200, Time: 4200,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -341,16 +377,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Duration: 50 * time.Millisecond,
Time: 50, Time: 50,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 20 * time.Millisecond,
Time: 20, Time: 20,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestTwo", Name: "TestTwo",
Duration: 30 * time.Millisecond,
Time: 30, Time: 30,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -367,88 +406,103 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Duration: 50 * time.Millisecond,
Time: 50, Time: 50,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 10 * time.Millisecond,
Time: 10, Time: 10,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestOne/Child", Name: "TestOne/Child",
Duration: 20 * time.Millisecond,
Time: 20, Time: 20,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestOne/Child#01", Name: "TestOne/Child#01",
Duration: 30 * time.Millisecond,
Time: 30, Time: 30,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestOne/Child=02", Name: "TestOne/Child=02",
Duration: 40 * time.Millisecond,
Time: 40, Time: 40,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestTwo", Name: "TestTwo",
Duration: 10 * time.Millisecond,
Time: 10, Time: 10,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestTwo/Child", Name: "TestTwo/Child",
Duration: 20 * time.Millisecond,
Time: 20, Time: 20,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestTwo/Child#01", Name: "TestTwo/Child#01",
Duration: 30 * time.Millisecond,
Time: 30, Time: 30,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestTwo/Child=02", Name: "TestTwo/Child=02",
Duration: 40 * time.Millisecond,
Time: 40, Time: 40,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestThree", Name: "TestThree",
Duration: 10 * time.Millisecond,
Time: 10, Time: 10,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestThree/a#1", Name: "TestThree/a#1",
Duration: 20 * time.Millisecond,
Time: 20, Time: 20,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestThree/a#1/b#1", Name: "TestThree/a#1/b#1",
Duration: 30 * time.Millisecond,
Time: 30, Time: 30,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestThree/a#1/b#1/c#1", Name: "TestThree/a#1/b#1/c#1",
Duration: 40 * time.Millisecond,
Time: 40, Time: 40,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestFour", Name: "TestFour",
Duration: 20 * time.Millisecond,
Time: 20, Time: 20,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestFour/#00", Name: "TestFour/#00",
Duration: 0,
Time: 0, Time: 0,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -459,6 +513,7 @@ var testCases = []TestCase{
}, },
{ {
Name: "TestFour/#01", Name: "TestFour/#01",
Duration: 0,
Time: 0, Time: 0,
Result: parser.SKIP, Result: parser.SKIP,
Output: []string{ Output: []string{
@ -467,12 +522,14 @@ var testCases = []TestCase{
}, },
{ {
Name: "TestFour/#02", Name: "TestFour/#02",
Duration: 0,
Time: 0, Time: 0,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestFive", Name: "TestFive",
Duration: 0,
Time: 0, Time: 0,
Result: parser.SKIP, Result: parser.SKIP,
Output: []string{ Output: []string{
@ -481,6 +538,7 @@ var testCases = []TestCase{
}, },
{ {
Name: "TestSix", Name: "TestSix",
Duration: 0,
Time: 0, Time: 0,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -499,10 +557,12 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name/passing1", Name: "package/name/passing1",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestA", Name: "TestA",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -511,10 +571,12 @@ var testCases = []TestCase{
}, },
{ {
Name: "package/name/passing2", Name: "package/name/passing2",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestB", Name: "TestB",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -526,6 +588,7 @@ var testCases = []TestCase{
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "[build failed]", Name: "[build failed]",
Duration: 0,
Time: 0, Time: 0,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -539,6 +602,7 @@ var testCases = []TestCase{
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "[build failed]", Name: "[build failed]",
Duration: 0,
Time: 0, Time: 0,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -552,6 +616,7 @@ var testCases = []TestCase{
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "[setup failed]", Name: "[setup failed]",
Duration: 0,
Time: 0, Time: 0,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -573,6 +638,7 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/panic", Name: "package/panic",
Duration: 3 * time.Millisecond,
Time: 3, Time: 3,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
@ -587,6 +653,7 @@ var testCases = []TestCase{
}, },
{ {
Name: "package/panic2", Name: "package/panic2",
Duration: 3 * time.Millisecond,
Time: 3, Time: 3,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
@ -609,6 +676,7 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/empty", Name: "package/empty",
Duration: 1 * time.Millisecond,
Time: 1, Time: 1,
Tests: []*parser.Test{}, Tests: []*parser.Test{},
}, },
@ -622,22 +690,26 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/repeated-names", Name: "package/repeated-names",
Duration: 1 * time.Millisecond,
Time: 1, Time: 1,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestRepeat", Name: "TestRepeat",
Duration: 0,
Time: 0, Time: 0,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestRepeat", Name: "TestRepeat",
Duration: 0,
Time: 0, Time: 0,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestRepeat", Name: "TestRepeat",
Duration: 0,
Time: 0, Time: 0,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -654,10 +726,12 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "race_test", Name: "race_test",
Duration: 15 * time.Millisecond,
Time: 15, Time: 15,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestRace", Name: "TestRace",
Duration: 0,
Time: 0, Time: 0,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -710,16 +784,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package1/foo", Name: "package1/foo",
Duration: 400 * time.Millisecond,
Time: 400, Time: 400,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestA", Name: "TestA",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestB", Name: "TestB",
Duration: 300 * time.Millisecond,
Time: 300, Time: 300,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -729,10 +806,12 @@ var testCases = []TestCase{
}, },
{ {
Name: "package2/bar", Name: "package2/bar",
Duration: 4200 * time.Millisecond,
Time: 4200, Time: 4200,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestC", Name: "TestC",
Duration: 4200 * time.Millisecond,
Time: 4200, Time: 4200,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -750,16 +829,19 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Duration: 160 * time.Millisecond,
Time: 160, Time: 160,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestZ", Name: "TestZ",
Duration: 60 * time.Millisecond,
Time: 60, Time: 60,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
}, },
{ {
Name: "TestA", Name: "TestA",
Duration: 100 * time.Millisecond,
Time: 100, Time: 100,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -776,10 +858,12 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "pkg/parallel", Name: "pkg/parallel",
Duration: 3010 * time.Millisecond,
Time: 3010, Time: 3010,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "FirstTest", Name: "FirstTest",
Duration: 2 * time.Second,
Time: 2000, Time: 2000,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -790,6 +874,7 @@ var testCases = []TestCase{
}, },
{ {
Name: "SecondTest", Name: "SecondTest",
Duration: 1 * time.Second,
Time: 1000, Time: 1000,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -799,6 +884,7 @@ var testCases = []TestCase{
}, },
{ {
Name: "ThirdTest", Name: "ThirdTest",
Duration: 10 * time.Millisecond,
Time: 10, Time: 10,
Result: parser.FAIL, Result: parser.FAIL,
Output: []string{ Output: []string{
@ -818,10 +904,12 @@ var testCases = []TestCase{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/one", Name: "package/one",
Duration: 0,
Time: 0, Time: 0,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
Duration: 0,
Time: 0, Time: 0,
Result: parser.PASS, Result: parser.PASS,
Output: []string{}, Output: []string{},
@ -867,6 +955,11 @@ func TestParser(t *testing.T) {
t.Errorf("Package.Name == %s, want %s", pkg.Name, expPkg.Name) t.Errorf("Package.Name == %s, want %s", pkg.Name, expPkg.Name)
} }
if pkg.Duration != expPkg.Duration {
t.Errorf("Package.Duration == %s, want %s", pkg.Duration, expPkg.Duration)
}
// pkg.Time is deprecated
if pkg.Time != expPkg.Time { if pkg.Time != expPkg.Time {
t.Errorf("Package.Time == %d, want %d", pkg.Time, expPkg.Time) t.Errorf("Package.Time == %d, want %d", pkg.Time, expPkg.Time)
} }
@ -882,6 +975,11 @@ func TestParser(t *testing.T) {
t.Errorf("Test.Name == %s, want %s", test.Name, expTest.Name) t.Errorf("Test.Name == %s, want %s", test.Name, expTest.Name)
} }
if test.Duration != expTest.Duration {
t.Errorf("Test.Duration == %s, want %s", test.Duration, expTest.Duration)
}
// test.Time is deprecated
if test.Time != expTest.Time { if test.Time != expTest.Time {
t.Errorf("Test.Time == %d, want %d", test.Time, expTest.Time) t.Errorf("Test.Time == %d, want %d", test.Time, expTest.Time)
} }
@ -930,7 +1028,7 @@ func testJUnitFormatter(t *testing.T, goVersion string) {
} }
if string(junitReport.Bytes()) != report { if string(junitReport.Bytes()) != report {
t.Fatalf("Fail: %s Report xml ==\n%s, want\n%s", testCase.name, string(junitReport.Bytes()), report) t.Errorf("Fail: %s Report xml ==\n%s, want\n%s", testCase.name, string(junitReport.Bytes()), report)
} }
} }
} }

View File

@ -4,8 +4,8 @@ import (
"bufio" "bufio"
"io" "io"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time"
) )
// Result represents a test result. // Result represents a test result.
@ -26,17 +26,23 @@ type Report struct {
// Package contains the test results of a single package. // Package contains the test results of a single package.
type Package struct { type Package struct {
Name string Name string
Time int Duration time.Duration
Tests []*Test Tests []*Test
CoveragePct string CoveragePct string
// Time is deprecated, use Duration instead.
Time int // in milliseconds
} }
// Test contains the results of a single test. // Test contains the results of a single test.
type Test struct { type Test struct {
Name string Name string
Time int Duration time.Duration
Result Result Result Result
Output []string Output []string
// Time is deprecated, use Duration instead.
Time int // in milliseconds
} }
var ( var (
@ -59,7 +65,7 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
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 var testsTime time.Duration
// current test // current test
var cur string var cur string
@ -133,9 +139,11 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
// all tests in this package are finished // all tests in this package are finished
report.Packages = append(report.Packages, Package{ report.Packages = append(report.Packages, Package{
Name: matches[2], Name: matches[2],
Time: parseTime(matches[3]), Duration: parseSeconds(matches[3]),
Tests: tests, Tests: tests,
CoveragePct: coveragePct, CoveragePct: coveragePct,
Time: int(parseSeconds(matches[3]) / time.Millisecond), // deprecated
}) })
buffers[cur] = buffers[cur][0:0] buffers[cur] = buffers[cur][0:0]
@ -161,9 +169,10 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
test.Output = buffers[cur] test.Output = buffers[cur]
test.Name = matches[2] test.Name = matches[2]
testTime := parseTime(matches[3]) * 10 test.Duration = parseSeconds(matches[3])
test.Time = testTime testsTime += test.Duration
testsTime += testTime
test.Time = int(test.Duration / time.Millisecond) // deprecated
} else if matches := regexCoverage.FindStringSubmatch(line); len(matches) == 2 { } else if matches := regexCoverage.FindStringSubmatch(line); len(matches) == 2 {
coveragePct = matches[1] coveragePct = matches[1]
} else if matches := regexOutput.FindStringSubmatch(line); capturedPackage == "" && len(matches) == 3 { } else if matches := regexOutput.FindStringSubmatch(line); capturedPackage == "" && len(matches) == 3 {
@ -194,7 +203,8 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
// no result line found // no result line found
report.Packages = append(report.Packages, Package{ report.Packages = append(report.Packages, Package{
Name: pkgName, Name: pkgName,
Time: testsTime, Duration: testsTime,
Time: int(testsTime / time.Millisecond),
Tests: tests, Tests: tests,
CoveragePct: coveragePct, CoveragePct: coveragePct,
}) })
@ -203,12 +213,13 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
return report, nil return report, nil
} }
func parseTime(time string) int { func parseSeconds(t string) time.Duration {
t, err := strconv.Atoi(strings.Replace(time, ".", "", -1)) if t == "" {
if err != nil { return time.Duration(0)
return 0
} }
return t // ignore error
d, _ := time.ParseDuration(t + "s")
return d
} }
func findTest(tests []*Test, name string) *Test { func findTest(tests []*Test, name string) *Test {

26
parser/parser_test.go Normal file
View File

@ -0,0 +1,26 @@
package parser
import (
"testing"
"time"
)
func TestParseSeconds(t *testing.T) {
tests := []struct {
in string
d time.Duration
}{
{"", 0},
{"4", 4 * time.Second},
{"0.1", 100 * time.Millisecond},
{"0.050", 50 * time.Millisecond},
{"2.003", 2*time.Second + 3*time.Millisecond},
}
for _, test := range tests {
d := parseSeconds(test.in)
if d != test.d {
t.Errorf("parseSeconds(%q) == %v, want %v\n", test.in, d, test.d)
}
}
}