PHPUnit in Drupal 8

The testing framework PHPUnit was added to Drupal 8. SimpleTest is supported but is deprecated. It is recommended to write new tests using the PHPUnit base classes UnitTestCase, KernelTestBase, BrowserTestBase (web tests) or JavascriptTestBase (javascript enabled web tests using PhantomJS).

We use multiple versions of PHPUnit. On PHP 5.5, 5.6, 7.0 and 7.1 we use PHPUnit 4.8. On PHP 7.2 we use PHPUnit 6.5. For support on writing tests use PHPUnit's 4.8. documentation to be compatible with the minimum version.

Related documentation about specific requirements for PHPUnit tests

PHPUnit file structure, namespace, and required metadata

File structure and namespace

Running PHPUnit tests

Execute PHPUnit tests from the command line.

Running PHPUnit tests within PhpStorm

Configure PhpStorm to run Drupal's PHPUnit tests.

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 other frequently used PHPUnit mocking framework

PHPUnit Browser test tutorial

This tutorial will take you through the basics of PHPUnit Browser testing in Drupal 8.

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.

Guide maintainers