How to run PHPUnit test cases on your local machine for Drupal

How to run PHPUnit test cases on your local machine for Drupal

On27th Aug 2023, 2024-12-13T17:24:47+05:30 ByKarthik Kumar D K | read
Listen Pause Resume Stop

Running PHPUnit tests helps ensure that your Drupal module behaves as expected and provides a reliable way to identify and fix issues during development.

To run PHPUnit test cases on your local machine for Drupal, follow these steps:

Install PHPUnit and its dependency:

  • Make sure you have PHP and Composer installed on your local machine. If you don't have PHPUnit installed, you can install it using Composer by running the following command:

composer require drupal/core-dev --dev --update-with-all-dependencies

Set up a Drupal Environment & Configure:

  • Install Drupal on your local machine. You can use the Drupal project template or any other method of your choice. Ensure you have a working Drupal site with the module you want to test installed and enabled (In my case using the composer & ddev for the setup).
  • To configure PHPUnit, you need to copy the phpunit.xml.dist file to phpunit.xml and make below changes in this file.

<env name="SIMPLETEST_BASE_URL" value="https://drupal.ddev.site"/>

<env name="SIMPLETEST_DB" value="mysql://db:db@db/db"/>

<env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/var/www/html/web/sites/simpletest/browser_output"/>

  • Next, create a directory to store the output and change the folder permissions to 777, below shown the commands.

mkdir -p sites/simpletest/browser_output

chmod -R 777 sites/simpletest

Run PHPUnit Tests:

  • Get into the container by running the command ddev ssh.
  • Now, navigate to the root of your Drupal site and execute the following command to run This command runs the PHPUnit tests for your Drupal module. If everything is set up correctly and your tests pass, you will see the test results displayed in the console.

cd web

../vendor/bin/phpunit -c core core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php

  • Below are a few example commands to run the test cases for core & contrib code.

../vendor/bin/phpunit -c core core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php

../vendor/bin/phpunit -c core core/modules/help/tests/src/Functional/HelpPageReverseOrderTest.php

../vendor/bin/phpunit -c core core/modules/action

../vendor/bin/phpunit -c core modules/contrib/admin_toolbar/tests/src/Functional/AdminToolbarAdminMenuTest.php

../vendor/bin/phpunit -c core modules/contrib/admin_toolbar

  • Below is the example output for one such test case.

karthik.dk@drupal-web:/var/www/html/web$ ../vendor/bin/phpunit -c core core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php

PHPUnit 9.6.10 by Sebastian Bergmann and contributors.

Testing Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest

......................                                            22 / 22 (100%)

Time: 00:01.852, Memory: 8.00 MB

OK (22 tests, 62 assertions)

Keep in mind that PHPUnit tests for Drupal modules are organized in specific directories and namespaces within your module's tests folder. Also, ensure that your module adheres to Drupal's coding standards for test classes and methods.

Thanks for reading the article, for more drupal related articles read and subscribe to peoples blog articles.

Related Articles

Recent Articles

Recent Quick Read

Recent Great People

We Need Your Consent
By clicking “Accept Cookies”, you agree to the storing of cookies on your device to enhance your site navigation experience.
I Accept Cookies