Struct cucumber::Cucumber

source ·
pub struct Cucumber<W, P, I, R, Wr, Cli = Empty>where
    W: World,
    P: Parser<I>,
    R: Runner<W>,
    Wr: Writer<W>,
    Cli: Args,
{ /* private fields */ }
Expand description

Top-level Cucumber executor.

Most of the time you don’t need to work with it directly, just use World::run() or World::cucumber() on your World deriver to get Cucumber up and running.

Otherwise use Cucumber::new() to get the default Cucumber executor, provide Steps with World::collection() or by hand with Cucumber::given(), Cucumber::when() and Cucumber::then().

In case you want a custom Parser, Runner or Writer, or some other finer control, use Cucumber::custom() or Cucumber::with_parser(), Cucumber::with_runner() and Cucumber::with_writer() to construct your dream Cucumber executor!

Implementations§

Creates a custom Cucumber executor with the provided Parser, Runner and Writer.

Replaces Parser.

Replaces Runner.

Replaces Writer.

Re-outputs Skipped steps for easier navigation.

Example

Output with a regular Cucumber::run():

Adjust Cucumber to re-output all the Skipped steps at the end:

MyWorld::cucumber()
    .repeat_skipped()
    .run_and_exit("tests/features/readme")
    .await;

Re-outputs Failed steps for easier navigation.

Example

Output with a regular Cucumber::fail_on_skipped():

MyWorld::cucumber()
    .fail_on_skipped()
    .run_and_exit("tests/features/readme")
    .await;

Adjust Cucumber to re-output all the Failed steps at the end:

MyWorld::cucumber()
    .repeat_failed()
    .fail_on_skipped()
    .run_and_exit("tests/features/readme")
    .await;

Re-outputs steps by the given filter predicate.

Example

Output with a regular Cucumber::fail_on_skipped():

MyWorld::cucumber()
    .fail_on_skipped()
    .run_and_exit("tests/features/readme")
    .await;

Adjust Cucumber to re-output all the Failed steps ta the end by providing a custom filter predicate:

MyWorld::cucumber()
    .repeat_if(|ev| {
        use cucumber::event::{
            Cucumber, Feature, RetryableScenario, Rule, Scenario, Step,
        };

        matches!(
            ev.as_deref(),
            Ok(Cucumber::Feature(
                _,
                Feature::Rule(
                    _,
                    Rule::Scenario(
                        _,
                        RetryableScenario {
                            event: Scenario::Step(_, Step::Failed(..))
                                | Scenario::Background(
                                    _,
                                    Step::Failed(_, _, _, _),
                                ),
                            retries: _
                        }
                    )
                ) | Feature::Scenario(
                    _,
                    RetryableScenario {
                        event: Scenario::Step(_, Step::Failed(..))
                            | Scenario::Background(_, Step::Failed(..)),
                        retries: _
                    }
                )
            )) | Err(_)
        )
    })
    .fail_on_skipped()
    .run_and_exit("tests/features/readme")
    .await;

Consider Skipped Background or regular Steps as Failed if their Scenario isn’t marked with @allow.skipped tag.

It’s useful option for ensuring that all the steps were covered.

Example

Output with a regular Cucumber::run():

To fail all the Skipped steps setup Cucumber like this:

MyWorld::cucumber()
    .fail_on_skipped()
    .run_and_exit("tests/features/readme")
    .await;

To intentionally suppress some Skipped steps failing, use the @allow.skipped tag:

Feature: Animal feature

  Scenario: If we feed a hungry cat it will no longer be hungry
    Given a hungry cat
    When I feed the cat
    Then the cat is not hungry

  @allow.skipped
  Scenario: If we feed a satiated dog it will not become hungry
    Given a satiated dog
    When I feed the dog
    Then the dog is not hungry

Consider Skipped Background or regular Steps as Failed if the given filter predicate returns true.

Example

Output with a regular Cucumber::run():

Adjust Cucumber to fail on all Skipped steps, but the ones marked with a @dog tag:

MyWorld::cucumber()
    .fail_on_skipped_with(|_, _, s| !s.tags.iter().any(|t| t == "dog"))
    .run_and_exit("tests/features/readme")
    .await;
Feature: Animal feature

  Scenario: If we feed a hungry cat it will no longer be hungry
    Given a hungry cat
    When I feed the cat
    Then the cat is not hungry

  Scenario: If we feed a satiated dog it will not become hungry
    Given a satiated dog
    When I feed the dog
    Then the dog is not hungry

And to avoid failing, use the @dog tag:

Feature: Animal feature

  Scenario: If we feed a hungry cat it will no longer be hungry
    Given a hungry cat
    When I feed the cat
    Then the cat is not hungry

  @dog
  Scenario: If we feed a satiated dog it will not become hungry
    Given a satiated dog
    When I feed the dog
    Then the dog is not hungry

Runs Cucumber.

Features sourced from a Parser are fed to a Runner, which produces events handled by a Writer.

Consumes already parsed cli::Opts.

This method allows to pre-parse cli::Opts for custom needs before using them inside Cucumber.

Also, any additional custom CLI options may be specified as a clap::Args deriving type, used as the last type parameter of cli::Opts.

⚠️ WARNING: Any CLI options of Parser, Runner, Writer or custom ones should not overlap, otherwise cli::Opts will fail to parse on startup.

Example
#[derive(clap::Args)]
struct CustomCli {
    /// Additional time to wait in a before hook.
    #[arg(
        long,
        value_parser = humantime::parse_duration,
    )]
    before_time: Option<Duration>,
}

let cli = cli::Opts::<_, _, _, CustomCli>::parsed();
let time = cli.custom.before_time.unwrap_or_default();

MyWorld::cucumber()
    .before(move |_, _, _, _| time::sleep(time).boxed_local())
    .with_cli(cli)
    .run_and_exit("tests/features/readme")
    .await;
Feature: Animal feature

  Scenario: If we feed a hungry cat it will no longer be hungry
    Given a hungry cat
    When I feed the cat
    Then the cat is not hungry

Also, specifying --help flag will describe --before-time now.

Initializes Default cli::Opts.

This method allows to omit parsing real cli::Opts, as eagerly initializes Default ones instead.

Runs Cucumber with Scenarios filter.

Features sourced from a Parser are fed to a Runner, which produces events handled by a Writer.

Example

Adjust Cucumber to run only Scenarios marked with @cat tag:

MyWorld::cucumber()
    .filter_run("tests/features/readme", |_, _, sc| {
        sc.tags.iter().any(|t| t == "cat")
    })
    .await;
Feature: Animal feature

  @cat
  Scenario: If we feed a hungry cat it will no longer be hungry
    Given a hungry cat
    When I feed the cat
    Then the cat is not hungry

  @dog
  Scenario: If we feed a satiated dog it will not become hungry
    Given a satiated dog
    When I feed the dog
    Then the dog is not hungry

Creates a default Cucumber executor.

Sets the provided language of gherkin files.

Errors

If the provided language isn’t supported.

If max is Some number of concurrently executed Scenarios will be limited.

Makes failed Scenarios being retried the specified number of times.

Makes stop running tests on the first failure.

NOTE: All the already started Scenarios at the moment of failure will be finished.

NOTE: Retried Scenarios are considered as failed, only in case they exhaust all retry attempts and still do fail.

Makes failed Scenarios being retried after the specified Duration passes.

Makes failed Scenarios being retried only if they’re matching the specified tag_expression.

Function determining whether a Scenario is Concurrent or a Serial one.

Function determining Scenario’s RetryOptions.

Sets a hook, executed on each Scenario before running all its Steps, including Background ones.

Sets a hook, executed on each Scenario after running all its Steps, even after Skipped of Failed Steps.

Last World argument is supplied to the function, in case it was initialized before by running before hook or any Step.

Replaces Collection of Steps.

Inserts Given Step.

Inserts When Step.

Inserts Then Step.

Runs Cucumber.

Features sourced from a Parser are fed to a Runner, which produces events handled by a Writer.

Panics

If encountered errors while parsing Features or at least one Step Failed.

Runs Cucumber with Scenarios filter.

Features sourced from a Parser are fed to a Runner, which produces events handled by a Writer.

Panics

If encountered errors while parsing Features or at least one Step Failed.

Example

Adjust Cucumber to run only Scenarios marked with @cat tag:

MyWorld::cucumber()
    .filter_run_and_exit("tests/features/readme", |_, _, sc| {
        sc.tags.iter().any(|t| t == "cat")
    })
    .await;
Feature: Animal feature

  @cat
  Scenario: If we feed a hungry cat it will no longer be hungry
    Given a hungry cat
    When I feed the cat
    Then the cat is not hungry

  @dog
  Scenario: If we feed a satiated dog it will not become hungry
    Given a satiated dog
    When I feed the dog
    Then the dog is not hungry

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Asserts this Writer being Normalized. Read more
Wraps this Writer into a Normalized version. Read more
Wraps this Writer to print a summary at the end of an output. Read more
Wraps this Writer to fail on Skipped Steps if their Scenario isn’t marked with @allow.skipped tag. Read more
Wraps this Writer to fail on Skipped Steps if the given with predicate returns true. Read more
Wraps this Writer to re-output Skipped Steps at the end of an output.
Wraps this Writer to re-output Failed Steps or Parser errors at the end of an output.
Wraps this Writer to re-output filtered events at the end of an output.
Attaches the provided other Writer to the current one for passing events to both of them simultaneously.
Wraps this Writer into a discard::Arbitrary one, providing a no-op ArbitraryWriter implementation. Read more
Wraps this Writer into a discard::Stats one, providing a no-op StatsWriter implementation returning only 0. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.