diff --git a/core/modules/simpletest/src/TestDiscovery.php b/core/modules/simpletest/src/TestDiscovery.php
index 132694607c..c92cdfe488 100644
--- a/core/modules/simpletest/src/TestDiscovery.php
+++ b/core/modules/simpletest/src/TestDiscovery.php
@@ -164,6 +164,11 @@ public function getTestClasses($extension = NULL, array $types = []) {
 
     $classmap = $this->findAllClassFiles($extension);
 
+    // Leave only the test classes.
+    $classmap = array_filter($classmap, function ($key) {
+      return substr($key, -4) === 'Test';
+    }, ARRAY_FILTER_USE_KEY);
+
     // Prevent expensive class loader lookups for each reflected test class by
     // registering the complete classmap of test classes to the class loader.
     // This also ensures that test classes are loaded from the discovered
diff --git a/core/modules/system/src/Tests/Entity/DoNotUseMeDeprecatedTestTrait.php b/core/modules/system/src/Tests/Entity/DoNotUseMeDeprecatedTestTrait.php
new file mode 100644
index 0000000000..89fcaacf51
--- /dev/null
+++ b/core/modules/system/src/Tests/Entity/DoNotUseMeDeprecatedTestTrait.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace Drupal\system\Tests\Entity;
+
+@trigger_error('Do not use me, thanks!', E_USER_DEPRECATED);
+
+/**
+ * Provides deprecated test trait for check unexpected deprecated error.
+ *
+ * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0.
+ */
+trait DoNotUseMeDeprecatedTestTrait {
+
+}
