Unit tests go into the same file as the actual code in src/*.rs
The annotation of #[cfg(test)] tells $ cargo build to not build the tests. Instead, use $ cargo test to build and run tests automatically.
#[cfg(test)]
mod tests {
#[test]
fn first_unit_test() {
// Add unit test code here.
}
}
Use the annotation [#test] to denote that the function is a test. Some functions may be used for setup which are not unit or integration tests.
Integration tests go into separate files at tests/integration_tests.rs.
Use $ cargo test -- integration_tests to only run a specific test in the tests/ directory.
Use $ cargo test <FUNCTION_NAME> to only run a specific test function.
For time-consuming tests, add the [#ignore] annotation after the [#test] annotation to disable the test by default. Run it by using $ cargo test -- --ignored.
assert*
macros to test code.claim
library): https://svartalf.info/posts/2020-03-13-assertion-macros-for-rust/src/*.rs
#[cfg(test)]
tells$ cargo build
to not build the tests. Instead, use$ cargo test
to build and run tests automatically.[#test]
to denote that the function is a test. Some functions may be used for setup which are not unit or integration tests.tests/integration_tests.rs
.$ cargo test -- integration_tests
to only run a specific test in thetests/
directory.$ cargo test <FUNCTION_NAME>
to only run a specific test function.[#ignore]
annotation after the[#test]
annotation to disable the test by default. Run it by using$ cargo test -- --ignored
.https://doc.rust-lang.org/book/ch11-00-testing.html