Search code examples
unit-testingtestingrustrust-cargo

cargo build failed with #[rstest] inside


I want to use parametrized testing and found rstest that can do this well.

adding: use rstest::rstest; in the main.rs file and putting the #[rstest] also inside main.rs runs fine on cargo test

but if I want to build the programm with cargo build I got this error

| use rstest::rstest;
 |     ^^^^^^ use of undeclared crate or module `rstest`

so the question is: How do I have to organize my code to use #[rstest] and also be able to build/run the program ?


Solution

  • Depending if you want to run the code using rstest with the non-test build or not you either have to add rstest in your Cargo.toml

    [dependencies]
    rstest = "*"
    

    or you have to remove the code using rstest from non test builds:

    #[cfg(test)]
    use rstest::rstest;