A card-payment scenario needs a payment gateway, but starting one for every scenario wastes time. Adding gateway setup and shutdown steps to the scenario would also obscure the behavior being tested. The Scenario Resource pattern manages a resource for the lifetime of an individual scenario while leaving those mechanics out of its business-facing steps. section 16.1 section 16.0
Keep the payment scenario focused
The scenario says that an authenticated customer places an order, their payment is authorized, and they see a success message with a payment reference. A @payment_gateway tag identifies its resource need; the scenario does not need separate steps to start and stop the gateway. section 16.1
For a hook-based implementation:
- Start the resource in a before-scenario hook and stop it in an after-scenario hook. Use a tag expression when only selected scenarios need it. section 16.1
- Make the resource available to the application when it starts or through an application API. Expose it through scenario context if step definitions must set its state or inspect outcomes. section 16.1
- If the application must receive the resource at startup, initialize the resource first. Assign an explicit order to every hook when their execution order matters. section 16.1
Table 16.1 Resource usages and tagging strategies
|
Resource
usage
|
Tagging
strategy
|
Tag
expression
|
|
Needed for all scenarios
|
No tags needed
|
n/a
|
|
Needed by a few scenarios
|
Tag scenarios that need the resource
|
@payment_gateway
|
|
Needed by most scenarios
|
Tag scenarios that do not need the
resource
|
not
@no_payment_gateway
|
|
A set of scenarios needs the
resource, but each needs a different variation or configuration
|
Tag the scenarios with tags following
a fixed format (e.g., @payment_gateway:<name>): @payment_gateway:stripe, @payment_gateway:paypal
|
Do not pass the tag expression to the
hook attribute, but use the API of the scenario automation tool in the tag
code to strip out the variation or configuration name from the scenario tags
(see listing 16.5)
|
Choose tags according to how widely the resource is needed: no tag for all scenarios, an opt-in or opt-out tag for a subset, or formatted tags for different configurations. section 16.1
For simple setup, dependency injection offers another route: a scenario context class can initialize resources when first needed and dispose of them when its scenario scope ends. Hooks give explicit control over initialization order; dependency injection does not. Choose the approach that provides the control the resource actually needs. section 16.1
