Skip to content

Commit

Permalink
Support for nested test suites (joshdk#37)
Browse files Browse the repository at this point in the history
Co-authored-by: William Petit <[email protected]>
  • Loading branch information
ekharchenko-avito and William Petit authored Jul 2, 2020
1 parent e5d93c0 commit 6efcf40
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
8 changes: 6 additions & 2 deletions ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions testdata/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="/untitled/tests" tests="7" assertions="7" errors="0" warnings="0" failures="3" skipped="0" time="0.008489">
<testsuite name="SampleTest" file="/untitled/tests/SampleTest.php" tests="7" assertions="7" errors="0" warnings="0" failures="3" skipped="0" time="0.008489">
<testcase name="testA" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="7" assertions="1" time="0.005917"/>
<testsuite name="SampleTest::testB" tests="3" assertions="3" errors="0" warnings="0" failures="1" skipped="0" time="0.002378">
<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">
<failure type="PHPUnit\Framework\ExpectationFailedException">SampleTest::testB with data set "bool" (false)
should be true
Failed asserting that false matches expected true.

/untitled/tests/SampleTest.php:18
</failure>
</testcase>
<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"/>
<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"/>
</testsuite>
<testsuite name="SampleTest::testC" tests="3" assertions="3" errors="0" warnings="0" failures="2" skipped="0" time="0.000194">
<testcase name="testC with data set #0" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="32" assertions="1" time="0.000060"/>
<testcase name="testC with data set #1" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="32" assertions="1" time="0.000071">
<failure type="PHPUnit\Framework\ExpectationFailedException">SampleTest::testC with data set #1 (0)
should be true
Failed asserting that 0 matches expected true.

/untitled/tests/SampleTest.php:34
</failure>
</testcase>
<testcase name="testC with data set #2" class="SampleTest" classname="SampleTest" file="/untitled/tests/SampleTest.php" line="32" assertions="1" time="0.000063">
<failure type="PHPUnit\Framework\ExpectationFailedException">SampleTest::testC with data set #2 ('')
should be true
Failed asserting that '' matches expected true.

/untitled/tests/SampleTest.php:34
</failure>
</testcase>
</testsuite>
</testsuite>
</testsuite>
</testsuites>
16 changes: 15 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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)}

Expand All @@ -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
}

Expand Down

0 comments on commit 6efcf40

Please sign in to comment.