diff --git a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php
index 793c8c0..c42ba8e 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php
@@ -106,6 +106,12 @@ protected function setUp() {
       $class = get_parent_class($class);
     }
     $this->enableModules($modules, FALSE);
+
+    // Re-initialize the theme to ensure that tests do not see an inconsistent
+    // behavior when calling functions that would initialize the theme if it has
+    // not been initialized yet.
+    drupal_theme_initialize();
+
   }
 
   /**
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
index 93cdbf2..ec66409 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -883,11 +883,6 @@ protected function prepareEnvironment() {
     unset($GLOBALS['theme_key']);
     unset($GLOBALS['theme']);
 
-    // Re-initialize the theme to ensure that tests do not see an inconsistent
-    // behavior when calling functions that would initialize the theme if it has
-    // not been initialized yet.
-    drupal_theme_initialize();
-
     // Log fatal errors.
     ini_set('log_errors', 1);
     ini_set('error_log', $this->public_files_directory . '/error.log');
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index 6e41540..e3097b6 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -761,6 +761,12 @@ protected function setUp() {
       $this->rebuildContainer();
     }
 
+
+    // Re-initialize the theme to ensure that tests do not see an inconsistent
+    // behavior when calling functions that would initialize the theme if it has
+    // not been initialized yet.
+    drupal_theme_initialize();
+
     // Reset/rebuild all data structures after enabling the modules.
     $this->resetAll();
 
