diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 490eebd6e9..da3d6d1f9d 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -1004,6 +1004,25 @@ public static function bootEnvironment($app_root = NULL) {
     Unicode::check();
 
     // Indicate that code is operating in a test child site.
+    static::bootEnvironmentForTestChildSite();
+
+    // Set the Drupal custom error handler.
+    set_error_handler('_drupal_error_handler');
+    set_exception_handler('_drupal_exception_handler');
+
+    static::$isEnvironmentInitialized = TRUE;
+  }
+
+  /**
+   * Set up the environment for test-within-test cases.
+   *
+   * This method ensures that TestBase and WebTestBase tests are isolated from
+   * themselves.
+   *
+   * This method defines the DRUPAL_TEST_IN_CHILD_SITE constant. This variable
+   * should not be defined anywhere else in code.
+   */
+  protected static function bootEnvironmentForTestChildSite() {
     if (!defined('DRUPAL_TEST_IN_CHILD_SITE')) {
       if ($test_prefix = drupal_valid_test_ua()) {
         $test_db = new TestDatabase($test_prefix);
@@ -1032,12 +1051,6 @@ public static function bootEnvironment($app_root = NULL) {
         define('DRUPAL_TEST_IN_CHILD_SITE', FALSE);
       }
     }
-
-    // Set the Drupal custom error handler.
-    set_error_handler('_drupal_error_handler');
-    set_exception_handler('_drupal_exception_handler');
-
-    static::$isEnvironmentInitialized = TRUE;
   }
 
   /**
