diff -u b/core/tests/bootstrap.php b/core/tests/bootstrap.php
--- b/core/tests/bootstrap.php
+++ b/core/tests/bootstrap.php
@@ -7,10 +7,11 @@
  * @see phpunit.xml.dist
  */
 
+require_once __DIR__ . '/../includes/bootstrap.inc';
+
 use Drupal\Component\Assertion\Handle;
 use Drupal\Core\Composer\Composer;
-use Drupal\Core\DrupalKernel;
-use Drupal\Core\Site\Settings;
+use Drupal\Core\Extension\ExtensionDiscovery;
 use PHPUnit\Runner\Version;
 
 /**
@@ -21,34 +22,25 @@
  * @return array
  *   An associative array of extension directories found within the scanned
  *   directory, keyed by extension name.
+ *
+ *   TODO Add deprecation notice to $scan_directory parameter.
  */
-function drupal_phpunit_find_extension_directories($scan_directory) {
-  $extensions = [];
-
-  $class_loader = require __DIR__ . '/../../autoload.php';
-  $kernel = new DrupalKernel('prod', $class_loader, FALSE);
-  Settings::initialize($kernel->getAppRoot(), 'sites/default', $class_loader);
-  $ignore_directories = Settings::get('file_scan_ignore_directories', []);
-  $dirs = new RecursiveIteratorIterator(new \RecursiveCallbackFilterIterator(new \RecursiveDirectoryIterator($scan_directory, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS), static function ($file, $key, $iterator) use ($ignore_directories) {
-    foreach ($ignore_directories as $directory) {
-      if (strpos($file->getPathname(), $directory) !== FALSE) {
-        return FALSE;
-      }
-    }
+function drupal_phpunit_find_extension_directories($scan_directory = NULL) {
+  $extension_directories = [];
 
-    return TRUE;
-  }));
-
-  foreach ($dirs as $dir) {
-    if (strpos($dir->getPathname(), '.info.yml') !== FALSE) {
-      // Cut off ".info.yml" from the filename for use as the extension name. We
-      // use getRealPath() so that we can scan extensions represented by
-      // directory aliases.
-      $extensions[substr($dir->getFilename(), 0, -9)] = $dir->getPathInfo()
-        ->getRealPath();
+  // Boot Drupal to be able to use the ExtensionDiscovery.
+  $root = dirname(dirname(__DIR__));
+  $discovery = new ExtensionDiscovery($root, FALSE);
+
+  foreach (['module', 'profile', 'theme'] as $type) {
+    foreach ($discovery->scan($type, TRUE) as $extension) {
+      // $extension->getPathInfo()->getRealpath() returns FALSE for some
+      // reasons.
+      $extension_directories[substr($extension->getFilename(), 0, -9)] = realpath($root . '/' . dirname($extension->getPathname()));
     }
   }
-  return $extensions;
+
+  return $extension_directories;
 }
 
 /**
@@ -59,6 +51,8 @@
  *
  * @return array
  *   An array of directories under which contributed extensions may exist.
+ *
+ *   TODO Add deprecation notice.
  */
 function drupal_phpunit_contrib_extension_directory_roots($root = NULL) {
   if ($root === NULL) {
@@ -151,12 +145,7 @@
   $loader->add('Drupal\\FunctionalJavascriptTests', __DIR__);
 
   if (!isset($GLOBALS['namespaces'])) {
-    // Scan for arbitrary extension namespaces from core and contrib.
-    $extension_roots = drupal_phpunit_contrib_extension_directory_roots();
-
-    $dirs = array_map('drupal_phpunit_find_extension_directories', $extension_roots);
-    $dirs = array_reduce($dirs, 'array_merge', []);
-    $GLOBALS['namespaces'] = drupal_phpunit_get_extension_namespaces($dirs);
+    $GLOBALS['namespaces'] = drupal_phpunit_get_extension_namespaces(drupal_phpunit_find_extension_directories());
   }
   foreach ($GLOBALS['namespaces'] as $prefix => $paths) {
     $loader->addPsr4($prefix, $paths);
only in patch2:
unchanged:
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -10,6 +10,7 @@
 use Drupal\Component\Render\FormattableMarkup;
 use Drupal\Component\Utility\Unicode;
 use Drupal\Core\Config\BootstrapConfigStorageFactory;
+use Drupal\Core\Database\ConnectionNotDefinedException;
 use Drupal\Core\Logger\RfcLogLevel;
 use Drupal\Core\Test\TestDatabase;
 use Drupal\Core\Session\AccountInterface;
@@ -797,7 +798,12 @@ function drupal_get_profile() {
       $profile = \Drupal::installProfile();
     }
     else {
-      $profile = BootstrapConfigStorageFactory::getDatabaseStorage()->read('core.extension')['profile'];
+      try {
+        $profile = BootstrapConfigStorageFactory::getDatabaseStorage()->read('core.extension')['profile'];
+      }
+      catch (ConnectionNotDefinedException $e) {
+        return NULL;
+      }
     }
   }
 
only in patch2:
unchanged:
--- a/core/tests/TestSuites/TestSuiteBase.php
+++ b/core/tests/TestSuites/TestSuiteBase.php
@@ -20,12 +20,11 @@
    *
    * @return string[]
    *   Associative array of extension paths, with extension name as keys.
+   *
+   *   TODO Deprecate $root parameter.
    */
   protected function findExtensionDirectories($root) {
-    $extension_roots = \drupal_phpunit_contrib_extension_directory_roots($root);
-
-    $extension_directories = array_map('drupal_phpunit_find_extension_directories', $extension_roots);
-    return array_reduce($extension_directories, 'array_merge', []);
+    return drupal_phpunit_find_extension_directories();
   }
 
   /**
