diff --git scripts/run-tests.sh scripts/run-tests.sh index 748fbbe..02b131d 100755 --- scripts/run-tests.sh +++ scripts/run-tests.sh @@ -87,9 +87,16 @@ simpletest_script_command($args['concurrency'], $test_id, implode(",", $test_lis list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id); simpletest_log_read($test_id, $last_prefix, $last_test_class); +// Stop the timer. +simpletest_script_reporter_timer_stop(); + // Display results before database is cleared. simpletest_script_reporter_display_results(); +if ($args['xml']) { + simpletest_script_reporter_write_xml_results(); +} + // Cleanup our test results. simpletest_clean_results_table($test_id); @@ -135,7 +142,11 @@ All arguments are long options. --file Run tests identified by specific file names, instead of group names. Specify the path and the extension (i.e. 'modules/user/user.test'). - --color Output the results with color highlighting. + --xml + + If provided, test results will be written as xml files to this path. + + --color Output text format results with color highlighting. --verbose Output detailed assertion messages in addition to summary. @@ -184,7 +195,8 @@ function simpletest_script_parse_args() { 'test_names' => array(), // Used internally. 'test-id' => NULL, - 'execute-batch' => FALSE + 'execute-batch' => FALSE, + 'xml' => '', ); // Override with set values. @@ -264,7 +276,7 @@ function simpletest_script_init($server_software) { if (!empty($args['url'])) { $parsed_url = parse_url($args['url']); $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''); - $path = $parsed_url['path']; + $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; // If the passed URL schema is 'https' then setup the $_SERVER variables // properly so that testing will run under https. @@ -450,7 +462,13 @@ function simpletest_script_get_test_list() { * Initialize the reporter. */ function simpletest_script_reporter_init() { - global $args, $all_tests, $test_list; + global $args, $all_tests, $test_list, $results_map; + + $results_map = array( + 'pass' => 'Pass', + 'fail' => 'Fail', + 'exception' => 'Exception' + ); echo "\n"; echo "Drupal test run\n"; @@ -480,15 +498,71 @@ function simpletest_script_reporter_init() { } /** - * Display test results. + * Display jUnit XML test results. */ -function simpletest_script_reporter_display_results() { +function simpletest_script_reporter_write_xml_results() { global $args, $test_id, $results_map; + $results = db_query("SELECT * FROM {simpletest} WHERE test_id = :test_id ORDER BY test_class, message_id", array(':test_id' => $test_id)); + + $test_class = ''; + $xml_files = array(); + + foreach ($results as $result) { + if (isset($results_map[$result->status])) { + if ($result->test_class != $test_class) { + // Display test class every time results are for new test class. + if (isset($xml_files[$test_class])) { + file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML()); + unset($xml_files[$test_class]); + } + $test_class = $result->test_class; + if (!isset($xml_files[$test_class])) { + $doc = new DomDocument('1.0'); + $root = $doc->createElement('testsuite'); + $root = $doc->appendChild($root); + $xml_files[$test_class] = array('doc' => $doc, 'suite' => $root); + } + } + // Save the result into the XML: + $case = $xml_files[$test_class]['doc']->createElement('testcase'); + $case->setAttribute('classname', $test_class); + list($class, $name) = explode('->', $result->function, 2); + $case->setAttribute('name', $name); + + if ($result->status == 'fail') { + $fail = $xml_files[$test_class]['doc']->createElement('failure'); + $fail->setAttribute('type', 'failure'); + $fail->setAttribute('message', $result->message_group); + $text = $xml_files[$test_class]['doc']->createTextNode($result->message); + $fail->appendChild($text); + $case->appendChild($fail); + } + $xml_files[$test_class]['suite']->appendChild($case); + } + } + // Save the last one: + if (isset($xml_files[$test_class])) { + file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML()); + unset($xml_files[$test_class]); + } +} + +/** + * Stop the test timer. + */ +function simpletest_script_reporter_timer_stop() { echo "\n"; $end = timer_stop('run-tests'); echo "Test run duration: " . format_interval($end['time'] / 1000); echo "\n"; +} + +/** + * Display test results. + */ +function simpletest_script_reporter_display_results() { + global $args, $test_id, $results_map; if ($args['verbose']) { // Report results. @@ -496,12 +570,6 @@ function simpletest_script_reporter_display_results() { echo "----------------------\n"; echo "\n"; - $results_map = array( - 'pass' => 'Pass', - 'fail' => 'Fail', - 'exception' => 'Exception' - ); - $results = db_query("SELECT * FROM {simpletest} WHERE test_id = :test_id ORDER BY test_class, message_id", array(':test_id' => $test_id)); $test_class = ''; foreach ($results as $result) {