Attribute Macro cucumber_codegen::given[][src]

#[given]
Expand description

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

There are 3 step-specific attributes:

Example

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

#[derive(Debug, WorldInit)]
struct MyWorld;

#[async_trait(?Send)]
impl World for MyWorld {
    type Error = Infallible;

    async fn new() -> Result<Self, Self::Error> {
        Ok(Self {})
    }
}

#[given(regex = r"(\S+) is (\d+)")]
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;
}

Arguments

  • First argument has to be mutable reference to the WorldInit deriver (your World implementer).
  • 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");
}