Cycle Features can often require set-ups before a system is in the correct state to begin the test or cleanup after having run to return everything to a clean state.
Cycle includes the ability to use two special types of Scenarios called Background and After Scenarios that will run before and after each non-@wip scenario that is executed in a Feature.
Backgrounds and After Scenarios use the same Cycle steps that are used in regular Scenarios, but they give the Cycle Feature developer the ability to run similar steps before and after each Scenario in the Feature without duplicating those steps in each feature. Backgrounds are most useful to run any sort of environment setup or data prep steps before a Scenario is executed. After Scenarios are best served to run cleanups, validations, and special messaging.
The purpose of this article is to highlight how Background/After Scenarios are executed and some common suggested practices for utilizing them.
Background/After Scenario scenarios will run before and after any non-@wip scenario that is executed in the Feature. This means that any Scenario executed within a single Feature will run the same Background/After Scenario.
Also, it is important to understand that After Scenarios always run after a Scenario finishes executing. This means that the After Scenario will run even if a step within the regular Scenario fails.
From the attached example:
The Feature above contains a Background, an After Scenario, two Scenarios, and one @wip scenario.
The Background/After Scenario should be considered as globally applying to any Scenario that is run in the Feature. You cannot specify a Background to be run for Scenario 1 and a Background to be run for Scenario 2. Both Scenario 1 and Scenario 2 will run the Background “My Background” in the above example. Then both Scenario 1 and Scenario 2 will run the same After Scenario “My After Scenario”.
You can see that each Scenario runs the Background and After Scenario by drilling down into the output.
Scenario 1:
Scenario 2:
After Scenarios will ALWAYS execute after a regular Scenario regardless of whether or not a step fails in the regular scenario. For example, I intentionally failed a step in Scenario 1, but the After Scenario still executed:
It is important to note that the Background/After Scenario did not execute before and after the @wip scenario was executed. Background/After Scenario scenario types run only before and after regular non-@wip scenarios:
---
The most commonly used steps/scenarios used in Backgrounds usually involve any sort of setup required to execute a scenario.
This could include but is not limited to:
Below is a deeper dive into an actual Background Scenario pulled from a JDA WMS Bundle Library feature and a description of what is being done with each of the steps. The bundle library Features make extensive use of Backgrounds and are a good reference to understand their usage.
In the above example:
Background scenarios are not limited to the usage as given in this article. You may also find there are instances where you can include dataset creation as part of your Background. This would be useful if every scenario in your Feature uses the same exact dataset with the same variables each time. However, scenarios usually require slightly varying data for each test case, and the dataset creation usually needs to be within the Scenario itself rather than the Background. In those instances, you will probably make slight changes to your dataset variables within the scenario and then run the dataset creation.
The most commonly used steps/scenarios used in After Scenarios usually involve any sort of cleanup required after a scenario is executed.
This could include but is not limited to:
Below is a deeper dive into a couple of After Scenarios included in the JDA WMS Bundle Library (one for an RF terminal-based feature and one for a web UI based feature).
In the above example:
In the above example:
Additional useful steps that might be included in an After Scenario are writing confirmations to the output or custom log file that a test passed or failed based on actions within the main scenario.
You may also run MOCA scripts in the After Scenario to validate that data is in a certain state based on the successful execution of the scenario. For example: you may be testing shipments going to “Load Complete” after a trailer is dispatched. You may include a validation msql MOCA script in the After Scenario to ensure the shipment status is correct after the Scenario executes. Validations may make more sense to be in the main scenario depending on the specific test case and layout of the file, but After Scenarios can be a good place to include validations when it makes sense.