diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index c3e18e1..a11163e 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -524,8 +524,10 @@ function find_conf_path($http_host, $script_name, $require_settings = TRUE) {
 function config_get_config_directory($type = CONFIG_ACTIVE_DIRECTORY) {
   global $config_directories;
 
+  // Determine whether we are in a unit test or web test and adjust the config
+  // directory automatically.
+  // @see Drupal\simpletest\TestBase::prepareEnvironment()
   if ($test_prefix = drupal_valid_test_ua()) {
-    // @see Drupal\simpletest\WebTestBase::setUp()
     $path = conf_path() . '/files/simpletest/' . substr($test_prefix, 10) . '/config_' . $type;
   }
   elseif (!empty($config_directories[$type])) {
@@ -2492,25 +2494,22 @@ function typed_data() {
 /**
  * Returns the test prefix if this is an internal request from SimpleTest.
  *
- * @param string $new_prefix
- *   Internal use only. A new prefix to be stored. Passed in by tests that use
- *   the test runner from within a test.
- *
- * @return
+ * @return string|false
  *   Either the simpletest prefix (the string "simpletest" followed by any
  *   number of digits) or FALSE if the user agent does not contain a valid
  *   HMAC and timestamp.
+ *
+ * @see drupal_generate_test_ua()
+ * @see TestBase::prepareEnvironment()
+ * @see WebTestBase::curlInitialize()
  */
-function drupal_valid_test_ua($new_prefix = NULL) {
-  static $test_prefix;
-
-  if (isset($new_prefix)) {
-    $test_prefix = $new_prefix;
+function drupal_valid_test_ua() {
+  // For unit tests, there is no request, but we still need to return the
+  // database prefix of the test, so the environment can be adjusted elsewhere.
+  // @see TestBase::prepareEnvironment()
+  if (!empty($GLOBALS['drupal_test_info']['test_run_id'])) {
+    return $GLOBALS['drupal_test_info']['test_run_id'];
   }
-  if (isset($test_prefix)) {
-    return $test_prefix;
-  }
-
   if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/^(simpletest\d+);(.+);(.+);(.+)$/", $_SERVER['HTTP_USER_AGENT'], $matches)) {
     list(, $prefix, $time, $salt, $hmac) = $matches;
     $check_string =  $prefix . ';' . $time . ';' . $salt;
@@ -2526,15 +2525,16 @@ function drupal_valid_test_ua($new_prefix = NULL) {
       return $test_prefix;
     }
   }
-
-  $test_prefix = FALSE;
-  return $test_prefix;
+  return FALSE;
 }
 
 /**
  * Generates a user agent string with a HMAC and timestamp for simpletest.
  */
 function drupal_generate_test_ua($prefix) {
+  // As of now, $drupal_hash_salt can only be changed in settings.php and does
+  // not change mid-request. As long as that is the case, it is safe to
+  // statically cache the computed $key.
   static $key;
 
   if (!isset($key)) {
diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index b011271..fd5e5c7 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -246,11 +246,8 @@ public function updateModules(array $module_list, array $module_filenames = arra
    */
   protected function getClassName() {
     $parts = array('service_container', $this->environment, $this->debug);
-    // Make sure to use a testing-specific container even in the parent site.
-    if (!empty($GLOBALS['drupal_test_info']['test_run_id'])) {
-      $parts[] = $GLOBALS['drupal_test_info']['test_run_id'];
-    }
-    elseif ($prefix = drupal_valid_test_ua()) {
+    // Make sure to use a testing-specific container.
+    if ($prefix = drupal_valid_test_ua()) {
       $parts[] = $prefix;
     }
     return implode('_', $parts);
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
index 93cdbf2..817b157 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -18,8 +18,12 @@
 /**
  * Base class for Drupal tests.
  *
- * Do not extend this class directly, use either Drupal\simpletest\WebTestBaseBase
- * or Drupal\simpletest\UnitTestBaseBase.
+ * Do not extend this class directly, use either Drupal\simpletest\WebTestBase
+ * or Drupal\simpletest\UnitTestBase.
+ *
+ * Requirements:
+ * - global $drupal_hash_salt needs to be set for drupal_generate_test_ua() to
+ *   generate a user agent HTTP header.
  */
 abstract class TestBase {
   /**
@@ -124,13 +128,6 @@
   protected $verboseDirectory;
 
   /**
-   * The original database prefix when running inside Simpletest.
-   *
-   * @var string
-   */
-  protected $originalPrefix;
-
-  /**
    * URL to the verbose output file directory.
    *
    * @var string
@@ -806,13 +803,6 @@ protected function prepareEnvironment() {
     global $user, $conf;
     $language_interface = language(LANGUAGE_TYPE_INTERFACE);
 
-    // When running the test runner within a test, back up the original database
-    // prefix and re-set the new/nested prefix in drupal_valid_test_ua().
-    if (drupal_valid_test_ua()) {
-      $this->originalPrefix = drupal_valid_test_ua();
-      drupal_valid_test_ua($this->databasePrefix);
-    }
-
     // Backup current in-memory configuration.
     $this->originalConf = $conf;
 
@@ -856,9 +846,17 @@ protected function prepareEnvironment() {
     file_prepare_directory($this->translation_files_directory, FILE_CREATE_DIRECTORY);
     $this->generatedTestFiles = FALSE;
 
-    // Create and set new configuration directories. The child site
-    // uses drupal_valid_test_ua() to adjust the config directory paths to
-    // a test-prefix-specific directory within the public files directory.
+    // Set the test information, which allows other parts of Drupal to adapt for
+    // the test environment.
+    // @see drupal_valid_test_ua()
+    $test_info = &$GLOBALS['drupal_test_info'];
+    $test_info['test_run_id'] = $this->databasePrefix;
+    $test_info['in_child_site'] = FALSE;
+
+    // Create and set new configuration directories.
+    // The test environment uses drupal_valid_test_ua() to adjust the config
+    // directory paths to a test-prefix-specific directory within the public
+    // files directory, both for unit tests and for web tests.
     // @see config_get_config_directory()
     $GLOBALS['config_directories'] = array();
     $this->configDirectories = array();
@@ -892,11 +890,6 @@ protected function prepareEnvironment() {
     ini_set('log_errors', 1);
     ini_set('error_log', $this->public_files_directory . '/error.log');
 
-    // Set the test information for use in other parts of Drupal.
-    $test_info = &$GLOBALS['drupal_test_info'];
-    $test_info['test_run_id'] = $this->databasePrefix;
-    $test_info['in_child_site'] = FALSE;
-
     // Indicate the environment was set up correctly.
     $this->setupEnvironment = TRUE;
   }
@@ -993,6 +986,10 @@ protected function tearDown() {
     }
     $GLOBALS['theme'] = $this->originalTheme;
 
+    // Unset the test information, so test environment overrides are no longer
+    // applied.
+    unset($GLOBALS['drupal_test_info']);
+
     // Reset all static variables.
     // All destructors of statically cached objects have been invoked above;
     // this second reset is guranteed to reset everything to nothing.
@@ -1011,9 +1008,6 @@ protected function tearDown() {
     // Restore original statics and globals.
     drupal_container($this->originalContainer);
     $GLOBALS['config_directories'] = $this->originalConfigDirectories;
-    if (isset($this->originalPrefix)) {
-      drupal_valid_test_ua($this->originalPrefix);
-    }
 
     // Reset module list and module load status.
     module_list_reset();
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
index 648aa42..c629da0 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
@@ -70,9 +70,6 @@ protected function setUp() {
       return FALSE;
     }
 
-    // Set user agent to be consistent with WebTestBase.
-    $_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix;
-
     $this->setup = TRUE;
   }
 }
