diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
index 6517bd1..f129a0f 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -86,6 +86,10 @@ abstract class TestBase {
    */
   protected $setup = FALSE;
 
+  protected $setupDatabasePrefix = FALSE;
+
+  protected $setupEnvironment = FALSE;
+
   /**
    * Constructor for Test.
    *
@@ -582,6 +586,12 @@ abstract class TestBase {
   protected function changeDatabasePrefix() {
     if (empty($this->databasePrefix)) {
       $this->prepareDatabasePrefix();
+      // If $this->prepareDatabasePrefix() failed to work, return without
+      // setting $this->setupDatabasePrefix to TRUE, so setUp() methods will
+      // know to bail out.
+      if (empty($this->databasePrefix)) {
+        return;
+      }
     }
 
     // Clone the current connection and replace the current prefix.
@@ -593,6 +603,9 @@ abstract class TestBase {
       );
     }
     Database::addConnectionInfo('default', 'default', $connection_info['default']);
+
+    // Indicate the database prefix was set up correctly.
+    $this->setupDatabasePrefix = TRUE;
   }
 
   /**
@@ -661,6 +674,9 @@ abstract class TestBase {
     $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;
   }
 
   /**
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
index a871639..d54912a 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
@@ -43,6 +43,9 @@ abstract class UnitTestBase extends TestBase {
 
     // Prepare the environment for running tests.
     $this->prepareEnvironment();
+    if (!$this->setupEnvironment) {
+      return FALSE;
+    }
     $this->originalThemeRegistry = theme_get_registry(FALSE);
 
     // Reset all statics and variables to perform tests in a clean environment.
@@ -65,6 +68,9 @@ abstract class UnitTestBase extends TestBase {
     // changed, since Drupal\Core\Utility\CacheArray implementations attempt to
     // write back to persistent caches when they are destructed.
     $this->changeDatabasePrefix();
+    if (!$this->setupDatabasePrefix) {
+      return FALSE;
+    }
 
     // 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 0856611..1f70278 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -586,6 +586,9 @@ abstract class WebTestBase extends TestBase {
 
     // Prepare the environment for running tests.
     $this->prepareEnvironment();
+    if (!$this->setupEnvironment) {
+      return FALSE;
+    }
 
     // Reset all statics and variables to perform tests in a clean environment.
     $conf = array();
@@ -596,6 +599,9 @@ abstract class WebTestBase extends TestBase {
     // changed, since Drupal\Core\Utility\CacheArray implementations attempt to
     // write back to persistent caches when they are destructed.
     $this->changeDatabasePrefix();
+    if (!$this->setupDatabasePrefix) {
+      return FALSE;
+    }
 
     // Preset the 'install_profile' system variable, so the first call into
     // system_rebuild_module_data() (in drupal_install_system()) will register
diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php
index 4d12123..4d0d002 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php
@@ -93,6 +93,9 @@ abstract class UpgradePathTestBase extends WebTestBase {
 
     // Prepare the environment for running tests.
     $this->prepareEnvironment();
+    if (!$this->setupEnvironment) {
+      return FALSE;
+    }
 
     // Reset all statics and variables to perform tests in a clean environment.
     $conf = array();
@@ -103,6 +106,9 @@ abstract class UpgradePathTestBase extends WebTestBase {
     // changed, since Drupal\Core\Utility\CacheArray implementations attempt to
     // write back to persistent caches when they are destructed.
     $this->changeDatabasePrefix();
+    if (!$this->setupDatabasePrefix) {
+      return FALSE;
+    }
 
     // Unregister the registry.
     // This is required to make sure that the database layer works properly.
