I have added some phpunit tests to my custom module. When trying to run them in I get this error: Class '\Drupal\Tests\DocumentElement' not found I have copied core/phpunit.xml.dist to core/phpunit.xml and edited the URL and DB values but nothing else.

The command I'm running is vendor/bin/phpunit -c web/core/phpunit.xml web/modules/contrib/my_module/.

It seems that the classloader is not working. What have I missed?

I'm using drupal 9 and phpunit 9. I've also tried with phpunit 7 and 8 but I get the same error.

2 Answers 2


Finally got to the bottom of this! The package drupal/core-dev is required.

composer require --dev drupal/core-dev

This installs the package as a development dependency.

  • Simple, but if you forgot about this as I did just now, can be frustrating! Commented Jul 15, 2022 at 9:52
  • composer require drupal/core-dev Commented Apr 5, 2023 at 14:25

Drupal forgot to add few dependency libraries.

See you have these 2 libraries.

composer show symfony/phpunit-bridge 
composer show phpspec/prophecy-phpunit

If you don't please install.

I also noticed that following library also essential.

  • Those are dependencies for drupal/core-dev. If you need them, you need drupal/core-dev.
    – apaderno
    Commented May 9, 2023 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.