From 6efcf4050909caae7cf77c689c17cdded2bfb705 Mon Sep 17 00:00:00 2001 From: ekharchenko-avito Date: Thu, 2 Jul 2020 08:55:22 +0300 Subject: [PATCH] Support for nested test suites (#37) Co-authored-by: William Petit --- ingest.go | 8 +++++-- ingest_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ testdata/phpunit.xml | 39 +++++++++++++++++++++++++++++++++ types.go | 16 +++++++++++++- 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 testdata/phpunit.xml diff --git a/ingest.go b/ingest.go index 0308cc9..ee12ea6 100644 --- a/ingest.go +++ b/ingest.go @@ -24,12 +24,16 @@ func findSuites(nodes []xmlNode, suites chan Suite) { func ingestSuite(root xmlNode) Suite { suite := Suite{ - Name: root.Attr("name"), - Package: root.Attr("package"), + Name: root.Attr("name"), + Package: root.Attr("package"), + Properties: root.Attrs, } for _, node := range root.Nodes { switch node.XMLName.Local { + case "testsuite": + testsuite := ingestSuite(node) + suite.Suites = append(suite.Suites, testsuite) case "testcase": testcase := ingestTestcase(node) suite.Tests = append(suite.Tests, testcase) diff --git a/ingest_test.go b/ingest_test.go index 3eafa68..5270d4d 100644 --- a/ingest_test.go +++ b/ingest_test.go @@ -159,6 +159,57 @@ func TestExamplesInTheWild(t *testing.T) { assert.EqualError(t, suites[0].Tests[3].Error, "NullPointerException") }, }, + { + title: "phpunit example", + filename: "testdata/phpunit.xml", + check: func(t *testing.T, suites []Suite) { + assert.Len(t, suites, 1) + assert.Len(t, suites[0].Tests, 0) + assert.Len(t, suites[0].Suites, 1) + + suite := suites[0].Suites[0] + assert.Len(t, suite.Tests, 1) + assert.Len(t, suite.Suites, 2) + + assert.Equal(t, "SampleTest", suite.Name) + assert.Equal(t, "/untitled/tests/SampleTest.php", suite.Properties["file"]) + + var testcase = Test{ + Name: "testA", + Classname: "SampleTest", + Duration: 5917 * time.Microsecond, + Status: StatusPassed, + Properties: map[string]string{ + "assertions": "1", + "class": "SampleTest", + "classname": "SampleTest", + "file": "/untitled/tests/SampleTest.php", + "line": "7", + "name": "testA", + "time": "0.005917", + }, + } + + assert.Equal(t, testcase, suite.Tests[0]) + + assert.Len(t, suite.Suites[1].Suites, 0) + assert.Len(t, suite.Suites[1].Tests, 3) + assert.Equal(t, "testC with data set #0", suite.Suites[1].Tests[0].Name) + + // checking recursive aggregation + suites[0].Aggregate() + actualTotals := suites[0].Totals + expectedTotals := Totals{ + Tests: 7, + Passed: 4, + Skipped: 0, + Failed: 3, + Error: 0, + Duration: 8489 * time.Microsecond, + } + assert.Equal(t, expectedTotals, actualTotals) + }, + }, } for index, test := range tests { diff --git a/testdata/phpunit.xml b/testdata/phpunit.xml new file mode 100644 index 0000000..5d84dc5 --- /dev/null +++ b/testdata/phpunit.xml @@ -0,0 +1,39 @@ + + + + + + + + SampleTest::testB with data set "bool" (false) + should be true + Failed asserting that false matches expected true. + + /untitled/tests/SampleTest.php:18 + + + + + + + + + SampleTest::testC with data set #1 (0) + should be true + Failed asserting that 0 matches expected true. + + /untitled/tests/SampleTest.php:34 + + + + SampleTest::testC with data set #2 ('') + should be true + Failed asserting that '' matches expected true. + + /untitled/tests/SampleTest.php:34 + + + + + + diff --git a/types.go b/types.go index a7d3c60..d01c6ba 100644 --- a/types.go +++ b/types.go @@ -74,6 +74,9 @@ type Suite struct { // Tests is an ordered collection of tests with associated results. Tests []Test `json:"tests,omitempty" yaml:"tests,omitempty"` + // Suites is an ordered collection of suites with associated tests. + Suites []Suite `json:"suites,omitempty" yaml:"suites,omitempty"` + // SystemOut is textual test output for the suite. Usually output that is // written to stdout. SystemOut string `json:"stdout,omitempty" yaml:"stdout,omitempty"` @@ -86,7 +89,7 @@ type Suite struct { Totals Totals `json:"totals" yaml:"totals"` } -// Aggregate calculates result sums across all tests. +// Aggregate calculates result sums across all tests and nested suites. func (s *Suite) Aggregate() { totals := Totals{Tests: len(s.Tests)} @@ -104,6 +107,17 @@ func (s *Suite) Aggregate() { } } + // just summing totals from nested suites + for _, suite := range s.Suites { + suite.Aggregate() + totals.Tests += suite.Totals.Tests + totals.Duration += suite.Totals.Duration + totals.Passed += suite.Totals.Passed + totals.Skipped += suite.Totals.Skipped + totals.Failed += suite.Totals.Failed + totals.Error += suite.Totals.Error + } + s.Totals = totals }