17 Dramatis personae patterns for clear naming of concepts
This chapter explains how BDD scenarios benefit from clear, descriptive naming for the business concepts they rely on. Drawing on the idea of dramatis personae, it argues that personas can represent users, data sets, configurations, messages, or other domain concepts that need to be referred to unambiguously in scenarios and automation. The central goal is to keep specifications readable for humans while still giving test code enough structure to create and identify the right objects and data.
The chapter then shows how small persona definitions can live directly in a feature file, either in a scenario or in a supporting block, so that the important data stays close to the behavior being described. This approach works well when the persona is simple, local to one feature, and useful for manual review, but it should be avoided when the data is large or would overwhelm the scenario. It also introduces the idea of naming user-facing messages so scenarios can assert meaning rather than brittle literal text, improving resilience when message wording changes or when localization is involved.
For larger or shared data sets, the chapter recommends moving personas outside the feature file and loading them from external sources such as CSV, XML, or JSON. It also shows how a templated approach can reduce duplication when many similar persona definitions differ only in a few details, letting domain experts curate reusable base data while scenarios specify just the variations they need. Across all these patterns, the emphasis is on preserving locality for essential facts, keeping scenarios understandable, and choosing the simplest data organization that still supports maintainable, expressive specifications.
Storing a named persona in a context object
Extending the custom parameter type to find the order number of a named order
Assert that correct localized message is generated
Setting the context using a sales traffic external persona
Summary
- It is often useful to give descriptive names to business concepts, data structures, or groups of entities. Every time we introduce a descriptive name, we are creating a persona.
- Every persona has some unique aspect, making it useful for illustrating some part of the system’s expected behavior. Those unique aspects are specified in a persona definition.
- Persona definitions range from a very simple naming of a single entity in one scenario all the way up to a complicated template containing a hierarchy of entities, shared among many feature files.
- The In-place Persona pattern describes how simple persona definitions can be created as part of the feature file.
- In-place personas can be created either in a feature file’s Background block (in which case they can be used by any scenario in the feature file) or in the specific Scenario that they are relevant to.
- Almost every system provides some sort of text intended for users, such as validation messages, labels, prompts, and notifications—and that often needs to be localizable. The Message Persona pattern extends the In-place Persona pattern to help us write meaningful scenarios that handle human-readable text, without needing to provide the actual text.
- Some persona definitions are too large to include in a feature file without harming readability. The External Persona pattern describes how a scenario can make use of a persona definition that is stored in an external file or repository.
- External persona definitions can be used by any feature file that needs them.
- Complicated persona definitions are usually created by domain experts to illustrate expected behavior. Therefore, many external persona definitions will express the expected results alongside the definition.
- When multiple scenarios need to use similar persona definitions, the Templated Persona pattern can be useful to minimize the maintenance cost of creating a different persona definition for each scenario.
- The Templated Persona pattern covers situations where a single persona definition is used as a template to be customized for various scenarios. This can be implemented using a templating library but can also be handled by hand-crafted step definitions.
- External persona definitions are part of the system’s specification and so should be kept alongside, and versioned with, the feature files.
Effective Behavior-Driven Development ebook for free