how to

To show output

Apr 28, 2024
langsrusttest
1 Minutes
95 Words

ref: https://stackoverflow.com/questions/54585804/how-to-run-a-specific-unit-test-in-rust

1
rustc --test <file>

This generates a binary file. Then if you run the binary file, all tests will be conducted. Source code format be like:

1
#[cfg(test)]
2
mod tests {
3
use super::*;
4
5
#[test]
6
fn test_tuple_out_of_range_positive() {
7
assert_eq!(
8
Color::try_from((256, 1000, 10000)),
9
Err(IntoColorError::IntConversion)
10
);
11
}

To run a specific test, run:

1
./xxx --exact test::test_tuple_out_of_range_positive

Cargo way

1
cargo test test_fn_name # filters with test_fn_name
2
cargo test test_fn_name -- --exact
3
cargo test test_mod_name::test_fn_name -- --exact
4
cargo test --package school_info repeat_students_should_not_get_full_marks -- --exact
5
# To show output
6
cargo test --package py-like --test io -- tests::main --exact --nocapture
7
# test1() in helper.rs
8
cargo test helper::test1 -- --exact
Article title:To show output
Article author:Julyfun
Release time:Apr 28, 2024
Copyright 2025
Sitemap