3 [Unit tests][unit] are testing one module in isolation at a time: they're small
4 and can test private code. Integration tests are external to your crate and use
5 only its public interface in the same way any other code would. Their purpose is
6 to test that many parts of your library work correctly together.
8 Cargo looks for integration tests in `tests` directory next to `src`.
13 // Define this in a crate called `adder`.
14 pub fn add(a: i32, b: i32) -> i32 {
19 File with test: `tests/integration_test.rs`:
24 assert_eq!(adder::add(3, 2), 5);
28 Running tests with `cargo test` command:
34 test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
36 Running target/debug/deps/integration_test-bcd60824f5fbfe19
41 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
47 test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
50 Each Rust source file in `tests` directory is compiled as a separate crate. One
51 way of sharing some code between integration tests is making module with public
52 functions, importing and using it within tests.
54 File `tests/common.rs`:
58 // some setup code, like creating required files/directories, starting
63 File with test: `tests/integration_test.rs`
66 // importing common module.
73 assert_eq!(adder::add(3, 2), 5);
77 Modules with common code follow the ordinary [modules][mod] rules, so it's ok to
78 create common module as `tests/common/mod.rs`.
80 [unit]: unit_testing.md