diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index c3e18e1..2a52f80 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -2492,25 +2492,16 @@ 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 WebTestBase::curlInitialize()
+ * @see UnitTestBase::setUp()
  */
-function drupal_valid_test_ua($new_prefix = NULL) {
-  static $test_prefix;
-
-  if (isset($new_prefix)) {
-    $test_prefix = $new_prefix;
-  }
-  if (isset($test_prefix)) {
-    return $test_prefix;
-  }
-
+function drupal_valid_test_ua() {
   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 +2517,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/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
index 93cdbf2..f35788e 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;
 
@@ -1011,9 +1001,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..20c4e80 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
@@ -71,7 +71,8 @@ protected function setUp() {
     }
 
     // Set user agent to be consistent with WebTestBase.
-    $_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix;
+    // @see WebTestBase::curlInitialize()
+    $_SERVER['HTTP_USER_AGENT'] = drupal_generate_test_ua($this->databasePrefix);
 
     $this->setup = TRUE;
   }
