PHPUnit for Drupal: A very basic how-to.
How to use this example module
You really should be reading the various docblocks in the test files.
PHPUnit tests belong in their own directory, so they won't be loaded by the autoloader during normal bootstrap. This means you should have a
/tests/srcdirectory in the root of your module directory.
Your tests should be in the
Drupal\Tests\[your_module]\Unitnamespace. Under Drupal's PSR-4 system, this means your PHPUnit-based tests should go in a
Your test case should subclass
You can run PHPUnit-based tests from within Drupal 8 by enabling the Testing module and then selecting the PHPUnit group from the testing page. As of this writing, this method doesn't provide any useful output.
Standard PHPUnit Practices
You can (and really, should) run PHPUnit from the command line.
On unix-based systems this means you need to
cd core and then
On Windows-based systems, assuming you have php in your path,
php ./vendor/phpunit/phpunit/composer/bin/phpunit --group phpunit_example
Also, you should mark your tests as belonging to a group, so they can be run
independently. You do this by annotating your test classes with
@group group_name. You should have a
your module name, and you should also have a
integrations, such as
So, for instance, to run all of the PHPUnit example tests, you would type
./vendor/bin/phpunit --group phpunit_example
As you can see, including a
@group annotation is a good idea.