way to show the presence of bugs, but it is hopelessly inadequate for showing their absence.” όάͷଘࡏΛࣔ͢ޮՌతͳํ๏Ͱ͋Δ͕ɺ ແ͍ࣄΛࣔ͢ʹઈతʹෆॆͰ͋Δ Ͱɺ ʮςετΛؤுΔ͖Ͱͳ͍ʯ ɺ ͱ͍͏ҙຯͰͳ͍ Katsuki Kobayashi RustBook ษڧձ
greeting_contains_name() { 3 let result = greeting("Carol"); 4 assert!( 5 result.contains("Carol"), 6 "Greeting␣did␣not␣contain␣name,␣value␣was␣‘{}‘", result 7 ); 8 } failures: ---- tests::greeting_contains_name stdout ---- thread ’tests::greeting_contains_name’ panicked at \ ’Greeting did not contain name, value was ‘Hello!‘’, src/lib.rs:12:9 note: Run with ‘RUST_BACKTRACE=1‘ for a backtrace. Katsuki Kobayashi RustBook ษڧձ
࣌ͷϝοηʔδͷ substring Λࢦఆ͢Δ 1 #[cfg(test)] 2 mod tests { 3 use super::*; 4 5 #[test] 6 #[should_panic( 7 expected = "Guess value must be less than or equal to 100")] 8 fn greater_than_100() { 9 Guess::new(200); 10 } 11 } running 1 test test tests::greater_than_100 ... ok Katsuki Kobayashi RustBook ษڧձ
---- tests::greater_than_100 stdout ---- thread ’tests::greater_than_100’ panicked at \ ’Guess value must be greater than or equal to 1, got 200.’, src/lib.rs:14:13 note: Run with ‘RUST_BACKTRACE=1‘ for a backtrace. note: Panic did not include expected string \ ’Guess value must be less than or equal to 100’ Katsuki Kobayashi RustBook ษڧձ
ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (ࢼ): 2 + 2 Λ 2 + 3 ͱͨ͠ΒҎԼͷΑ͏ʹ assert_eq! Έ͍ͨͳग़ྗ͕ͩ ࠨลςετόΠφϦͷ݁Ռ (ඇ 0) ͷΛҙຯ͍ͯ͠ Δ༷ ӈลޭ࣌ͷ݁Ռ (0) Error: "two plus two does not equal four" thread ’tests::it_works’ panicked at \ ’assertion failed: ‘(left == right)‘ left: ‘1‘, right: ‘0‘: the test returned a termination value \ with a non-zero status code (1) which indicates a failure’, src/libtest/lib.rs Katsuki Kobayashi RustBook ษڧձ
| head cargo-test Execute all unit and integration tests of a local package USAGE: cargo test [OPTIONS] [TESTNAME] [-- <args>...] OPTIONS: --lib Test only this package’s library --bin <NAME>... Test only the specified binary --bins Test all binaries % cargo test -- --help | head Compiling hello v0.1.0 (/tmp/hello) Finished dev [unoptimized + debuginfo] target(s) in 0.59s Running target/debug/deps/hello-ef71f4ccd46d6561 Usage: --help [OPTIONS] [FILTER] Options: --include-ignored Run ignored and not ignored tests --ignored Run only ignored tests --test Run tests and not benchmarks --bench Run benchmarks instead of tests --list List all tests and benchmarks -h, --help Display this message (longer with --help) Katsuki Kobayashi RustBook ษڧձ
test tests::this_test_will_pass ... ok test tests::this_test_will_fail ... FAILED failures: ---- tests::this_test_will_fail stdout ---- I got the value 8 thread ’tests::this_test_will_fail’ panicked at ’assertion failed: ‘(left == r left: ‘5‘, right: ‘10‘’, src/lib.rs:20:9 note: Run with ‘RUST_BACKTRACE=1‘ for a backtrace. failures: tests::this_test_will_fail test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out Katsuki Kobayashi RustBook ษڧձ
͠·͏ˡ ฒྻ࣮ߦ͢ΔͨΊ running 2 tests I got the value 8 thread ’I got the value 4 tests::this_test_will_fail’ panicked at ’assertion failed: ‘(left == right)‘ left: ‘5‘, right: ‘10‘’, src/lib.rs:20:9 note: Run with ‘RUST_BACKTRACE=1‘ environment variable to display a backtrace. test tests::this_test_will_pass ... ok test tests::this_test_will_fail ... FAILED failures: failures: tests::this_test_will_fail test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out Katsuki Kobayashi RustBook ษڧձ
2 tests test tests::this_test_will_fail ... I got the value 8 thread ’main’ panicked at ’assertion failed: ‘(left == right)‘ left: ‘5‘, right: ‘10‘’, src/lib.rs:20:9 note: Run with ‘RUST_BACKTRACE=1‘ environment variable to display a backtrace. FAILED test tests::this_test_will_pass ... I got the value 4 ok failures: failures: tests::this_test_will_fail test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out Katsuki Kobayashi RustBook ษڧձ
3 tests test tests::add_three_and_two ... ok test tests::add_two_and_two ... ok test tests::one_hundred ... ok test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out cargo test one_hundred 2 ͭͷςετ͕࣮ߦ͞Ε͍ͯͳ͍ࣄ͕දࣔ͞ΕΔ running 1 test test tests::one_hundred ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; \ 2 filtered out ^^^^^^^^^^^^^^ Katsuki Kobayashi RustBook ษڧձ
ετ͕࣮ߦ͞ΕΔ one_hundred ςετ͚࣮ͩߦ͞Εͳ͍ ςετ໊Ϟδϡʔϧ໊ͰࢦఆͰ͖Δ cargo test e ͱ͔Ͱશ෦͔͔Δ running 2 tests test tests::add_three_and_two ... ok test tests::add_two_and_two ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out Katsuki Kobayashi RustBook ษڧձ
1 #[test] 2 #[ignore] 3 fn expensive_test() { 4 // code that takes an hour to run 5 } % cargo test running 2 tests test expensive_test ... ignored test it_works ... ok test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out ^^^^^^^^^ % cargo test -- --ignored running 1 test test expensive_test ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out Katsuki Kobayashi RustBook ษڧձ