Index: modules/simpletest/simpletest.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.module,v
retrieving revision 1.56
diff -u -r1.56 simpletest.module
--- modules/simpletest/simpletest.module	5 Jul 2009 18:00:10 -0000	1.56
+++ modules/simpletest/simpletest.module	7 Jul 2009 03:07:39 -0000
@@ -191,11 +191,33 @@
     drupal_set_message(t('The test run finished in @elapsed.', array('@elapsed' => $elapsed)));
   }
   else {
+    simpletest_collect_errors($operations[0][1][1]);
+
     drupal_set_message(t('The test run did not successfully finish.'), 'error');
   }
   module_invoke_all('test_group_finished');
 }
 
+function simpletest_collect_errors($test_id) {
+  $last_prefix = db_result(db_query('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id)));
+  $last_prefix = substr($last_prefix, 10);
+
+  $test_class = db_result(db_query('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id', array(':test_id' => $test_id)));
+  $log = file_directory_path() . "/simpletest/$last_prefix/error.log";
+  foreach (file($log) as $line) {
+    if (preg_match('/.*?PHP Fatal error: (.*?) in (.*?) on line (\d+)/', $line, $match)) {
+      $caller = array(
+        'line' => $match[3],
+        'file' => $match[2],
+      );
+      DrupalTestCase::assertStatic($test_id, $test_class, FALSE, $match[1], 'Fatal error', $caller);
+    }
+    else {
+      DrupalTestCase::assertStatic($test_id, $test_class, FALSE, $line, 'Fatal error');
+    }
+  }
+}
+
 /**
  * Get a list of all of the tests provided by the system.
  *
@@ -326,11 +348,12 @@
  * Find all leftover temporary directories and remove them.
  */
 function simpletest_clean_temporary_directories() {
-  $files = scandir(file_directory_path());
+  $directory = file_directory_path() . '/simpletest';
+  $files = scandir($directory);
   $count = 0;
   foreach ($files as $file) {
-    $path = file_directory_path() . '/' . $file;
-    if (is_dir($path) && preg_match('/^simpletest\d+/', $file)) {
+    $path = "$directory/$file";
+    if (is_dir($path) && is_numeric($file)) {
       file_unmanaged_delete_recursive($path);
       $count++;
     }
Index: modules/simpletest/drupal_web_test_case.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v
retrieving revision 1.119
diff -u -r1.119 drupal_web_test_case.php
--- modules/simpletest/drupal_web_test_case.php	5 Jul 2009 18:00:10 -0000	1.119
+++ modules/simpletest/drupal_web_test_case.php	7 Jul 2009 03:07:39 -0000
@@ -137,6 +137,34 @@
     }
   }
 
+  public static function assertStatic($test_id, $test_class, $status, $message = '', $group = 'Other', array $caller = NULL) {
+    // Convert boolean status to string status.
+    if (is_bool($status)) {
+      $status = $status ? 'pass' : 'fail';
+    }
+
+    $caller += array(
+      'function' => t('N/A'),
+      'line' => -1,
+      'file' => t('N/A'),
+    );
+
+    $assertion = array(
+      'test_id' => $test_id,
+      'test_class' => $test_class,
+      'status' => $status,
+      'message' => $message,
+      'message_group' => $group,
+      'function' => $caller['function'],
+      'line' => $caller['line'],
+      'file' => $caller['file'],
+    );
+
+    db_insert('simpletest')
+      ->fields($assertion)
+      ->execute();
+  }
+
   /**
    * Cycles through backtrace until the first non-assertion method is found.
    *
@@ -981,7 +1009,12 @@
     $clean_url_original = variable_get('clean_url', 0);
 
     // Generate temporary prefixed database to ensure that tests have a clean starting point.
-    $db_prefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
+    $db_prefix_new = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
+    db_update('simpletest_test_id')
+      ->fields(array('last_prefix' => $db_prefix_new))
+      ->condition('test_id', $this->testId)
+      ->execute();
+    $db_prefix = $db_prefix_new;
 
     include_once DRUPAL_ROOT . '/includes/install.inc';
     drupal_install_system();
@@ -1038,11 +1071,15 @@
     variable_set('smtp_library', drupal_get_path('module', 'simpletest') . '/drupal_web_test_case.php');
 
     // Use temporary files directory with the same prefix as database.
-    variable_set('file_directory_path', $this->originalFileDirectory . '/' . $db_prefix);
+    variable_set('file_directory_path', $this->originalFileDirectory . '/simpletest/' . substr($db_prefix, 10));
     $directory = file_directory_path();
     // Create the files directory.
     file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
 
+    // Log fatal errors.
+    ini_set('log_errors', 1);
+    ini_set('error_log', $directory . '/error.log');
+
     set_time_limit($this->timeLimit);
   }
 
Index: modules/simpletest/simpletest.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.install,v
retrieving revision 1.21
diff -u -r1.21 simpletest.install
--- modules/simpletest/simpletest.install	27 May 2009 18:34:00 -0000	1.21
+++ modules/simpletest/simpletest.install	7 Jul 2009 03:07:39 -0000
@@ -229,6 +229,13 @@
         'description' => 'Primary Key: Unique simpletest ID used to group test results together. Each time a set of tests
                             are run a new test ID is used.',
       ),
+      'last_prefix' => array(
+        'type' => 'varchar',
+        'length' => 60,
+        'not null' => FALSE,
+        'default' => '',
+        'description' => 'The last database prefix used during testing.',
+      ),
     ),
     'primary key' => array('test_id'),
   );
