diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
index b31d8a0..c0f156e 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -811,11 +811,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/UnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
index 98ed71d..79f7f77 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
@@ -66,6 +66,11 @@ protected function setUp() {
       return 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();
+
     // Set user agent to be consistent with WebTestBase.
     $_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix;
 
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index 400d885..de45b78 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -720,6 +720,11 @@ 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();
 
