Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 295104

TestFileParser should fail if classes can't be loaded

$
0
0

Problem/Motivation

Total noob here, so this might be a terrible idea, but here goes. I spent quite a bit of time trying to figure out why my unit tests weren't working when running with run-tests.php. I kept getting the following message:

ERROR: No valid tests were specified.

More details of my attempt at finding the problem here: https://drupal.stackexchange.com/questions/296686/run-tests-sh-error-tes...

As a new user, I immediately read that message as meaning "Everything is working fine, but there are no tests to run". This led me to spend quite a bit of time trying to figure out whether my class was properly inherited from one of the valid base classes, whether my methods were named correctly, etc.

The problem actually turned out to be that the class wasn't loadable at all, and is_subclass_of in TestFileParser.php happily returned "FALSE" even though it _was_ actually inherited from one of the correct base classes (it just couldn't load the class). The behaviour of is_subclass_of aside, I think the above error message fails the principle of least astonishment.

I'm not a php developer, nor do I have any experience with Drupal, but the below patch did make it much easier for me to diagnose the problem, so I wanted to share it.

Steps to reproduce

Rename your "Unit" directory in your tests to "unit" and try to run the test with run-tests.sh --file

Proposed resolution

--- web/core/lib/Drupal/Core/Test/RunTests/TestFileParser.php 2020-09-03 23:20:59.000000000 +0000
+++ web/core/lib/Drupal/Core/Test/RunTests/TestFileParser_new.php 2020-09-13 04:40:26.000000000 +0000
@@ -24,6 +24,9 @@
   public function getTestListFromFile($file) {
     $test_list = $this->parseContents(file_get_contents($file));
     return array_filter($test_list, function ($class) {
+      if (!class_exists($class)) {
+        throw new \Exception("class {$class} could not be loaded");
+      }
       return (is_subclass_of($class, TestCase::class) || is_subclass_of($class, TestBase::class));
     });
   }

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet


Viewing all articles
Browse latest Browse all 295104

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>