workshop-drupal-automated-testing
workshop-drupal-automated-testing copied to clipboard
First test throwing errors for PHPUnit 9
I'm following along using ddev. At 13.6 I got the following error:
# ddev exec ../vendor/bin/phpunit ./modules/custom/my_module
PHP Fatal error: Uncaught Error: Class 'Drupal\Tests\BrowserTestBase' not found in /var/www/html/web/modules/custom/my_module/tests/src/Functional/FrontPageTest.php:8
This is fixed by specifying -c ./core as part of the command.
# ddev exec ../vendor/bin/phpunit -c ./core ./modules/custom/my_module
Although this then shows a dependency error:
Drupal requires Prophecy PhpUnit when using PHPUnit 9 or greater. Please use 'composer require --dev phpspec/prophecy-phpunit:^2' to ensure that it is present.
Executing the command allows the test to start, but then there's a new error:
1) Drupal\Tests\my_module\Functional\FrontPageTest::the_front_page_loads_for_anonymous_users
Exception: Drupal\Tests\BrowserTestBase::$defaultTheme is required. See https://www.drupal.org/node/3083055, which includes recommendations on which theme to use.
This requires the addition of a line in the test class
class FrontPageTest extends BrowserTestBase {
...
protected $defaultTheme = 'bartik';
...
}
With this done the test passes as expected:
# ddev exec ../vendor/bin/phpunit -c ./core ./modules/custom/my_module
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
Warning: Your XML configuration validates against a deprecated schema.
Suggestion: Migrate your XML configuration using "--migrate-configuration"!
Testing /var/www/html/web/modules/custom/my_module
. 1 / 1 (100%)
Time: 00:08.580, Memory: 6.00 MB
OK (1 test, 3 assertions)