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.

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.