Setup & Teardown
Managing database seeds, cleanups, and dynamic pre-conditions is critical for reliable integration tests. Gherkio provides native setup and teardown lifecycle blocks to isolate environment mutations.
π Lifecycle Execution Path
Gherkio follows a strict execution guarantee for test scenarios:
ββββββββββββββββββββββ
β Start Scenario β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββββββββ
β Execute SETUP Stepsβ
βββββββββββ¬βββββββββββ
β
βββββββββββββββ΄ββββββββββββββ
β β
Success β Failure β
βΌ βΌ
ββββββββββββββββββββ ββββββββββββββββββββ
β Execute Main β β Skip Main β
β STEPS β β STEPS β
βββββββββββ¬βββββββββ βββββββββββ¬βββββββββ
β β
βββββββββββββββ¬ββββββββββββββ
β
βΌ
ββββββββββββββββββββββ
β Execute TEARDOWN β
β Steps (Guaranteed) β
βββββββββββ¬βββββββββββ
β
βΌ
ββββββββββββββββββββββ
β End Scenario β
ββββββββββββββββββββββ
- Setup Failures: If any step in the
setupblock fails, Gherkio halts execution immediately, skips the primarystepsblock entirely (marking the scenario as failed), and jumps directly to theteardownblock. - Teardown Guarantees: The
teardownblock is guaranteed to run regardless of whether thesetupblock or the primarystepsblock succeeded or failed. This ensures database cleanups, session invalidations, or resource releases are always performed.
π Lifecycle Configuration Example
Here is a typical scenario writing a temporary resource, asserting against it, and then cleaning it up:
scenario: Manage User Profile
setup:
- use: shared/authenticate.yaml # Get auth token
- request:
method: POST
url: /profiles
headers:
Authorization: "Bearer ${token}"
body:
username: "temp_user"
save:
profileId: body.id
steps:
- request:
method: GET
url: /profiles/$profileId
headers:
Authorization: "Bearer ${token}"
expect:
status: 200
body.username: temp_user
teardown:
- request:
method: DELETE
url: /profiles/$profileId # Clean up target profile
headers:
Authorization: "Bearer ${token}"
expect:
status: 200