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 composercomposer 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 commandvendor/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
Comments