cucumber-rs / cucumber

Cucumber testing framework for Rust. Fully native, no external test runners or dependencies.
https://cucumber-rs.github.io/cucumber/main
Apache License 2.0
579 stars 70 forks source link

How can I provide a specific context to a specific test? #227

Closed artgocode closed 2 years ago

artgocode commented 2 years ago

Hi! First of all I want to thank you for the effort you put into this project. This crate is awesome!

I am now facing ambiguity issues when running my tests. I need help to wrap my head around on how can I provide a specific context to my tests? Under "contex" I mean the .rs files with given, when, then expressions defined for a specific .feature files.

I am developing a backend api based on actix_web framework. My workspace contains two members as for now. Application crate with library and my test crate witch contains all cucumber tests/features. My test workspace member has next structure:

src/_
    |_lib.rs            // mod helpers; mod context;
    |_helpers.rs        // here I put all common structs/functions/assertion helpers for all context files, no "given", "when", "then" expressions here
    |_context/
        |_api/
           |_admin/
               |_managing_currencies_context.rs // here I define all "given", "when", "then" matches for "managing_currencies.feature" 
               |_managing_locales_context.rs // here I define all "given", "when", "then" matches for "managing_locales.feature"
tests/
    |_features/
    |   |_currency/
    |   |   |_managing_currencies/
    |   |       |_adding_currency.feature
    |   |       |_removing_currency.feature
    |   |_locale/
    |       |_managing_locales/
    |           |_adding_locale.feature
    |           |_removing_locale.feature
    |_currency/
    |   |_adding_currency.rs
    |   |_removing_currency.rs
    |_locale/
        |_adding_locale.rs

adding_locale.rs contains code:

use cucumber::WorldInit;
use test::helpers::TestApp;

#[tokio::main]
async fn main() {
    TestApp::run("features/locale/managing_locales/adding_locale.feature").await;
}

adding_currency.rs contains code:

use cucumber::WorldInit;
use test::helpers::TestApp;

#[tokio::main]
async fn main() {
    TestApp::run("features/currency/managing_currencies/adding_currency.feature").await;
}

Cargo.toml for test crate:

[package]
***
[lib]
name = "test"

[[test]]
name = "adding_locale"                
harness = false                       
path = "tests/locale/adding_locale.rs"

[[test]]
name = "adding_currency"
harness = false
path = "tests/currancy/adding_currency.rs"

TestApp definition from helpers.rs file:

#[derive(Debug, WorldInit)]
pub struct TestApp {
    pub api_client: ApiClient,
    pub db_pool: hryven::DbPool,
}

#[async_trait(?Send)]
impl World for TestApp {
    // We do require some error type.
    type Error = Infallible;

    async fn new() -> Result<Self, Infallible> {
        Ok(spawn_app().await) // spawn_app() bootstraps the application instance
    }
}

I am now trying to figure out, how can I provide a managing_currencies_context.rs context only to adding_currency.rs and removing_currency.rs tests? Or maybe there is another way to structure my tests? I tried to find some examples of a large test code bases, but failed to.