Index: modules/simpletest/drupal_web_test_case.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v
retrieving revision 1.220
diff -u -p -r1.220 drupal_web_test_case.php
--- modules/simpletest/drupal_web_test_case.php	13 Jun 2010 13:14:18 -0000	1.220
+++ modules/simpletest/drupal_web_test_case.php	17 Jun 2010 07:13:09 -0000
@@ -1150,26 +1150,78 @@ class DrupalWebTestCase extends DrupalTe
     ini_set('log_errors', 1);
     ini_set('error_log', $public_files_directory . '/error.log');
 
-    // Reset all statics and variables to perform tests in a clean environment.
-    $conf = array();
-    drupal_static_reset();
+    $db_prefix = $this->originalPrefix;
+    if (unserialize(db_query("SELECT value FROM {variable} WHERE name = 'simpletest_base_tables_available'")->fetchField())) {
+      do {
+        if (!$lock_acquired = lock_acquire('simpletest_base_tables_available')) {
+          lock_wait('simpletest_base_tables_available');
+        }
+      } while ($lock_acquired === FALSE);
+
+      foreach (simpletest_get_base_tables() as $table) {
+        if (db_table_exists($db_prefix_new . $table)) {
+          db_truncate($db_prefix_new . $table);
+        }
+        else {
+          db_create_table($db_prefix_new . $table, drupal_get_schema($table));
+        }
+        db_query('INSERT INTO ' . $db_prefix_new . $table . ' SELECT * FROM {simpletest_base_' . $table . '}');
+      }
+      lock_release('simpletest_base_tables_available');
 
-    include_once DRUPAL_ROOT . '/includes/install.inc';
-    drupal_install_system();
+      $db_prefix = $db_prefix_new;
 
-    $this->preloadRegistry();
+      // Reset all statics and variables to perform tests in a clean environment.
+      $conf = array();
+      drupal_static_reset();
 
-    // Set path variables.
-    variable_set('file_public_path', $public_files_directory);
-    variable_set('file_private_path', $private_files_directory);
-    variable_set('file_temporary_path', $temp_files_directory);
-
-    // Include the default profile.
-    variable_set('install_profile', 'standard');
-    $profile_details = install_profile_info('standard', 'en');
+      // Set path variables.
+      variable_set('file_public_path', $public_files_directory);
+      variable_set('file_private_path', $private_files_directory);
+      variable_set('file_temporary_path', $temp_files_directory);
+    }
+    else {
+      $db_prefix = $db_prefix_new;
+      // Reset all statics and variables to perform tests in a clean environment.
+      $conf = array();
+      drupal_static_reset();
+
+      include_once DRUPAL_ROOT . '/includes/install.inc';
+      drupal_install_system();
+
+      $this->preloadRegistry();
+
+      // Set path variables.
+      variable_set('file_public_path', $public_files_directory);
+      variable_set('file_private_path', $private_files_directory);
+      variable_set('file_temporary_path', $temp_files_directory);
+
+      // Include the default profile.
+      variable_set('install_profile', 'standard');
+      $profile_details = install_profile_info('standard', 'en');
+
+      // Install the modules specified by the default profile.
+      module_enable($profile_details['dependencies'], FALSE);
+    }
 
-    // Install the modules specified by the default profile.
-    module_enable($profile_details['dependencies'], FALSE);
+    $db_prefix = $this->originalPrefix;
+    if (!unserialize(db_query("SELECT value FROM {variable} WHERE name = 'simpletest_base_tables_available'")->fetchField())) {
+      do {
+        if (!$lock_acquired = lock_acquire('simpletest_base_tables_available')) {
+          lock_wait('simpletest_base_tables_available');
+        }
+      } while ($lock_acquired === FALSE);
+
+      foreach (simpletest_get_base_tables() as $table) {
+        db_drop_table('simpletest_base_' . $table);
+        db_create_table('simpletest_base_' . $table, drupal_get_schema($table));
+        db_query('INSERT INTO {simpletest_base_' . $table . '} SELECT * FROM ' . $db_prefix_new . $table);
+      }
+      db_merge('variable')->key(array('name' => 'simpletest_base_tables_available'))->fields(array('value' => serialize(TRUE)))->execute();
+      lock_release('simpletest_base_tables_available');
+      cache_clear_all('variables', 'cache_bootstrap');
+    }
+    $db_prefix = $db_prefix_new;
 
     // Install modules needed for this test. This could have been passed in as
     // either a single array argument or a variable number of string arguments.
Index: modules/simpletest/simpletest.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.module,v
retrieving revision 1.91
diff -u -p -r1.91 simpletest.module
--- modules/simpletest/simpletest.module	30 Mar 2010 07:17:19 -0000	1.91
+++ modules/simpletest/simpletest.module	17 Jun 2010 07:13:10 -0000
@@ -143,6 +143,8 @@ function simpletest_run_tests($test_list
     ->useDefaults(array('test_id'))
     ->execute();
 
+  variable_set('simpletest_base_tables_available', FALSE);
+
   // Clear out the previous verbose files.
   file_unmanaged_delete_recursive(file_directory_path() . '/simpletest/verbose');
 
@@ -169,6 +171,78 @@ function simpletest_run_tests($test_list
   return $test_id;
 }
 
+function simpletest_get_base_tables() {
+  return array(
+    'actions',
+    'authmap',
+    'batch',
+    'block',
+    'block_custom',
+    'block_node_type',
+    'block_role',
+    'blocked_ips',
+    'cache',
+    'cache_block',
+    'cache_bootstrap',
+    'cache_field',
+    'cache_filter',
+    'cache_form',
+    'cache_image',
+    'cache_menu',
+    'cache_page',
+    'cache_path',
+    'comment',
+    'date_format_locale',
+    'date_format_type',
+    'date_formats',
+    'field_config',
+    'field_config_entity_type',
+    'field_config_instance',
+    'field_data_comment_body',
+    'field_revision_comment_body',
+    'file_managed',
+    'filter',
+    'filter_format',
+    'flood',
+    'history',
+    'image_effects',
+    'image_styles',
+    'menu_custom',
+    'menu_links',
+    'menu_router',
+    'node',
+    'node_access',
+    'node_comment_statistics',
+    'node_revision',
+    'node_type',
+    'queue',
+    'rdf_mapping',
+    'registry',
+    'registry_file',
+    'role',
+    'role_permission',
+    'search_dataset',
+    'search_index',
+    'search_node_links',
+    'search_total',
+    'semaphore',
+    'sequences',
+    'sessions',
+    'shortcut_set',
+    'shortcut_set_users',
+    'system',
+    'taxonomy_index',
+    'taxonomy_term_data',
+    'taxonomy_term_hierarchy',
+    'taxonomy_vocabulary',
+    'url_alias',
+    'users',
+    'users_roles',
+    'variable',
+    'watchdog',
+  );
+}
+
 /**
  * Batch operation callback.
  */
