21 Stagecraft patterns for interacting with external resources
The chapter focuses on practical ways to keep test automation fast and reliable when scenarios depend on external resources that are slow, costly, or difficult to coordinate. It presents a set of low-level “stagecraft” techniques for reducing setup overhead, preserving isolation, and making automation code easier to maintain. The core idea is to hide resource-specific complexity behind reusable helpers and lifecycle hooks so tests stay readable while the infrastructure work happens behind the scenes.
One major theme is efficient environment setup and cleanup. Instead of initializing expensive resources before every scenario, the chapter recommends doing one-time work with run-level hooks, while noting the limitations of those hooks because they operate outside scenario context and must therefore be static. It also shows how on-demand initialization can defer work until needed. Another key technique is resource pooling, which lets parallel scenarios lease separate instances of a resource, such as databases, and return them afterward. By moving pooling into dependency injection configuration, the test code itself can remain largely unchanged while still benefiting from parallel execution.
The chapter also treats the file system as an external resource that deserves the same discipline as databases or services. It recommends wrapping all file-path logic in a single helper that knows where to read input, write output, and place temporary files, avoiding fragile assumptions about the current directory or machine-specific layouts. Finally, it surveys patterns for asynchronous resources: polling until completion, waiting for a completion event, or replacing asynchronous behavior with a synchronous mode for testing. Each approach has trade-offs, especially around timing, timeouts, and the risk of masking real concurrency issues, so the choice should match the behavior the scenario is actually meant to verify.
Using before-test-run and after-test-run hooks to manage the test environment
Lazy initialization of a resource pool
Summary
- Initializing external resources can be time-consuming, so the Test Environment Initialization pattern suggests using a before-test-run hook to make sure that they are not automatically initialized for every scenario.
- Resource initialization can be further optimized by deferring initialization until the first time the resource is requested by a scenario.
- To preserve fast feedback, it is often desirable to have multiple scenarios run in parallel. The Resource Pooling pattern offers a solution when those scenarios depend on costly external resources by providing a limited pool of those resources that can be reused.
- The number of parallel scenarios using a given external resource is limited by the number of instances of that resource available from the resource pool.
- To avoid incurring costs when running a small number of scenarios, initialization of resource instances should be deferred until they have been requested by a scenario.
- When combining Resource Pooling with the Skippable Reset pattern from chapter 18, the tracking of any changes made to the resource by the scenario needs to be stored by the context object that owns that specific resource instance.
- Since the resource pool is intended to support interaction with multiple threads executing in parallel, its implementation must be thread safe.
- The Test File System pattern encapsulates and abstracts the complications of interacting with the underlying file system.
- The calculation of environment-specific file paths should be delegated to the Test File System abstraction.
- The Test File System can prevent symptoms of caching or buffering that might occur in the real file system from affecting test outcomes. This can result in false positives, so the tests should regularly be run using the real system.
- Asynchronous requests are ones whose actions are not guaranteed to have completed by the time the call making the request returns.
- The Active Waiting pattern (aka busy looping and polling) is a simple but often inefficient approach to synchronizing automation code with the completion of an asynchronous operation.
- The Completed Event pattern is usually more efficient than the Active Waiting pattern, because it synchronizes the automation code with an event emitted as soon as the asynchronous operation completes.
- The Synchronous Testing pattern can simplify the execution of some scenarios by running an asynchronous operation synchronously. This can result in false positives when the lack of asynchrony affects the behavior exhibited by the system as a whole.
Effective Behavior-Driven Development ebook for free