mercredi 27 novembre 2019

How to call test helper function from within integration tests?

I'm trying to figure out how to best organize my tests in Rust, and I'm running into the following problem. I have a test utility (test_util) that I define in a module and I would like to be able to use it from my unit tests as well as from my integration tests.

Definition of test_util in src/lib.rs:

#[cfg(test)]
pub mod test_util {
    pub fn test_helper() {}
}

I can access my helper function from my unit tests in another module, src/some_module.rs:

#[cfg(test)]
pub mod test {
    use crate::test_util::test_helper;

    #[test]
    fn test_test_helper() {
        test_helper();
    }
}

However, when I try to use the utility from my integration test, as in tests/integration_test.rs:

use my_project::test_util::test_helper;

#[test]
fn integration_test_test_helper() {
    test_helper();
}

I get the following compiler message:

8 | use my_project::test_util::test_helper;
  |                 ^^^^^^^^^ could not find `test_util` in `my_project`

Is there a good reason why it is not allowed to access test code from the project from within an integration test belonging to that same project? I get it that integration tests can only access the public parts of the code, but I think it would make sense to also allow access to the public parts of the unit test code. What would be a work around for this?

Aucun commentaire:

Enregistrer un commentaire