How to organise common test code?

I have this workspace (?) structure:

root
├───common
│   ├───src
│   │   └───lib.rs
│   └───Cargo.toml
│
├───crate_01
│   ├───src
│   │   ├───lib.rs 
│   │   └───tests.rs
│   └───Cargo.toml
│   
├───crate_02
│   ├───src
│   │   ├───lib.rs 
│   │   └───tests.rs
│   └───Cargo.toml
│   
├───exe_(application)
    ├───src
    │   └───main.rs
	  └───Cargo.toml

exe_(application) is the main executable cli.

In root/crate_01/tests.rs and root/crate_02/tests.rs, I have some identical and repeated codes.

How do I move this code to a common (test) area, so that tests in both crates can access it, please?

Thank you and best regards,

...behai.

What about adding a publish = false crate with all your testing code in it and adding it as a path dev-dependency to each of the main crates?

[dev-dependencies]
testing = { path = "../testing" }
4 Likes

Thank you Michael, I will give this a try.

Best regards,

...behai.

1 Like

The suggested solution works. I post what I've done for the shake of completeness.

I now have one more crate testing:

root
├───common
├───crate_01
├───crate_02
│
├───testing
│   ├───src
│   │   └───lib.rs # all test helper functions defined as pub.
│   └───Cargo.toml
│
├───exe_(application)

testing's Cargo.toml:

[package]
...
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
common = { path = "../common" }

-- Please note publish = false under [package].

Both crate_01's and crate_02's Cargo.toml have this additional entry:

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

Both crate_01's and crate_02's tests.rs module have this additional import:

use testing::{fn1, ..., etc.};

References:

3 Likes