diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
index 1052c51..6f17f57 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -692,6 +692,8 @@ protected function prepareEnvironment() {
     $this->originalContainer = clone drupal_container();
     $this->originalLanguage = $language_interface;
     $this->originalConfigDirectories = $GLOBALS['config_directories'];
+    $this->originalThemeKey = $GLOBALS['theme_key'];
+    $this->originalTheme = $GLOBALS['theme'];
 
     // Save further contextual information.
     $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
@@ -740,6 +742,15 @@ protected function prepareEnvironment() {
       $this->configDirectories[$type] = $this->originalFileDirectory . '/' . $path;
     }
 
+    // Unset globals.
+    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');
@@ -783,6 +794,10 @@ protected function tearDown() {
     Database::removeConnection('default');
     Database::renameConnection('simpletest_original_default', 'default');
 
+    // Restore original globals.
+    $GLOBALS['theme_key'] = $this->originalThemeKey;
+    $GLOBALS['theme'] = $this->originalTheme;
+
     // Reset all static variables.
     drupal_static_reset();
 
