Skip to content

Commit 6efcf40

Browse files
ekharchenko-avitoWilliam Petit
andauthored
Support for nested test suites (#37)
Co-authored-by: William Petit <[email protected]>
1 parent e5d93c0 commit 6efcf40

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

ingest.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ func findSuites(nodes []xmlNode, suites chan Suite) {
2424

2525
func ingestSuite(root xmlNode) Suite {
2626
suite := Suite{
27-
Name: root.Attr("name"),
28-
Package: root.Attr("package"),
27+
Name: root.Attr("name"),
28+
Package: root.Attr("package"),
29+
Properties: root.Attrs,
2930
}
3031

3132
for _, node := range root.Nodes {
3233
switch node.XMLName.Local {
34+
case "testsuite":
35+
testsuite := ingestSuite(node)
36+
suite.Suites = append(suite.Suites, testsuite)
3337
case "testcase":
3438
testcase := ingestTestcase(node)
3539
suite.Tests = append(suite.Tests, testcase)

ingest_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,57 @@ func TestExamplesInTheWild(t *testing.T) {
159159
assert.EqualError(t, suites[0].Tests[3].Error, "NullPointerException")
160160
},
161161
},
162+
{
163+
title: "phpunit example",
164+
filename: "testdata/phpunit.xml",
165+
check: func(t *testing.T, suites []Suite) {
166+
assert.Len(t, suites, 1)
167+
assert.Len(t, suites[0].Tests, 0)
168+
assert.Len(t, suites[0].Suites, 1)
169+
170+
suite := suites[0].Suites[0]
171+
assert.Len(t, suite.Tests, 1)
172+
assert.Len(t, suite.Suites, 2)
173+
174+
assert.Equal(t, "SampleTest", suite.Name)
175+
assert.Equal(t, "/untitled/tests/SampleTest.php", suite.Properties["file"])
176+
177+
var testcase = Test{
178+
Name: "testA",
179+
Classname: "SampleTest",
180+
Duration: 5917 * time.Microsecond,
181+
Status: StatusPassed,
182+
Properties: map[string]string{
183+
"assertions": "1",
184+
"class": "SampleTest",
185+
"classname": "SampleTest",
186+
"file": "/untitled/tests/SampleTest.php",
187+
"line": "7",
188+
"name": "testA",
189+
"time": "0.005917",
190+
},
191+
}
192+
193+
assert.Equal(t, testcase, suite.Tests[0])
194+
195+
assert.Len(t, suite.Suites[1].Suites, 0)
196+
assert.Len(t, suite.Suites[1].Tests, 3)
197+
assert.Equal(t, "testC with data set #0", suite.Suites[1].Tests[0].Name)
198+
199+
// checking recursive aggregation
200+
suites[0].Aggregate()
201+
actualTotals := suites[0].Totals
202+
expectedTotals := Totals{
203+
Tests: 7,
204+
Passed: 4,
205+
Skipped: 0,
206+
Failed: 3,
207+
Error: 0,
208+
Duration: 8489 * time.Microsecond,
209+
}
210+
assert.Equal(t, expectedTotals, actualTotals)
211+
},
212+
},
162213
}
163214

164215
for index, test := range tests {

testdata/phpunit.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuites>
3+
<testsuite name="/untitled/tests" tests="7" assertions="7" errors="0" warnings="0" failures="3" skipped="0" time="0.008489">
4+
<testsuite name="SampleTest" file="/untitled/tests/SampleTest.php" tests="7" assertions="7" errors="0" warnings="0" failures="3" skipped="0" time="0.008489">
5+
<testcase name="testA" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="7" assertions="1" time="0.005917"/>
6+
<testsuite name="SampleTest::testB" tests="3" assertions="3" errors="0" warnings="0" failures="1" skipped="0" time="0.002378">
7+
<testcase name="testB with data set &quot;bool&quot;" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="16" assertions="1" time="0.002254">
8+
<failure type="PHPUnit\Framework\ExpectationFailedException">SampleTest::testB with data set "bool" (false)
9+
should be true
10+
Failed asserting that false matches expected true.
11+
12+
/untitled/tests/SampleTest.php:18
13+
</failure>
14+
</testcase>
15+
<testcase name="testB with data set &quot;int&quot;" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="16" assertions="1" time="0.000075"/>
16+
<testcase name="testB with data set &quot;string&quot;" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="16" assertions="1" time="0.000049"/>
17+
</testsuite>
18+
<testsuite name="SampleTest::testC" tests="3" assertions="3" errors="0" warnings="0" failures="2" skipped="0" time="0.000194">
19+
<testcase name="testC with data set #0" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="32" assertions="1" time="0.000060"/>
20+
<testcase name="testC with data set #1" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="32" assertions="1" time="0.000071">
21+
<failure type="PHPUnit\Framework\ExpectationFailedException">SampleTest::testC with data set #1 (0)
22+
should be true
23+
Failed asserting that 0 matches expected true.
24+
25+
/untitled/tests/SampleTest.php:34
26+
</failure>
27+
</testcase>
28+
<testcase name="testC with data set #2" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="32" assertions="1" time="0.000063">
29+
<failure type="PHPUnit\Framework\ExpectationFailedException">SampleTest::testC with data set #2 ('')
30+
should be true
31+
Failed asserting that '' matches expected true.
32+
33+
/untitled/tests/SampleTest.php:34
34+
</failure>
35+
</testcase>
36+
</testsuite>
37+
</testsuite>
38+
</testsuite>
39+
</testsuites>

types.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ type Suite struct {
7474
// Tests is an ordered collection of tests with associated results.
7575
Tests []Test `json:"tests,omitempty" yaml:"tests,omitempty"`
7676

77+
// Suites is an ordered collection of suites with associated tests.
78+
Suites []Suite `json:"suites,omitempty" yaml:"suites,omitempty"`
79+
7780
// SystemOut is textual test output for the suite. Usually output that is
7881
// written to stdout.
7982
SystemOut string `json:"stdout,omitempty" yaml:"stdout,omitempty"`
@@ -86,7 +89,7 @@ type Suite struct {
8689
Totals Totals `json:"totals" yaml:"totals"`
8790
}
8891

89-
// Aggregate calculates result sums across all tests.
92+
// Aggregate calculates result sums across all tests and nested suites.
9093
func (s *Suite) Aggregate() {
9194
totals := Totals{Tests: len(s.Tests)}
9295

@@ -104,6 +107,17 @@ func (s *Suite) Aggregate() {
104107
}
105108
}
106109

110+
// just summing totals from nested suites
111+
for _, suite := range s.Suites {
112+
suite.Aggregate()
113+
totals.Tests += suite.Totals.Tests
114+
totals.Duration += suite.Totals.Duration
115+
totals.Passed += suite.Totals.Passed
116+
totals.Skipped += suite.Totals.Skipped
117+
totals.Failed += suite.Totals.Failed
118+
totals.Error += suite.Totals.Error
119+
}
120+
107121
s.Totals = totals
108122
}
109123

0 commit comments

Comments
 (0)