Drupal 8 uses PHPUnit as the testing framework for all types of tests. In this section, we will see how we can work with it to run tests.
Although Drupal has a UI for running tests, PHPUnit is not well integrated with this. So, it's recommended that we run them using the command line instead. Actually, it's very easy to do so. To run the entire test suite (of a certain type), we have to navigate to the Drupal core folder:
cd core
And run the following command:
../vendor/bin/phpunit —testsuite=unit
This command goes back a folder through the vendor directory and uses the installed phpunit executable. As an option, in the previous example, we specified that we only want to run unit tests. Omitting that would run all types of tests. However, for most of the others, there will be some configuration needed, as we will see in the respective sections.
If we want to run a specific test, we can pass it as an argument to the phpunit command (the path to the file):
../vendor/bin/phpunit tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
In this example, we run a Drupal core test that tests the UrlGenerator class.
Alternatively, we can run multiple tests that belong to the same group (we will see how tests are added to a group soon):
../vendor/bin/phpunit —group=Routing
This runs all the tests from the Routing group which actually contains the UrlGeneratorTest we saw earlier. We can run tests from multiple groups if we separate them by a comma.
Also, to check what the available groups are, we can run the following command:
../vendor/bin/phpunit —list-groups
This will list all the groups that have been registered with PHPUnit.
Finally, we can also run a specific method found inside a test by using the —filter argument:
../vendor/bin/phpunit —filter=testAliasGenerationUsingInterfaceConstants
This is one of the test methods from the same UrlGeneratorTest we saw before and is the only one that would run.