Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an idiomatic way to have shared utility functions for integration tests and benchmarks?

I have Rust project with both integration tests (in the /tests dir) and benchmarks (in the /benches dir). There are a couple of utility functions that I need in tests and benches, but they aren't related to my crate itself, so I can't just put them in the /utils dir.

What is idiomatic way to handle this situation?

like image 979
Constantine Avatar asked Jun 14 '17 08:06

Constantine


People also ask

What are the different types of integration testing?

Some different types of integration testing are big-bang, mixed (sandwich), risky-hardest, top-down, and bottom-up. Other Integration Patterns are: collaboration integration, backbone integration, layer integration, client-server integration, distributed services integration and high-frequency integration.

What is main purpose of integration testing?

The aim of integration testing is to test the interfaces between the modules and expose any defects that may arise when these components are integrated and need to interact with each other.


2 Answers

Create a shared crate (preferred)

As stated in the comments, create a new crate. You don't have to publish the crate to crates.io. Just keep it as a local unpublished crate inside your project and mark it as a development-only dependency.

This is best used with version 2 of the Cargo resolver. For better performance, consider using a Cargo workspace.

.
├── Cargo.toml
├── src
│   └── lib.rs
├── tests
│   └── integration.rs
└── utilities
    ├── Cargo.toml
    └── src
        └── lib.rs

Cargo.toml

# ...

[dev-dependencies]
utilities = { path = "utilities" }

utilities/src/lib.rs

pub fn shared_code() {
    println!("I am shared code");
}

tests/integration.rs

extern crate utilities;

#[test]
fn a_test() {
    utilities::shared_code();
}

A test-only module

You could place a module inside your crate that is only compiled when a specific feature is passed. This is the same concept used for unit tests. This has the advantage that it can access internals of your library code. It has the disadvantage that you need to pass the flag each time you run the code.

This is best used with version 2 of the Cargo resolver.

Cargo.toml

# ...

[features]
test-utilities = []

src/lib.rs

#[cfg(feature = "test-utilities")]
pub mod test_utilities {
    pub fn shared_code() {
        println!("I'm inside the library")
    }
}

tests/integration.rs

extern crate the_library;

#[test]
fn a_test() {
    the_library::test_utilities::shared_code();
}

execution

cargo test --features=test-utilities

This is best used with version 2 of the Cargo resolver.

Use a module from an arbitrary file path

This is just ugly to me, and really goes out of the normal path.

utilities.rs

pub fn shared_code() {
    println!("This is just sitting out there");
}

tests/integration.rs

#[path = "../utilities.rs"]
mod utilities;

#[test]
fn a_test() {
    utilities::shared_code();
}

See also:

  • Where should I put test utility functions in Rust?
like image 144
Shepmaster Avatar answered Oct 23 '22 15:10

Shepmaster


You could add those utility-functions to a pub-module inside your main crate and use the #[doc(hidden)] or #![doc(hidden)] attribute to hide them from the docs-generator. Extra comments will guide the reader to why they are there.

like image 32
user2722968 Avatar answered Oct 23 '22 14:10

user2722968