uuid: - value: 20b8e2be-b5ec-404d-853e-55cf19327558 langcode: - value: en type: - target_id: daily_email target_type: node_type target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7 revision_timestamp: - value: '2025-06-21T12:44:54+00:00' revision_uid: - target_type: user target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849 revision_log: { } status: - value: true uid: - target_type: user target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849 title: - value: 'Exploring Drupal Test Traits' created: - value: '2025-06-18T12:36:14+00:00' changed: - value: '2025-06-21T12:44:54+00:00' promote: - value: false sticky: - value: false default_langcode: - value: true revision_translation_affected: - value: true path: - alias: /daily/2025/06/18/exploring-drupal-test-traits langcode: en body: - value: |- I've [given talks and workshops][3] on automated testing in Drupal. I created [a free 10-day email course][2]. Something I haven't used until recently is [Drupal Test Traits][4]. 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][0] - 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][1] 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. [0]: https://code.oliverdavies.uk/opdavies/oliverdavies.uk/src/commit/b8c3f6d9a761f126616f0be12d3c35dffef41bbe/modules/opd_presentations/tests/src/Functional/PresentationTest.php [1]: https://code.oliverdavies.uk/opdavies/oliverdavies.uk/src/commit/b8c3f6d9a761f126616f0be12d3c35dffef41bbe/modules/opd_presentations/tests/src/Functional/PresentationCounterTest.php [2]: /atdc [3]: /presentations/tdd-test-driven-drupal [4]: https://www.drupal.org/project/dtt format: markdown processed: |
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.
summary: '' field_daily_email_cta: - target_type: node target_uuid: e3f6c728-7855-4804-8614-e2a0c08c368f