Overview

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
Automation concerns are handled by a separate “driver” layer
Separation of concerns into three layers: test, automation, and infrastructure
Separation of concerns into three layers: test, automation, and infrastructure
Using a lambda to create a Test Action without creating a new derived class
Using a lambda to create a Test Action without creating a new derived class
Separating action from validation with the Attempt Action pattern
Separating action from validation with the Attempt Action pattern
Polymorphic driver interface with multiple implementations
Polymorphic driver interface with multiple implementations
Behavior of the application with a real external time service
Behavior of the application with a real external time service
Setting and retrieving the time from a stub service
Setting and retrieving the time from a stub service
In hexagonal architectures, external services connect to the domain logic through ports and adapters
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
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.

FAQ

What is the main purpose of the mise-en-scène patterns in this chapter?The main purpose is to help structure BDD automation code cleanly and maintainably by separating responsibilities into layers. These patterns make it easier to support outer-interface automation, reuse code, and keep step definitions focused on testing decisions rather than technical details.
What does the Test Driver pattern do?The Test Driver pattern encapsulates the technical details of interacting with the system under test, so step definitions can focus on test intent. A driver initiates and controls execution of a component while hiding protocol, request, and implementation details from the caller.
Why should step definitions stay thin?Step definitions should stay thin because they represent the testing concern, not the automation concern. Keeping them small reduces duplication, avoids hard-to-maintain logic, and prevents step definitions from becoming overloaded with technical details.
What problem does the Infrastructure Layer pattern solve?The Infrastructure Layer pattern separates low-level technical plumbing from automation logic. It centralizes repeated details like HTTP handling, serialization, status-code checks, and authentication headers, which improves maintainability and reduces vendor or framework-specific duplication.
How is the Test Action pattern different from a Test Driver?A Test Driver organizes the automation for a component or interface, while a Test Action represents a single executable business action with shared cross-cutting behavior. Test Actions are useful for wrapping execution with common concerns like logging, timing, and error handling.
When should you use the Attempt Action pattern?You should use the Attempt Action pattern when the expected outcome is validated in a later step, especially in negative scenarios where failure is acceptable. It allows the action result to be captured instead of immediately failing the test, so the Then step can verify the result explicitly.
Why is a destructive-by-default action model recommended?A destructive-by-default model is recommended because most actions in happy-path scenarios should succeed, and failures should usually stop the test immediately. This reduces false negatives and makes unexpected failures easier to diagnose, while still allowing an opt-in attempt mode for negative scenarios.
What is the Polymorphic Driver pattern used for?The Polymorphic Driver pattern lets the same scenarios run against different application interfaces, such as direct service calls, REST API calls, or UI automation. It achieves this by defining a common driver interface and providing multiple implementations selected at runtime through configuration.
How does the Stub Dependency pattern help with hard-to-test external services?The Stub Dependency pattern replaces slow or difficult external dependencies with test-controlled substitutes. This makes scenarios more reliable and faster, especially for dependencies like time, databases, or external services that are hard to influence in tests.
How are Hexagonal Architecture and Screenplay related to the patterns in this chapter?Both are broader industry patterns that address similar challenges. Hexagonal Architecture supports clean boundaries between core logic and external dependencies, which helps with driver selection and stubbing. Screenplay offers a conceptual model of actors, tasks, interactions, and questions that aligns well with layered BDD automation.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Effective Behavior-Driven Development Effective Behavior-Driven Development ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Effective Behavior-Driven Development Effective Behavior-Driven Development ebook for free