vextorspace
vextorspace

Reputation: 934

How can I use the same test_helper function in integration tests and unit tests in rust?

So I have an integration test /tests/my_test.rs and a unit test in /src/some_module/function.rs. If I create a test helper in

/tests/test_helper.rs



pub struct TestHelper {
}
impl TestHelper {
    fn meaning(&self) -> i32 {
        42
    }
}

I can figure out to use it in the /tests/my_test.rs

use super::test_helper::TestHelper;

#[test]
fn test_helper_works() {
    let helper = TestHelper::new();
    assert_eq!(helper.meaning(), 42);
}

But I cannot figure out how to access it from /src/some_module/function.rs in a mod tests block

#[cfg(test)]
mod tests {
    use super::*;
    use super::test_helper::TestHelper;

    #[test]
    fn some_old_test() {
        let helper = TestHelper::new();
        assert_eq!(helper.meaning(), 42);
    }
}

I've tried putting the helper in /src/test_helpers/ using #[cfg(test)] but that allowed it in the unit tests but not the integration tests any way I could think to include it.

Upvotes: 0

Views: 38

Answers (0)

Related Questions