Index: includes/database/mysql/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/mysql/database.inc,v
retrieving revision 1.3
diff -u -p -r1.3 database.inc
--- includes/database/mysql/database.inc	21 Sep 2008 15:27:20 -0000	1.3
+++ includes/database/mysql/database.inc	11 Oct 2008 10:27:23 -0000
@@ -36,7 +36,7 @@ class DatabaseConnection_mysql extends D
 
     // Enable MySQL's "strict mode", which removes most of MySQL's
     // "just be lazy" behaviors that end up causing more trouble than they're worth.
-    $this->exec('SET sql_mode=STRICT_ALL_TABLES');
+    $this->exec("SET sql_mode='STRICT_ALL_TABLES,NO_AUTO_VALUE_ON_ZERO'");
   }
 
   public function queryRange($query, Array $args, $from, $count, Array $options) {
Index: modules/simpletest/drupal_web_test_case.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v
retrieving revision 1.51
diff -u -p -r1.51 drupal_web_test_case.php
--- modules/simpletest/drupal_web_test_case.php	15 Oct 2008 16:05:51 -0000	1.51
+++ modules/simpletest/drupal_web_test_case.php	11 Oct 2008 10:27:23 -0000
@@ -294,6 +294,7 @@ class DrupalWebTestCase {
    * Run all tests in this class.
    */
   function run() {
+    global $db_prefix;
     set_error_handler(array($this, 'errorHandler'));
     $methods = array();
     // Iterate through all the methods in this class.
@@ -311,9 +312,15 @@ class DrupalWebTestCase {
         $this->tearDown();
       }
     }
+
     // Clear out the error messages and restore error handler.
     drupal_get_messages();
     restore_error_handler();
+
+    // Clean-up the reference environment.
+    $db_prefix = $this->reference_db_prefix;
+    $this->cleanUp();
+    $db_prefix = $this->db_prefix_original;
   }
 
   /**
@@ -689,30 +696,47 @@ class DrupalWebTestCase {
     $clean_url_original = variable_get('clean_url', 0);
 
     // Generate temporary prefixed database to ensure that tests have a clean starting point.
-    $db_prefix = 'simpletest' . mt_rand(1000, 1000000);
+    $new_db_prefix = 'simpletest' . mt_rand(1000, 1000000);
+
+    if (!$this->reference_db_prefix) {
+      $db_prefix = $this->reference_db_prefix = 'simpletest' . mt_rand(1000, 1000000);
+
+      include_once './includes/install.inc';
+      drupal_install_system();
+
+      // Add the specified modules to the list of modules in the default profile.
+      $args = func_get_args();
+      $modules = array_unique(array_merge(drupal_verify_profile('default', 'en'), $args));
+      exec("echo " . implode(',', $modules) . " >>/tmp/modules");
+      drupal_install_modules($modules);
+
+      // Because the schema is static cached, we need to flush
+      // it between each run.  If we don't, then it will contain
+      // stale data for the previous run's database prefix and all
+      // calls to it will fail.
+      drupal_get_schema(NULL, TRUE);
+
+      // Run default profile tasks.
+      $task = 'profile';
+      default_profile_tasks($task, '');
+
+      // Rebuild caches.
+      menu_rebuild();
+      actions_synchronize();
+      _drupal_flush_css_js();
+    }
+
+    // Now, copy the reference environment.
+    $db_prefix = $this->reference_db_prefix;
+    $schema = drupal_get_schema();
+    $db_prefix = "";
 
-    include_once DRUPAL_ROOT . '/includes/install.inc';
-    drupal_install_system();
+    foreach ($schema as $name => $table) {
+     db_create_table($ret, $new_db_prefix . $name, $table);
+     db_query("INSERT INTO " . $new_db_prefix . $name . " SELECT * FROM " . $this->reference_db_prefix . $name);
+    }
 
-    // Add the specified modules to the list of modules in the default profile.
-    $args = func_get_args();
-    $modules = array_unique(array_merge(drupal_get_profile_modules('default', 'en'), $args));
-    drupal_install_modules($modules);
-
-    // Because the schema is static cached, we need to flush
-    // it between each run.  If we don't, then it will contain
-    // stale data for the previous run's database prefix and all
-    // calls to it will fail.
-    drupal_get_schema(NULL, TRUE);
-
-    // Run default profile tasks.
-    $task = 'profile';
-    default_profile_tasks($task, '');
-
-    // Rebuild caches.
-    menu_rebuild();
-    actions_synchronize();
-    _drupal_flush_css_js();
+    $db_prefix = $new_db_prefix;
     $this->refreshVariables();
     $this->checkPermissions(array(), TRUE);
 
@@ -757,12 +781,7 @@ class DrupalWebTestCase {
       simpletest_clean_temporary_directory(file_directory_path());
       variable_set('file_directory_path', $this->original_file_directory);
 
-      // Remove all prefixed tables (all the tables in the schema).
-      $schema = drupal_get_schema(NULL, TRUE);
-      $ret = array();
-      foreach ($schema as $name => $table) {
-        db_drop_table($ret, $name);
-      }
+      $this->cleanUp();
 
       // Return the database prefix to the original.
       $db_prefix = $this->db_prefix_original;
@@ -780,6 +799,17 @@ class DrupalWebTestCase {
       $this->curlClose();
     }
   }
+  
+  /**
+   * Remove all tables from the environment.
+   */
+  private function cleanUp() {
+    $schema = drupal_get_schema(NULL, TRUE);
+    $ret = array();
+    foreach ($schema as $name => $table) {
+      db_drop_table($ret, $name);
+    }
+  }
 
   /**
    * Initializes the cURL connection and gets a session cookie.
