Exploring Drupal Test Traits

I've given talks and workshops on automated testing in Drupal.

I created a free 10-day email course.

Something I haven't used until recently is Drupal Test Traits.

The built-in PHPUnit tests create a new installation of Drupal for each test, setting up everything from scratch and installing the specified modules and configuration for each test.

The tests don't know about the site they're running on, which is what allows them to run in Drupal CI or on any Developer's computer.

Each test is responsible for creating its own environment.

This is part of the "arrange" step of writing an automated test - creating the situation for the test to run, such as installing configuration and creating content, before performing actions and making assertions.

But, what if you want to test an existing website?

Drupal Test Traits provides an ExistingSiteBase class and a number of traits that allow for testing an existing Drupal website and simplifies the setup process.

You can see this in my PresentationTest class - there's no setup like recreating content type and field configuration as it knows about the "real" site I'm working on.

Some tests like my PresentationCounterTest I've had to write differently to count for the existing content, but still work.

I'm going to continue trying out Drupal Test Traits and, if it continues to go well, include it in my future talks and workshops.