logo

Attribute Macro cucumber::when

source · []
#[when]
Available on crate feature macros only.
Expand description

Attribute to auto-wire the test to the World implementer.

There are 3 step-specific attributes:

Example

use cucumber::{given, when, World};

#[derive(Debug, Default, World)]
struct MyWorld;

#[given(regex = r"(\S+) is (\d+)")]
#[when(expr = "{word} is {int}")]
fn test(w: &mut MyWorld, param: String, num: i32) {
    assert_eq!(param, "foo");
    assert_eq!(num, 0);
}

#[tokio::main]
async fn main() {
    MyWorld::run("./tests/features/doctests.feature").await;
}

Attribute arguments

  • #[given(regex = "regex")]

    Uses Regex for matching the step. Regex is checked at compile time to have valid syntax.

  • #[given(expr = "cucumber-expression")]

    Uses Cucumber Expression for matching the step. It’s checked at compile time to have valid syntax.

  • #[given("literal")]

    Matches the step with an exact literal only. Doesn’t allow any values capturing to use as function arguments.

Function arguments

  • First argument has to be mutable reference to the World deriver.
  • Other argument’s types have to implement FromStr or it has to be a slice where the element type also implements FromStr.
  • To use gherkin::Step, name the argument as step, or mark the argument with a #[step] attribute.
#[given(regex = r"(\S+) is not (\S+)")]
fn test_step(
    w: &mut MyWorld,
    #[step] s: &Step,
    matches: &[String],
) {
    assert_eq!(matches[0], "foo");
    assert_eq!(matches[1], "bar");
    assert_eq!(s.value, "foo is not bar");
}

Return value

A function may also return a Result, which Err is expected to implement Display, so returning it will cause the step to fail.