Writing Automated Tests in Drupal

Automated tests are an essential part of ensuring the code we write is reliable, that future updates don’t cause regressions, and that our applications remain stable.

This course covers the creation and configuration of PHPUnit for testing, how to implement unit, kernel, and functional tests, and writing tests that run in a simulated browser.. Additionally, learners will explore JavaScript testing using tools like Nightwatch.js, and understand how to manage test dependencies effectively. By the end of the course, you will be able to write automated tests that ensure your Drupal code is reliable, maintainable, and free of regressions.

Key topics

  • Customizing PHPUnit configuration for your Drupal project
  • Write and run functional tests, kernel tests, and unit tests
  • Explicitly declare and manage a test's dependencies (e.g. what modules are enabled?)
  • Functional JavaScript tests and related tools like WebDriver and Nightwatch.js
  • Converting old Simpletest tests to PHPUnit
  • Best practices for organizing test files within your project
  • Techniques for debugging failing tests, including using tools like PhpStorm, DDEV, and xDebug
Tutorials in this course
Categories
Drupal 8, 9, 10, and 11
More information

In this tutorial, we'll walk through the basics of how to implement a functional test by extending Drupal's BrowserTestBase class. We'll assume you've already determined that you need to write a functional test and that you've Set up a Functional Test.

In this tutorial, we'll:

  • Determine the specifications of the test.
  • Walk through the behavior we want to test.
  • Document our test in the test class.
  • Implement the testing steps.
  • Decide how to deal with dependencies (for now). (We'll go into details about handling test dependencies in Implement Drupal Functional Test Dependencies.)
More information

Previously, in Implement a Functional Test, we learned how to tell BrowserTestBase to use the Standard installation profile in order to get our test passing, letting the Standard profile implicitly provide our dependencies. We mentioned that doing so probably wasn't the best decision and that we should explicitly declare those dependencies instead.

In this tutorial, we'll walk through how to explicitly declare our test's dependencies. When in doubt, it's generally considered a best practice to be as explicit about the dependencies of our tests as possible. By the end of this tutorial, you should be able to:

  • Understand why we want to explicitly declare our dependencies.
  • Determine what the dependencies really are and make a list of them.
  • Implement each dependency in our list.
  • Emerge with a thorough passing test.
Categories
Drupal 8, 9, 10, and 11
More information

Let's write somewhat strict unit tests in a Drupal module. By the end of this tutorial, you will be able to:

  • Understand what makes a unit test different from other types of tests.
  • Determine the specifications of a unit test.
  • Use mocking to isolate units under test, and to force code flow to achieve high coverage.

We'll start out with a brief introduction to unit tests. Then we'll look at a contrived example of a Drupal controller class for illustration purposes. Next, we'll test two units of this controller class, each requiring different mock styles.

More information

Simpletest has been removed from Drupal 9. If you're preparing to upgrade your site to the latest version of Drupal and you have Simpletests in your codebase that extend WebTestBase you'll need to update them to use PHPUnit's BrowserTestBase class instead. This will ensure your tests don't depend on a deprecated testing framework. By the end of this tutorial, you should be able to convert WebTestBase-based tests to use PHPUnit's BrowserTestBase class instead.

This course appears in the following guides:
Module Development
Learn how to run and write unit, kernel, and functional tests for your custom Drupal code.

Test Custom Code