There are different possibilities for Unit Tests in Rust.
You have this directory structure:
project_name/
src/
file.rs
main.rs || lib.rs
cargo.toml
Same file: simple and recommended
In Rust a file is module but you can declare a module inside a file with the mod
keyword, children module can access parent private code (ex. your_function()
):
fn your_function() {
//...
}
#[cfg(test)]
mod tests {
#[test]
fn your_function_test() {}
}
Separate files hence modules
In your main.rs
or lib.rs
you declare the two modules
mod file;
#[cfg(test);
mod tests;
Then in your file.rs
:
// !!! pub function since they are sibling modules
// and by default in Rust all is private !!!
pub fn your_function() {
//...
}
And in the tests.rs
:
#[test]
fn your_function_test() {
// ...
}
The concepts are practically the same, when you learn how Rust manages modules it's all easy. You just put a macro on top of the module declaration and that module is your test module. Different story for the integration tests.