Chapter 11: Testing

This chapter is part of the Drupal Module Developer Guide.

In this chapter we’ll learn how to configure your local environment to run Drupal’s PHPUnit-based tests. We’ll define the different types of tests (unit, kernel, and functional) that a Drupal module might need. And work through examples of writing tests of each type in order to provide coverage for the features of our 2 custom modules.

Tutorials in this course
Categories
Drupal 8, 9, and 10
More information

Testing ensures that code remains reliable and functional. This tutorial introduces the primary types of tests in Drupal: Unit, Kernel, Functional, and FunctionalJavascript -- all executed via PHPUnit. We'll clarify the differences between each type of test and appropriate use cases. As module developers, understanding what to test and how to write tests is vital for robust and maintainable code.

In this tutorial, we'll:

  • Identify the primary test types in Drupal and their use cases.
  • Emphasize the importance of functional tests in custom module development.
  • Introduce the basics of authoring tests in a custom module.

By the end of this tutorial, you should recognize the different types of tests Drupal uses and when and how to use each kind.

More information

Before you can run tests, you'll need to configure your local environment. This setup involves Drupal-specific configuration for PHPUnit and ensuring your environment supports Functional JavaScript tests with a WebDriver client and a compatible browser. The setup process varies based on the development environment. In this tutorial, we're using DDEV as the local environment.

In this tutorial, we'll:

  • Install all required dependencies.
  • Configure PHPUnit specific to our environment.
  • Validate the setup by running a Drupal core test.

By the end of this tutorial, you'll be equipped to run Drupal's PHPUnit tests locally using DDEV.

Categories
Drupal 8, 9, and 10
More information

Unit tests are the simplest among Drupal's test types, ideal for verifying code that performs computations. This tutorial guides through writing unit tests for the anytown module, focusing on the ForecastClient service, and illustrates how to use mocks for dependencies.

In this tutorial, we'll:

  • List potential unit tests for the anytown module.
  • Write tests for ForecastClient service logic.
  • Demonstrate how to mock services in unit tests.

By the end of this tutorial you should be able to write PHPUnit Unit tests for logic in the anytown module.

Categories
Drupal 8, 9, and 10
More information

Kernel tests in Drupal enable module integration testing with Drupal core systems in a bootstrapped environment. Kernel tests bridge the gap between unit and functional tests. This tutorial focuses on writing kernel tests for the anytown module, specifically to test the ForecastClient service's caching logic and custom username validation.

In this tutorial, we'll:

  • Explore the parts of a kernel test.
  • Write kernel tests for anytown module features.
  • Use mocks and the Drupal container in kernel tests.

By the end of this tutorial, you should be able to get started writing kernel tests to verify your module's integration with Drupal core.

Categories
Drupal 8, 9, and 10
More information

Functional tests simulate user interactions with Drupal applications, which enables us to test user interfaces and complex workflows. This tutorial guides you through writing functional tests for the anytown module, focusing on custom user registration workflow enhancements.

In this tutorial, we'll:

  • Examine functional test structure.
  • Test anytown_form_user_register_form_alter() customizations.
  • Discuss the functional test execution environment.

By the end of this tutorial, you'll know how to write functional tests that emulate browser interactions with your Drupal application.

This course appears in the following guides:
Module Development
Learn Drupal module development in this hands-on guide.

Drupal Module Developer Guide