'Can I import a module from an integration test of another package in the same workspace

I have a Rust workspace as follows:

|-- Cargo.toml
|-- p1
    |-- Cargo.toml
    |-- src
        |-- lib.rs
    |-- tests
        |-- test.rs
     
|-- p2
    |-- Cargo.toml
    |-- src
        |-- lib.rs
    |-- tests
        |-- test.rs

Can I somehow directly export p1/tests/test.rs so that it's functions can be used from p2/tests/test.rs?

I'm aware of the following alternatives, both of which I'd like to avoid because reasons:

  • use the #[path="../../p1/tests/test.rs"] attribute with a mod in p2/tests/test.rs to create a new sub-module in p1/tests/test.rs.
  • factor out common test definitions into a third, test_utils package


Solution 1:[1]

Tests have access to dev dependencies, so you can provide a dev dependency in p2's Cargo.toml. And you can define a dependency from a local path:

[dev-dependencies]
p1 = { path = "../p1" }

Then in your p2/tests/test.rs file you can use p1::*


EDIT: Just realized that you want to export just the functions in p1/tests/test.rs.

If the test is an integration between p1 and p2, then a separate test file in the top level, outside of p1 and p2, sounds appropriate.

If you want shared utilities between p1 tests and p2 tests, then a 3rd package sounds appropriate.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1