How to run PHPUnit tests with Drupal 10?

Created on 14 October 2022, over 2 years ago
Updated 2 June 2024, 10 months ago

Problem/Motivation

I want to require phpunit/phpunit in a compatible version and run tests on command line.

Steps to reproduce

Require PHPUnit (composer require phpunit/phpunit). Here you will currently get PHPUnit in version 9.5.25. Then run the PHPUnit tests from the "core" folder, since there is the "phpunit.xml.dist" file located:
../../vendor/bin/phpunit
Then you will see such errors:

majmesku@pbt-d10-web:/var/www/html/web/core$ ../../vendor/bin/phpunit
PHP Warning: Class "\Drupal\Tests\DocumentElement" not found in /var/www/html/web/core/tests/bootstrap.php on line 157

Warning: Class "\Drupal\Tests\DocumentElement" not found in /var/www/html/web/core/tests/bootstrap.php on line 157
PHPUnit 9.5.25 #StandWithUkraine

Error in bootstrap script: Error:
Class "Behat\Mink\Element\TraversableElement" not found
#0 /var/www/html/vendor/composer/ClassLoader.php(571): include()
#1 /var/www/html/vendor/composer/ClassLoader.php(428): Composer\Autoload\includeFile('/var/www/html/w...')
#2 [internal function]: Composer\Autoload\ClassLoader->loadClass('Drupal\\Tests\\Do...')
#3 /var/www/html/web/core/tests/bootstrap.php(157): class_alias('\\Drupal\\Tests\\D...', '\\Behat\\Mink\\Ele...', true)
#4 /var/www/html/vendor/phpunit/phpunit/src/Util/FileLoader.php(66): include_once('/var/www/html/w...')
#5 /var/www/html/vendor/phpunit/phpunit/src/Util/FileLoader.php(49): PHPUnit\Util\FileLoader::load('/var/www/html/w...')
#6 /var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php(565): PHPUnit\Util\FileLoader::checkAndLoad('/var/www/html/w...')
#7 /var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php(345): PHPUnit\TextUI\Command->handleBootstrap('/var/www/html/w...')
#8 /var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php(112): PHPUnit\TextUI\Command->handleArguments(Array)
#9 /var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php(97): PHPUnit\TextUI\Command->run(Array, true)
#10 /var/www/html/vendor/phpunit/phpunit/phpunit(98): PHPUnit\TextUI\Command::main()
#11 /var/www/html/vendor/bin/phpunit(123): include('/var/www/html/v...')
#12 {main}

Proposed resolution

Please let me know how to execute PHPUnit tests with Drupal 10 or fix Drupal, so it will work with PHPUnit. Are there any special configs needed? How the PHPUnit is installed and run by the Drupal test bot? It would be appreciated, it you could point me here to the right direction.

๐Ÿ’ฌ Support request
Status

Fixed

Version

10.0 โœจ

Component
PHPUnitย  โ†’

Last updated about 2 hours ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany Peter Majmesku ๐Ÿ‡ฉ๐Ÿ‡ชDรผsseldorf

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024