The #[cfg(test)] annotation tells Rust to run the function only when cargo test is run and not when cargo build is run

Functions

1
2
3
assert!(1 == 2, "wasn't true"); //it will print wasn't true when test fails
assert_eq!(6, 6);
assert_ne!(6, 5);

Run test

1
cargo test

Run tests in parallel or consecutively

1
cargo test -- --test-threads=<numbers>

Change output capture behavior, show logs

1
cargo test -- --nocapture

Run single or multiple tests

1
cargo test <function-name|pattern>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
pub fn add_one(num: i64) -> i64 {
    num + 1
}

pub fn add_two(num: i64) -> i64 {
    num + 2
}

#[cfg(test)]
mod tests {
	use super::*;
	#[test]
	fn add_one_test() {
		assert_eq!!(4, add_one(3));
	}

	#[test]
	fn add_two_test() {
		assert_eq!(6, add_two(4));
	}
}

Example

1
2
3
4
//will run only one test
cargo test add_one_test
//will run all tests starting with add_
cargo test add_

Test function that should panic

use #[should_panic] attribute

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
pub fn i_panic() {
    panic!("omg!");
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    #[should_panic]
    fn should_panic() {
        i_panic();
    }
}

//The above test will pass

Ignore a test

Use #[ignore] attribute

Integration tests

Make a directory called tests at the root level of the project. Assume you have this code in the lib.rs file in the project adder

1
2
3
4
5
6
7
/*
|-adder/src
  |--lib.rs
*/
pub add_two(num: i64) -> i64{
	num + 2
}

tests/integration_test.rs

1
2
3
4
5
6
extern crate adder;

#[test]
fn add_two_test(){
	assert_eq!(6, adder::add_two(4));
}

We don’t need to use #[cfg(test)] here. When running cargo test, it will run both unit and integration tests. You can also run only tests from a particular integration test file. In the above example, if you want to run tests from integration_test.ts file

1
cargo test --test integration_test.rs