Warning message

Documentation is currently being migrated into the new system. Some pages might be temporarily missing, and some guides might appear empty. Thank you for your patience while we are improving Drupal.org documentation.

PHPUnit in Drupal 8

The testing framework PHPUnit was added to Drupal 8 in June of 2013. SimpleTest is still supported but is deprecated. Any new tests should be written using the PHPUnit based classes UnitTestCase, KernelTestBase, BrowserTestBase (web tests) or JavascriptTestBase (javascript enabled web tests using PhantomJS).

This guide contains documentation specific to Drupal's implementation of PHPUnit. See PHPUnit's documentation for support on writing tests.

Some PHPUnit tests may require specific extensions and if missing those tests will be skipped.

General PHPUnit Resources

Drupal documentation

PHPUnit file structure, namespace, and required metadata

File structure and namespace

Running PHPUnit tests

PHPUnit tests are not well-integrated with the SimpleTest administrative interface and should be run from the command line.

Running PHPUnit tests within PhpStorm

Understanding PHPunit expectations

A mock object most of the time will contain an expectation like

Unit testing more complicated Drupal classes

Dealing with function calls

Using Prophecy

Mocking with Prophecy in PHPUnit

Comparison with PHPUnit mocks

Now that we saw how you can use prophecy as a start, let's compare it with the often other used mocking framework which comes with phpunit

PHPUnit Browser test tutorial

This tutorial will take you through the basics of PHPUnit Browser testing in Drupal 8. By the end you should be able to write your first

Agile Unit Testing

What You Should Know

PHPUnit Javascript testing tutorial

Learn about PHPUnit Javascript testing in Drupal 8. By the end you should be able to write your first test.