15 Mise-en-scène patterns for layering the automation codebase
This chapter explains how to structure BDD automation so it stays clear, reusable, and adaptable as the codebase grows. It starts from the practical problem that scenario automation often becomes tangled with duplication, awkward step definitions, and hidden infrastructure details, especially when tests run through outer interfaces like a UI or REST API. The solution is to separate responsibilities into layers so the step definitions remain focused on testing intent, while lower layers handle how the application is actually driven and how technical plumbing is managed.
The first major idea is to keep step definitions thin by introducing test drivers that encapsulate automation details such as invoking endpoints, handling requests, and dealing with application hosting. For outer-interface automation, the application must be running in a dedicated host, ideally self-hosted for isolation, and the automation code may need shared context to reach it. The chapter then refines this structure further with an infrastructure layer that centralizes recurring technical concerns like HTTP handling, authentication headers, status-code checks, and JSON parsing. On top of that, the test action pattern wraps cross-cutting concerns such as logging, timing, and execution control, while the attempt action variant supports negative scenarios where failure is expected and should be validated later rather than treated as a hard stop.
The chapter also shows how to make the automation strategy flexible. Polymorphic drivers let the same scenarios run against different interfaces, such as direct service calls or REST API calls, by switching implementations behind a common driver interface. Stub dependencies are used when external services like time or other hard-to-control resources make testing slow or unreliable; by replacing them with controllable stubs, tests gain speed and determinism without changing the scenarios. Finally, the chapter connects these ideas to broader architectural approaches such as hexagonal architecture and screenplay, showing that the same layering and abstraction principles support maintainable automation across multiple interfaces, environments, and levels of confidence.
Automation concerns are handled by a separate “driver” layer
Separation of concerns into three layers: test, automation, and infrastructure
Using a lambda to create a Test Action without creating a new derived class
Separating action from validation with the Attempt Action pattern
Polymorphic driver interface with multiple implementations
Behavior of the application with a real external time service
Setting and retrieving the time from a stub service
In hexagonal architectures, external services connect to the domain logic through ports and adapters
The Screenplay pattern concepts are actors, abilities, tasks, interactions, and questions
Summary
- Automated tests can exercise application functionality through any interface that is available.
- To test the application through an outer interface, there needs to be a running instance, called an application host which can be externally hosted or self-hosted.
- Testing self-hosted applications is preferable, because the tests can be provided with a temporary, isolated execution environment and can be given access to the application’s memory.
- The Test Driver pattern encapsulates the technical details of how a component works (the automation concern), while providing an outcome-oriented interface to the step definition (the testing concern) that depends on that component.
- Many components will depend on the same technical infrastructure (e.g. HTTPS), so use the Infrastructure Layer pattern to encapsulate the technical details that are needed by each Test Driver.
- The development team will benefit from non-functional test automation features (such as test logging and timing) that cuts across all domains. The Test Action pattern supports centralized definition and maintenance of these test features.
- A test action is a specific executable step that is used to prepare, exercise or verify a particular aspect of the application.
- Any test action may have several possible outcomes, so use the Attempt Action pattern to separate the initiation of an action from the verification of the expected outcome.
- Most scenarios can be targeted at any of the available application interfaces (e.g. UI, REST, direct method call). To gain confidence that all interfaces are behaving as expected, use the Polymorphic Driver pattern to dynamically select the appropriate application interface to target.
- Some behaviors depend on resource intensive components or component states that are hard to achieve. Use the Stub Dependency pattern to provide stubbed test components that are cheaper to use and easier to force into the required state.
- The Hexagonal Architecture pattern (aka Ports and Adapters) is a widely documented pattern that encompasses the driver patterns (Test Driver, Infrastructure Layer, Polymorphic Driver) and the stubbing of dependencies (Stub Dependency).
- The Screenplay pattern is a widely discussed model that introduces five key concepts: Actors, Abilities, Tasks, Interactions, and Questions. These concepts map closely to the layering patterns described in this chapter.
Effective Behavior-Driven Development ebook for free