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(cucumber::World, Debug, Default)]
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: Before hook is enabled globally for all the executed scenarios. No exception is possible.

WARNING: Think twice before using Before hook!
Whatever happens in a Before hook is invisible to people reading .features. You should consider using a Background keyword as a more explicit alternative, especially if the setup should be readable by non-technical people. Only use a Before hook 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(cucumber::World, Debug, Default)]
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: After hook is enabled globally for all the executed scenarios. No exception is possible.

TIP: After hook receives an event::ScenarioFinished as 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.