Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Unit test with Drupal using PHPUnit (Drupal 10)

By editor , 2 February, 2024
Question

How to test a module using Drupal's unit test framework (like the old simpletest module) on Drupal 9 or above ?

Drupal testing framework (PHPUnit) provide 3 types of tests: UnitTest, KernelTest, FunctionalTest (JSFunctionalTest)
UnitTest : Very fast but Unit Only, without drupal.
KernelTest : Is a service test with drupal but without interfaces ans theming.
FunctionalTest (Also JSFunctionalTest) : Like a real browser, but very slow, may take morethan 10 seconds for each test.

1. Install PHPUnit framework.

Note : This example is tested with a drupal recommended-project, installed via composer create-project drupal/recommended-project
Install the unit test packages via composer
composer require --dev drupal/core-dev

Once installed you can check the version using:
vendor/bin/phpunit --version

Complete tutorial for package installation : https://www.drupal.org/docs/develop/using-composer/starting-a-site-using-drupal-composer-project-templates#s-adding-drupalcore-dev

2. Configure PHPUnit for drupal.

Drupal core provides an example at core/phpunit.xml.dist. Make a copy of this file with a name phpunit.xml and place it in to the project root directory (for example).

Drupal bootstrap configuration.
Change the attribute 'bootstrap' with the correct path from your xml frile.
Example (in my case) : bootstrap="web/core/tests/bootstrap.php"

To use Kernel and Functional test, you must configure a database and site URL.

Database (SIMPLETEST_DB)

Edit the database variable (<env name="SIMPLETEST_DB" value=""/>)
You can use a real database (like mysql) or a tempory database on memory like sqlite database, which is faster than mysql.
Example for Sqlite on memory : <env name="SIMPLETEST_DB" value="sqlite://localhost/:memory:"/>
Example for Sqlite on file : <env name="SIMPLETEST_DB" value="sqlite://localhost/sites/default/files/db.sqlite"/>

Site Url

Edit the site url variable (<env name="SIMPLETEST_BASE_URL" value=""/>)
Fill this field with your local site url.
Example :     <env name="SIMPLETEST_BASE_URL" value="http://drupal"/>

You can also set the BROWSERTEST_OUTPUT_DIRECTORY
Example : <env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/var/www/html/simpletest"/>

Test files and directories.

Change the testsuites and coverage directories to fit to your project (See the example fiel).

Note : You can create your testsuites to point to your mosues or to exclude directories.
Example:

   <testsuite name="mytests">
     <file>web/core/tests/TestSuites/UnitTestSuite.php</file>
     <directory>web/modules/testing</directory>
     <directory>web/modules/custom/mymodule/tests/src/Kernel/</directory>
     <!-- Exclude Functional tests -->
     <exclude>web/modules/testing/mymodule/tests/src/Functional/</exclude>
   </testsuite>
   
   
Complete tutorial : https://www.drupal.org/node/2116263


3. Run

Now you can run unit tests using one of following command
vendor/bin/phpunit --testsuite unit
vendor/bin/phpunit --testsuite kernel
vendor/bin/phpunit --testsuite functional
vendor/bin/phpunit --testsuite functional-javascript
vendor/bin/phpunit --testsuite build

 

File
phpunit.zip (2.15 KB)

Tags

  • Drupal 8
  • Testing
  • Drupal 9
  • Drupal 10
SimpleTest : Running PHPUnit tests from command line
SimpleTest With Drupal 8

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer