From aebcd7a985c07c397a0a0ec4d8ab935e7a4ee5e5 Mon Sep 17 00:00:00 2001 From: Aviv Eyal Date: Mon, 30 Sep 2013 10:43:08 -0700 Subject: [PATCH] Extract xUnit test results parser Summary: Many test frameworks can format their output in xUnit-like format. Test Plan: Tested the Nose engine with a Nose one, and the pytest with a demo project. Reviewers: epriestley Reviewed By: epriestley CC: Korvin, aran Differential Revision: https://secure.phabricator.com/D7011 Conflicts: src/__phutil_library_map__.php --- src/__phutil_library_map__.php | 3 + .../engine/ArcanistXUnitTestResultParser.php | 97 +++++++++++++++++++ src/unit/engine/NoseTestEngine.php | 68 +------------ src/unit/engine/PytestTestEngine.php | 70 +------------ .../XUnitTestResultParserTestCase.php | 55 +++++++++++ .../__tests__/testresults/xunit.invallid-xml | 1 + .../__tests__/testresults/xunit.no-tests | 1 + .../engine/__tests__/testresults/xunit.simple | 13 +++ 8 files changed, 177 insertions(+), 131 deletions(-) create mode 100644 src/unit/engine/ArcanistXUnitTestResultParser.php create mode 100755 src/unit/engine/__tests__/XUnitTestResultParserTestCase.php create mode 100755 src/unit/engine/__tests__/testresults/xunit.invallid-xml create mode 100755 src/unit/engine/__tests__/testresults/xunit.no-tests create mode 100755 src/unit/engine/__tests__/testresults/xunit.simple diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index fdb205d5..5b597ce1 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -163,6 +163,7 @@ phutil_register_library_map(array( 'ArcanistXHPASTLintTestSwitchHook' => 'lint/linter/__tests__/ArcanistXHPASTLintTestSwitchHook.php', 'ArcanistXHPASTLinter' => 'lint/linter/ArcanistXHPASTLinter.php', 'ArcanistXHPASTLinterTestCase' => 'lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php', + 'ArcanistXUnitTestResultParser' => 'unit/engine/ArcanistXUnitTestResultParser.php', 'CSharpToolsTestEngine' => 'unit/engine/CSharpToolsTestEngine.php', 'ComprehensiveLintEngine' => 'lint/engine/ComprehensiveLintEngine.php', 'ExampleLintEngine' => 'lint/engine/ExampleLintEngine.php', @@ -178,6 +179,7 @@ phutil_register_library_map(array( 'PytestTestEngine' => 'unit/engine/PytestTestEngine.php', 'UnitTestableArcanistLintEngine' => 'lint/engine/UnitTestableArcanistLintEngine.php', 'XUnitTestEngine' => 'unit/engine/XUnitTestEngine.php', + 'XUnitTestResultParserTestCase' => 'unit/engine/__tests__/XUnitTestResultParserTestCase.php', ), 'function' => array( @@ -318,5 +320,6 @@ phutil_register_library_map(array( 'PytestTestEngine' => 'ArcanistBaseUnitTestEngine', 'UnitTestableArcanistLintEngine' => 'ArcanistLintEngine', 'XUnitTestEngine' => 'ArcanistBaseUnitTestEngine', + 'XUnitTestResultParserTestCase' => 'ArcanistTestCase', ), )); diff --git a/src/unit/engine/ArcanistXUnitTestResultParser.php b/src/unit/engine/ArcanistXUnitTestResultParser.php new file mode 100644 index 00000000..a3fdacf8 --- /dev/null +++ b/src/unit/engine/ArcanistXUnitTestResultParser.php @@ -0,0 +1,97 @@ +loadXML($test_results); + + if (!$load_success) { + $input_start = phutil_utf8_shorten($test_results, 150); + throw new Exception( + "Failed to load XUnit report; Input starts with:\n\n {$input_start}"); + } + + $results = array(); + $testcases = $xunit_dom->getElementsByTagName("testcase"); + foreach ($testcases as $testcase) { + $classname = $testcase->getAttribute("classname"); + $name = $testcase->getAttribute("name"); + $time = $testcase->getAttribute("time"); + + $status = ArcanistUnitTestResult::RESULT_PASS; + $user_data = ""; + + // A skipped test is a test which was ignored using framework + // mechanizms (e.g. @skip decorator) + $skipped = $testcase->getElementsByTagName("skipped"); + if ($skipped->length > 0) { + $status = ArcanistUnitTestResult::RESULT_SKIP; + $messages = array(); + for ($ii = 0; $ii < $skipped->length; $ii++) { + $messages[] = trim($skipped->item($ii)->nodeValue, " \n"); + } + + $user_data .= implode("\n", $messages); + } + + // Failure is a test which the code has explicitly failed by using + // the mechanizms for that purpose. e.g., via an assertEquals + $failures = $testcase->getElementsByTagName("failure"); + if ($failures->length > 0) { + $status = ArcanistUnitTestResult::RESULT_FAIL; + $messages = array(); + for ($ii = 0; $ii < $failures->length; $ii++) { + $messages[] = trim($failures->item($ii)->nodeValue, " \n"); + } + + $user_data .= implode("\n", $messages)."\n"; + } + + // An errored test is one that had an unanticipated problem. e.g., an + // unchecked throwable, or a problem with an implementation of the + // test. + $errors = $testcase->getElementsByTagName("error"); + if ($errors->length > 0) { + $status = ArcanistUnitTestResult::RESULT_BROKEN; + $messages = array(); + for ($ii = 0; $ii < $errors->length; $ii++) { + $messages[] = trim($errors->item($ii)->nodeValue, " \n"); + } + + $user_data .= implode("\n", $messages)."\n"; + } + + $result = new ArcanistUnitTestResult(); + $result->setName($classname.".".$name); + $result->setResult($status); + $result->setDuration($time); + $result->setUserData($user_data); + + $results[] = $result; + } + + return $results; + } + +} diff --git a/src/unit/engine/NoseTestEngine.php b/src/unit/engine/NoseTestEngine.php index 49f03994..00296a08 100644 --- a/src/unit/engine/NoseTestEngine.php +++ b/src/unit/engine/NoseTestEngine.php @@ -77,6 +77,7 @@ final class NoseTestEngine extends ArcanistBaseUnitTestEngine { $xunit_tmp = $tmpfiles[$test_path]['xunit']; $cover_tmp = $tmpfiles[$test_path]['cover']; + $this->parser = new ArcanistXUnitTestResultParser(); $results[] = $this->parseTestResults($source_path, $xunit_tmp, $cover_tmp); @@ -99,74 +100,15 @@ final class NoseTestEngine extends ArcanistBaseUnitTestEngine { } public function parseTestResults($source_path, $xunit_tmp, $cover_tmp) { - // xunit xsd: https://gist.github.com/959290 - $xunit_dom = new DOMDocument(); - $xunit_dom->loadXML(Filesystem::readFile($xunit_tmp)); + $results = $this->parser->parseTestResults( + Filesystem::readFile($xunit_tmp)); // coverage is for all testcases in the executed $path - $coverage = array(); if ($this->getEnableCoverage() !== false) { $coverage = $this->readCoverage($cover_tmp, $source_path); - } - - $results = array(); - $testcases = $xunit_dom->getElementsByTagName("testcase"); - foreach ($testcases as $testcase) { - $classname = $testcase->getAttribute("classname"); - $name = $testcase->getAttribute("name"); - $time = $testcase->getAttribute("time"); - - $status = ArcanistUnitTestResult::RESULT_PASS; - $user_data = ""; - - // A skipped test is a test which was ignored using framework - // mechanizms (e.g. @skip decorator) - $skipped = $testcase->getElementsByTagName("skipped"); - if ($skipped->length > 0) { - $status = ArcanistUnitTestResult::RESULT_SKIP; - $messages = array(); - for ($ii = 0; $ii < $skipped->length; $ii++) { - $messages[] = trim($skipped->item($ii)->nodeValue, " \n"); - } - - $user_data .= implode("\n", $messages); + foreach ($results as $result) { + $result->setCoverage($coverage); } - - // Failure is a test which the code has explicitly failed by using - // the mechanizms for that purpose. e.g., via an assertEquals - $failures = $testcase->getElementsByTagName("failure"); - if ($failures->length > 0) { - $status = ArcanistUnitTestResult::RESULT_FAIL; - $messages = array(); - for ($ii = 0; $ii < $failures->length; $ii++) { - $messages[] = trim($failures->item($ii)->nodeValue, " \n"); - } - - $user_data .= implode("\n", $messages)."\n"; - } - - // An errored test is one that had an unanticipated problem. e.g., an - // unchecked throwable, or a problem with an implementation of the - // test. - $errors = $testcase->getElementsByTagName("error"); - if ($errors->length > 0) { - $status = ArcanistUnitTestResult::RESULT_BROKEN; - $messages = array(); - for ($ii = 0; $ii < $errors->length; $ii++) { - $messages[] = trim($errors->item($ii)->nodeValue, " \n"); - } - - $user_data .= implode("\n", $messages)."\n"; - } - - $result = new ArcanistUnitTestResult(); - $result->setName($classname.".".$name); - $result->setResult($status); - $result->setDuration($time); - $result->setCoverage($coverage); - $result->setUserData($user_data); - - $results[] = $result; } return $results; diff --git a/src/unit/engine/PytestTestEngine.php b/src/unit/engine/PytestTestEngine.php index 37a43b72..ca2da944 100644 --- a/src/unit/engine/PytestTestEngine.php +++ b/src/unit/engine/PytestTestEngine.php @@ -12,78 +12,12 @@ final class PytestTestEngine extends ArcanistBaseUnitTestEngine { $cmd_line = csprintf('py.test --junitxml=%s', $junit_tmp); - $future = new ExecFuture('%C', $cmd_line); list($stdout, $stderr) = $future->resolvex(); - return $this->parseTestResults($junit_tmp); - } - - public function parseTestResults($junit_tmp) { - // xunit xsd: https://gist.github.com/959290 - $xunit_dom = new DOMDocument(); - $xunit_dom->loadXML(Filesystem::readFile($junit_tmp)); - - $results = array(); - $testcases = $xunit_dom->getElementsByTagName("testcase"); - foreach ($testcases as $testcase) { - $classname = $testcase->getAttribute("classname"); - $name = $testcase->getAttribute("name"); - $time = $testcase->getAttribute("time"); - - $status = ArcanistUnitTestResult::RESULT_PASS; - $user_data = ""; - - // A skipped test is a test which was ignored using framework - // mechanizms (e.g. @skip decorator) - $skipped = $testcase->getElementsByTagName("skipped"); - if ($skipped->length > 0) { - $status = ArcanistUnitTestResult::RESULT_SKIP; - $messages = array(); - for ($ii = 0; $ii < $skipped->length; $ii++) { - $messages[] = trim($skipped->item($ii)->nodeValue, " \n"); - } - - $user_data .= implode("\n", $messages); - } - - // Failure is a test which the code has explicitly failed by using - // the mechanizms for that purpose. e.g., via an assertEquals - $failures = $testcase->getElementsByTagName("failure"); - if ($failures->length > 0) { - $status = ArcanistUnitTestResult::RESULT_FAIL; - $messages = array(); - for ($ii = 0; $ii < $failures->length; $ii++) { - $messages[] = trim($failures->item($ii)->nodeValue, " \n"); - } - - $user_data .= implode("\n", $messages)."\n"; - } - - // An errored test is one that had an unanticipated problem. e.g., an - // unchecked throwable, or a problem with an implementation of the - // test. - $errors = $testcase->getElementsByTagName("error"); - if ($errors->length > 0) { - $status = ArcanistUnitTestResult::RESULT_BROKEN; - $messages = array(); - for ($ii = 0; $ii < $errors->length; $ii++) { - $messages[] = trim($errors->item($ii)->nodeValue, " \n"); - } - - $user_data .= implode("\n", $messages)."\n"; - } - - $result = new ArcanistUnitTestResult(); - $result->setName($classname.".".$name); - $result->setResult($status); - $result->setDuration($time); - $result->setUserData($user_data); - - $results[] = $result; - } + $parser = new ArcanistXUnitTestResultParser(); - return $results; + return $parser->parseTestResults(Filesystem::readFile($junit_tmp)); } } diff --git a/src/unit/engine/__tests__/XUnitTestResultParserTestCase.php b/src/unit/engine/__tests__/XUnitTestResultParserTestCase.php new file mode 100755 index 00000000..e752cfbe --- /dev/null +++ b/src/unit/engine/__tests__/XUnitTestResultParserTestCase.php @@ -0,0 +1,55 @@ +parseTestResults($stubbed_results); + + $this->assertEqual(0, count($parsed_results)); + } + + public function testAcceptsSimpleInput() { + $stubbed_results = Filesystem::readFile( + dirname(__FILE__).'/testresults/xunit.simple'); + $parsed_results = id(new ArcanistXUnitTestResultParser()) + ->parseTestResults($stubbed_results); + + $this->assertEqual(3, count($parsed_results)); + } + + public function testEmptyInputFailure() { + try { + $parsed_results = id(new ArcanistXUnitTestResultParser()) + ->parseTestResults(''); + + $this->failTest('Should throw on empty input'); + } catch (Exception $e) { + // OK + } + } + + public function testInvalidXmlInputFailure() { + $stubbed_results = Filesystem::readFile( + dirname(__FILE__).'/testresults/xunit.invallid-xml'); + try { + $parsed_results = id(new ArcanistXUnitTestResultParser()) + ->parseTestResults($stubbed_results); + + $this->failTest('Should throw on non-xml input'); + } catch (Exception $e) { + // OK + } + } + +} diff --git a/src/unit/engine/__tests__/testresults/xunit.invallid-xml b/src/unit/engine/__tests__/testresults/xunit.invallid-xml new file mode 100755 index 00000000..3dfdc59b --- /dev/null +++ b/src/unit/engine/__tests__/testresults/xunit.invallid-xml @@ -0,0 +1 @@ +something else diff --git a/src/unit/engine/__tests__/testresults/xunit.no-tests b/src/unit/engine/__tests__/testresults/xunit.no-tests new file mode 100755 index 00000000..11aa5365 --- /dev/null +++ b/src/unit/engine/__tests__/testresults/xunit.no-tests @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/unit/engine/__tests__/testresults/xunit.simple b/src/unit/engine/__tests__/testresults/xunit.simple new file mode 100755 index 00000000..ca3559fd --- /dev/null +++ b/src/unit/engine/__tests__/testresults/xunit.simple @@ -0,0 +1,13 @@ + + + + def test_answer(): +> assert func(3) == 5 +E assert 4 == 5 +E + where 4 = func(3) + +tests.py:5: AssertionError + + + + -- 2.43.0