Expectest assertions helper in both unit and integration test



First, a bit of context.

Currently, some of my unit tests look like that:

fn it_works() {

My problem is that assertions are a bit hard to read. The second assertion is the opposite of the first but it's easy to miss.

So I've been trying to use expectest with custom assertions

 struct Contain {
     // ...

 fn contain(suffix: &pattern) -> StringContains {
      // ...

impl<T> Matcher<T, ()> for Contain
    T: AsRef<str> + std::fmt::Display ,
    fn matches(&self, actual: &T) -> bool {

That way I can write tests like this and I'm happy:

fn it_works() {

So far, so good, but now I want to use my contain help in integration tests too.

Things I've tried

1/ Using:

// in src/lib.rs
pub mod test_helpers() {
   // ...

does not work: integration tests do not seem to be able to see the code inside #[cfg(test]]` in the main crate - and if I'm not mistaken, this is by design.

2/ Create a workspace with two crates

# <root>/Cargo.toml
members = ["myapp", "expectest_helpers"]
# <root>/myapp/Cargo.toml
expectest_helpers = { path = "../expectest_helpers" }

This works but seems a bit overkill.

Any ideas?

That's how I'd do it.

You can try to #[cfg(feature = "test-helpers")] instead of #[cfg(test)], (adding test-helpers to the features of your crate), and then run the tests with cargo test --features test-helpers

OK. Thans for the pointers :slight_smile: