Scenario hooks
Scenario hooks represent a code running for each scenario and not visible in .feature files.
Before hook
Before hook runs before the first step of each scenario, even before Background ones.
extern crate cucumber; extern crate futures; extern crate tokio; use std::time::Duration; use cucumber::World as _; use futures::FutureExt as _; use tokio::time; #[derive(Debug, Default, cucumber::World)] struct World; fn main() { World::cucumber() .before(|_feature, _rule, _scenario, _world| { time::sleep(Duration::from_millis(300)).boxed_local() }) .run_and_exit("tests/features/book"); }
NOTE:
Beforehook is enabled globally for all the executed scenarios. No exception is possible.
WARNING: Think twice before using
Beforehook! Whatever happens in aBeforehook is invisible to people reading.features. You should consider using aBackgroundkeyword as a more explicit alternative, especially if the setup should be readable by non-technical people. Only use aBeforehook for low-level logic such as starting a browser or deleting data from a database.
After hook
After hook runs after the last step of each scenario, even when that step fails or is skipped.
extern crate cucumber; extern crate futures; extern crate tokio; use std::time::Duration; use cucumber::World as _; use futures::FutureExt as _; use tokio::time; #[derive(Debug, Default, cucumber::World)] struct World; fn main() { World::cucumber() .after(|_feature, _rule, _scenario, _ev, _world| { time::sleep(Duration::from_millis(300)).boxed_local() }) .run_and_exit("tests/features/book"); }
NOTE:
Afterhook is enabled globally for all the executed scenarios. No exception is possible.
TIP:
Afterhook receives anevent::ScenarioFinishedas one of its arguments, which indicates why the scenario has finished (passed, failed or skipped). This information, for example, may be used to decide whether some external resources (like files) should be cleaned up if the scenario passes, or leaved "as is" if it fails, so helping to "freeze" the failure conditions for better investigation.