diff -u -r1.1.2.9 run-tests.sh
--- run-tests.sh	1 Dec 2010 06:55:26 -0000	1.1.2.9
+++ run-tests.sh	14 Jan 2011 21:12:44 -0000
@@ -91,6 +91,10 @@
 // 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,6 +139,10 @@
   --file      Run tests identified by specific file names, instead of group names.
               Specify the path and the extension (i.e. 'modules/user/user.test').
 
+  --xml       <path>
+
+              If provided, test results will be written as xml files to this path.
+
   --color     Output the results with color highlighting.
 
   --verbose   Output detailed assertion messages in addition to summary.
@@ -184,7 +192,8 @@
     'test_names' => array(),
     // Used internally.
     'test-id' => NULL,
-    'execute-batch' => FALSE
+    'execute-batch' => FALSE,
+    'xml' => '',
   );
 
   // Override with set values.
@@ -478,6 +487,12 @@
 function simpletest_script_reporter_display_results() {
   global $args, $test_id, $results_map;
 
+  $results_map = array(
+    'pass' => 'Pass',
+    'fail' => 'Fail',
+    'exception' => 'Exception'
+  );
+
   echo "\n";
   $end = timer_stop('run-tests');
   echo "Test run duration: " . format_interval($end['time'] / 1000);
@@ -489,12 +504,6 @@
     echo "----------------------\n";
     echo "\n";
 
-    $results_map = array(
-      'pass' => 'Pass',
-      'fail' => 'Fail',
-      'exception' => 'Exception'
-    );
-
     $results = db_query("SELECT * FROM {simpletest} WHERE test_id = %d ORDER BY test_class, message_id", $test_id);
 
     $test_class = '';
@@ -513,6 +522,77 @@
 }
 
 /**
+ * Display jUnit XML test 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));
+  $results = db_query("SELECT * FROM {simpletest} WHERE test_id = %d ORDER BY test_class, message_id", $test_id);
+
+  $test_class = '';
+  $xml_files = array();
+
+//  foreach ($results as $result) {
+  while ($result = db_fetch_object($results)) {
+    if (isset($results_map[$result->status])) {
+      if ($result->test_class != $test_class) {
+        // We've moved onto a new class, so write the last classes results to a file:
+        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);
+        }
+      }
+      // For convenience:
+      $dom_document = &$xml_files[$test_class]['doc'];
+
+      // Create the XML element for this test case:
+      $case = $dom_document->createElement('testcase');
+      $case->setAttribute('classname', $test_class);
+      list($class, $name) = explode('->', $result->function, 2);
+      $case->setAttribute('name', $name);
+
+      // Passes get no further attention, but failures and exceptions get to add more detail:
+      if ($result->status == 'fail') {
+        $fail = $dom_document->createElement('failure');
+        $fail->setAttribute('type', 'failure');
+        $fail->setAttribute('message', $result->message_group);
+        $text = $dom_document->createTextNode($result->message);
+        $fail->appendChild($text);
+        $case->appendChild($fail);
+      }
+      elseif ($result->status == 'exception') {
+        // In the case of an exception the $result->function may not be a class
+        // method so we record the full function name:
+        $case->setAttribute('name', $result->function);
+
+        $fail = $dom_document->createElement('error');
+        $fail->setAttribute('type', 'exception');
+        $fail->setAttribute('message', $result->message_group);
+        $full_message = $result->message . "\n\nline: " . $result->line . "\nfile: " . $result->file;
+        $text = $dom_document->createTextNode($full_message);
+        $fail->appendChild($text);
+        $case->appendChild($fail);
+      }
+      // Append the test case XML to the test suite:
+      $xml_files[$test_class]['suite']->appendChild($case);
+    }
+  }
+  // The last test case hasn't been saved to a file yet, so do that now:
+  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]);
+  }
+}
+
+/**
  * Format the result so that it fits within the default 80 character
  * terminal size.
  *
