Running A PHPUnit Test

There are different ways in which we can run our PHPUnit tests. We can either run a full unit test, a test suite or one specific test as shown in following commands:

To run all the tests available from the custom modules directory:

bin/phpunit -c core/phpunit.xml.dist modules/custom

Here, phpunit.xml.dist is the configuration file for PHPUnit. This holds various attributes like bootstrap, color, etc, which helps PHPUnit to perform tests.

To run all tests (Unit, Kernel, Functional) from a particular module:

bin/phpunit -c core/phpunit.xml.dist modules/custom/phpunit_example

To run only all unit tests from a specific module:

bin/phpunit -c core/phpunit.xml.dist modules/custom/phpunit_example/tests/src/Unit

To run a specific unit test:

bin/phpunit -c core/phpunit.xml.dist modules/custom/phpunit_example/tests/src/Unit/UnitTest.php

To run a specific group test:

bin/phpunit -c core/phpunit.xml.dist modules/custom --group phpunit_example

----------------------------------

For docker 4 drupal this setup is needed to get your test running with PHPUnit

1.Inside your drupal/core directory, copy the file phpunit.xml.dist and rename it to phpunit.xml (then also change the commands above to reflect this change)
2. Open that file and make sure that you update SIMPLETEST_BASE_URL to http://nginx
3. In order to make sure that your DB connection is working as well, update SIMPLETEST_DB to mysql://drupal:drupal@mariadb/drupal
4. Log into your docker with docker-compose exec php bash  and rung commands above depending on tests you want to run