diff --git a/core/modules/simpletest/drupal_web_test_case.php b/core/modules/simpletest/drupal_web_test_case.php
index 6d563f5..087bae2 100644
--- a/core/modules/simpletest/drupal_web_test_case.php
+++ b/core/modules/simpletest/drupal_web_test_case.php
@@ -706,21 +706,26 @@ class DrupalUnitTestCase extends DrupalTestCase {
   protected function setUp() {
     global $conf;
 
-    // Store necessary current values before switching to the test environment.
-    $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
-
-    // Reset all statics so that test is performed with a clean environment.
-    drupal_static_reset();
-
-    // Generate temporary prefixed database to ensure that tests have a clean starting point.
+    // Create the database prefix for this test.
     $this->databasePrefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
 
-    // Create test directory.
+    // Prepare the environment for running tests.
+    $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
+
     $public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
     file_prepare_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
     $conf['file_public_path'] = $public_files_directory;
 
-    // Clone the current connection and replace the current prefix.
+    $this->originalModuleList = module_list();
+
+    // Reset all statics and variables to perform tests in a clean environment.
+    $conf = array();
+    drupal_static_reset();
+
+    // Change the database prefix.
+    // All static variables need to be reset before the database prefix is
+    // changed, since Drupal\Core\Utility\CacheArray implementations attempt to
+    // write back to persistent caches when they are destructed.
     $connection_info = Database::getConnectionInfo('default');
     Database::renameConnection('default', 'simpletest_original_default');
     foreach ($connection_info as $target => $value) {
@@ -735,9 +740,8 @@ class DrupalUnitTestCase extends DrupalTestCase {
 
     // If locale is enabled then t() will try to access the database and
     // subsequently will fail as the database is not accessible.
-    $module_list = module_list();
-    if (isset($module_list['locale'])) {
-      $this->originalModuleList = $module_list;
+    if (isset($this->originalModuleList['locale'])) {
+      $module_list = $this->originalModuleList;
       unset($module_list['locale']);
       module_list(TRUE, FALSE, FALSE, $module_list);
     }
@@ -752,8 +756,9 @@ class DrupalUnitTestCase extends DrupalTestCase {
     Database::renameConnection('simpletest_original_default', 'default');
 
     $conf['file_public_path'] = $this->originalFileDirectory;
+
     // Restore modules if necessary.
-    if (isset($this->originalModuleList)) {
+    if (isset($this->originalModuleList['locale'])) {
       module_list(TRUE, FALSE, FALSE, $this->originalModuleList);
     }
   }
