Integration test common module questions

Hi all,

I followed the Test Organization - The Rust Programming Language to organize my test files under tests folder, and created a common module in tests/common/mod.rs.

I have two questions:

  1. In my test files, do I have to declare mod common; in each single test file?
  2. In my tests/common/mod.rs, I have a type alias as pub type Dimension = i32;, when I run cargo test, it shows warning: type alias Dimension is never used, but actually I used it in some of my test files under the tests folder. Should add the allow(dead_code) to avoid the warning?
#[allow(dead_code)]
pub type Dimension = i32;

Thanks!

Yes. Every test file is like its own crate and gets compiled separately.

It won't show the warning for the files where you use them, only for those where you don't. You can add an inner attribute at the top of your common/mod.rs file: #![allow(dead_code)] instead of having to add the attribute to each unused item.


In general it is discouraged as being unidiomatic to share common functionality across your integration test files like that. Rust-by-example actually describes this as something you can do but then discourages you from doing it:

Creating the module as tests/common.rs also works, but is not recommended because the test runner will treat the file as a test crate and try to run tests inside it.

A better approach I have seen is how Cargo does it, for example. Cargo has extracted "integration test only" shared functionality into its own crate cargo-test-support part of the workspace that is then imported as dev-dependency to expose it to the integration tests.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.