{ "uuid": [ { "value": "45636e82-61b1-4298-a28c-34f139b9a559" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-05-14T01:19:06+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": "Writing your own test traits" } ], "created": [ { "value": "2025-05-13T01:08:34+00:00" } ], "changed": [ { "value": "2025-05-14T01:19:06+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2025\/05\/13\/writing-your-own-test-traits", "langcode": "en" } ], "body": [ { "value": "
In Drupal, there are different types of automated tests we can write.<\/p>
The most common I use are Functional, Kernel and Unit - which I explain about in my Test Driven Drupal talk<\/a>.<\/p> If I'm writing multiple of the same type, I'll often create my own base test class that extends This allows me to write custom helper functions and share behaviour between the tests.<\/p> An example is the But, what if you want to do this for different types of test?<\/p> Enter, traits.<\/p> Traits are a way of reusing code without inheritance - meaning without extending a base class.<\/p> I can use a trait in my functional and kernel tests whilst both extend their required base classes.<\/p> I did this with my To see this, you can look at the code<\/a> on my Forgejo instance.<\/p> P.S. If you want to learn how to write automated tests in Drupal, subscribe to my free 10-day email course<\/a>.<\/p>",
"format": "basic_html",
"processed": " In Drupal, there are different types of automated tests we can write.<\/p> The most common I use are Functional, Kernel and Unit - which I explain about in my Test Driven Drupal talk<\/a>.<\/p> If I'm writing multiple of the same type, I'll often create my own base test class that extends This allows me to write custom helper functions and share behaviour between the tests.<\/p> An example is the But, what if you want to do this for different types of test?<\/p> Enter, traits.<\/p> Traits are a way of reusing code without inheritance - meaning without extending a base class.<\/p> I can use a trait in my functional and kernel tests whilst both extend their required base classes.<\/p> I did this with my To see this, you can look at the code<\/a> on my Forgejo instance.<\/p> P.S. If you want to learn how to write automated tests in Drupal, subscribe to my free 10-day email course<\/a>.<\/p>",
"summary": ""
}
],
"feeds_item": []
}BrowserTestCase<\/code>,
KernelTestCase<\/code> or whatever base class I need rather than extending it directly.<\/p>
createDailyEmailNode<\/code> method I wrote to simplify creating daily emails in my tests since migrating to Tome<\/a>.<\/p>
createDailyEmailNode<\/code> method so I could re-use it in both types of tests when counting the number of sent daily emails.<\/p>
BrowserTestCase<\/code>,
KernelTestCase<\/code> or whatever base class I need rather than extending it directly.<\/p>
createDailyEmailNode<\/code> method I wrote to simplify creating daily emails in my tests since migrating to Tome<\/a>.<\/p>
createDailyEmailNode<\/code> method so I could re-use it in both types of tests when counting the number of sent daily emails.<\/p>