Module Development
Topic

Testing in Drupal for Drupal 7, 8, 9, and 10

Drupal ships with a wide variety of unit and functional tests. It is important to understand the main differences between these types of tests, as well as why they are important to improve the overall quality of your application. Drupal also includes a couple of methods for running these test suites.

Example tasks

  • Run the tests included in Drupal core
  • Understand the differences between the different types of tests included in Drupal
  • Write custom tests to minimize bugs in your own application

Confidence

Drupal's use of PHPUnit and testing best practices are well established.

Drupalize.Me resources

Posted on Tuesday, August 3, 2021 - 08:52 by joe

Being able to execute, and step debug, Drupal's test using the PhpStorm UI makes it easier and more efficient to write tests for your projects. Learn how to configure PhpStorm to run Drupal's PHPUnit tests inside of a DDEV environment using docker-compose and a remote PHP CLI Interpreter. This setup will work for both Drupal core and your custom code.

Drupal 7

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate guides

External resources

  • PHPUnit in Drupal 8 (Drupal.org)
    • This is the official documentation on Drupal.org around the PHPUnit testing framework that is included in the core software. It provides a great landing page for learning more about the variety of tests that ship with Drupal core and how to get started writing your own tests.
  • Examples for Developers project (drupal.org)
    • The Examples module contains sub-modules (phpunit_example, simpletest_example) that illustrate how to write various types of tests. In addition most (if not all) of the other example sub-modules that demonstrate additional types of functionality contain tests of their own as well. This is a great source of well-commented code covering a variety of testing cases.
  • Simplytest.me Evaluate Drupal projects online (simplytest.me)
    • The community-provided simplytest.me service allows you to create an environment with any combination of contributed module, theme, or patch and run the test suite against it. It's a really handy way to run core's test suite if you don't have a local installation handy.
  • Learn to test like a grumpy programmer (grumpy-learning.com)
    • This site was created by well-respected PHP developer Chris Hartjes. His e-books contain a lot of higher-level, and practical, advice about why you should be writing tests alongside your code.