Overview

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
Using before-test-run and after-test-run hooks to manage the test environment
Lazy initialization of a resource pool
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.

FAQ

What is the main goal of the stagecraft patterns in this chapter?The chapter’s patterns aim to reduce the cost and complexity of working with external resources in tests, so you can keep feedback fast while still handling databases, files, and asynchronous behavior effectively.
What is the Test Environment Initialization pattern used for?It is used to initialize shared external resources efficiently, ideally once per test run instead of once per scenario. A common implementation is a before-test-run hook, with optional cleanup in an after-test-run hook.
Why should test environment setup sometimes be done outside individual scenarios?Because some resources are expensive to provision, start, or upgrade, and repeating that work for every scenario slows the test suite unnecessarily. Running setup once per test run can significantly improve performance.
Why must before-test-run and after-test-run hooks be static methods?They run outside of a scenario context, so dependency injection objects and scenario-scoped services are not available. For that reason, these hooks must be declared as static methods.
What is the Resource Pooling pattern?It is a technique where a limited number of external resources are created and then leased exclusively to scenarios as needed. When a scenario finishes, the resource is returned to the pool for reuse by another scenario.
How does resource pooling help with parallel test execution?If the pool contains as many resources as there are parallel workers, each worker can lease its own resource and run scenarios concurrently. This allows parallel execution without scenarios interfering with one another through shared state.
What happens if a scenario requests a pooled resource and none are available?If the pool cannot satisfy the request, the implementation should throw an exception and fail the scenario. In practice, this should not happen if the pool size matches the configured parallel worker count.
What is the Test File System pattern for?It centralizes all knowledge about input, output, and temporary file locations into a single helper class. This makes file handling easier to maintain and avoids scattering path logic throughout the automation code.
Why is it recommended to encapsulate file-system paths in a helper class?Because current directories, temp paths, source layouts, and output locations can vary across environments. A helper class keeps those rules in one place, so changes only need to be made once.
What are the three main approaches described for asynchronous resources?The chapter outlines Active Waiting, Completed Event, and Synchronous Testing. Active Waiting polls until a result appears, Completed Event waits for a notification, and Synchronous Testing forces the behavior to complete synchronously during tests.

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