diff --git a/core/modules/simpletest/src/TestDiscovery.php b/core/modules/simpletest/src/TestDiscovery.php
index 1b512b55bb8da2324eb55c78ecd19a186f1ef59c..2c86907a5984d45649fd524e976aba6102ade1fd 100644
--- a/core/modules/simpletest/src/TestDiscovery.php
+++ b/core/modules/simpletest/src/TestDiscovery.php
@@ -207,7 +207,9 @@ public function findAllClassFiles($extension = NULL) {
     $classmap = array();
     $namespaces = $this->registerTestNamespaces();
     if (isset($extension)) {
-      $namespaces = array_intersect_key($namespaces, array_flip(preg_grep('/' . preg_quote("Drupal\\$extension\\", '/') . '/', array_keys($namespaces))));
+      // Include tests in the \Drupal\Tests\{$extension} namespace.
+      $pattern = "/Drupal\\\(Tests\\\)?$extension\\\/";
+      $namespaces = array_intersect_key($namespaces, array_flip(preg_grep($pattern, array_keys($namespaces))));
     }
     foreach ($namespaces as $namespace => $paths) {
       foreach ($paths as $path) {