Share
Embed
RustBook Ch.11 Testing
2019-02-23(Sat)に、RustBook勉強会にて使用したスライドです。
Katsuki Kobayashi
February 23, 2019
Transcript
76 slides
-
༧Ί͓ͼ ൃදऀ C ݴޠ/ARM ΞηϯϒϥͷΈॻ͚Δ ͱ͍͏͜ͱͰɺଞͷݴޠͱ͔ͬ͞ͺΓͰ͢ ʮ͋ͷݴޠͰʙʙʯͱ͔͍͏ͷ શମʹฉ͍͍ͯͩ͘͞ ෆʹ࠷ۙ C++ͷຊΛಡ·͞Ε͍ͯ·͢
λεές ࣾ෩:ʮςετ?ʯ ʮੈؒҰൠͰ͜Μͳײ͡ʯతͳποίϛܴ Katsuki Kobayashi RustBook ษڧձ -
ࣗಈςετΛॻ͜͏! μΠΫετϥઌੜᐌ͘ (1972 ͷΤοηΠ) “Program testing can be very effective
way to show the presence of bugs, but it is hopelessly inadequate for showing their absence.” όάͷଘࡏΛࣔ͢ޮՌతͳํ๏Ͱ͋Δ͕ɺ ແ͍ࣄΛࣔ͢ʹઈతʹෆॆͰ͋Δ Ͱɺ ʮςετΛؤுΔ͖Ͱͳ͍ʯ ɺ ͱ͍͏ҙຯͰͳ͍ Katsuki Kobayashi RustBook ษڧձ -
ਖ਼֬͞ (correctness) Rust ਖ਼֬͞ʹ͍͔ͭͯͳΓߟ͑ΒΕ͍ͯΔ ͕ɺͬͺΓਖ਼֬Ͱ͋Δ͜ͱͷূ໌େม Rust ͷܕγεςϜ͜ͷॏΛ୲͍ͬͯΔ ͕ɺܕ͚ͩͰෆਖ਼֬Λશ෦͑ΒΕͳ͍ ͱ͍͏Θ͚ͰɺRust ݴޠϨϕϧͰͷ
ςετͷࣗಈԽΛαϙʔτ͠·͢ Katsuki Kobayashi RustBook ษڧձ -
ྫ: add_two() (1/2) add_two() ೖྗ͞Εͨʹ 2 ΛՃ͑ͯฦؔ͢ ೖग़ྗͱʹ ίϯύΠϧ͢Δͱ Rust
͕νΣοΫ ܕͷҰக (String ΛೖΕͨΒΤϥʔ) borrow (มͳࢀরΛͨ͠ΒΤϥʔ) ؔͷڍಈϊʔνΣοΫ 10 ͞ΕΑ͏͕ 50 Ҿ͔ΕΑ͏͕ ΤϥʔʹͳΒͳ͍ ʮࣗಈςετʯͷग़൪ Katsuki Kobayashi RustBook ษڧձ -
ྫ: add_two() (2/2) ςετͷํ๏ͷྫ 3 Λ༩͑ͨΒ 5 Λฦ͢ͱ assert ͢Δ
͜ͷςετΛɺίʔυͷมߋͷʹߦͳ͏ ਖ਼͍͠ڍಈΛ͍ͯ͠ΔࣄΛ֬ೝ Katsuki Kobayashi RustBook ษڧձ -
͜ͷνϟϓλʔ ςετ͍͠ͷͰ ྑ͍ςετͷৄࡉʹ͍ͭͯΧόʔ͠ͳ͍ Rust ͷςετͷͨΊͷػೳʹ͍ͭͯٞ͢Δ औΓѻ͏༰ Ξϊςʔγϣϯ ϚΫϩ σϑΥϧτͷڍಈ Φϓγϣϯ
unit test ͱ integration test ͷߏํ๏ Katsuki Kobayashi RustBook ษڧձ -
How to Write Tests Rust ͷςετ ίʔυ͕ظ௨Γಈ͔͘Λ֬ೝ͢Δςετؔ ςετؔͰҰൠʹҎԼͷ 3 ͭͷΞΫγϣϯ
Λ࣮ߦ ඞཁͳσʔλঢ়ଶΛ४උ͢Δ ςετ͍ͨ͠ίʔυΛ࣮ߦ͢Δ ݁Ռ͕ظ௨Γ͔Ξαʔτ͢Δ Ҏ߱ɺRust ͕ςετͷͨΊʹఏڙ͍ͯ͠Δ ͷΛհ test attribute ϚΫϩ should_panic attributeɺ Katsuki Kobayashi RustBook ษڧձ -
The Anatomy of a Test Function(1/7) ͿͬͪΌ͚ɺRust ͷςετͱ test attribute
Λ͚ͭͨؔͷ͜ͱ attribute: Chapt. 5 Ͱͬͨ derive ͱ͔ test attribute ͷ͚ํ: fn ͷલʹ #[test] Λ͚Δ test attribute Λ͚Δͱ cargo test ίϚϯυ ςετϥϯφʔΛϏϧυ࣮ͯ͠ߦ ϨϙʔτΛදࣔ (passes or fails) Katsuki Kobayashi RustBook ษڧձ -
The Anatomy of a Test Function(2/7) cargo new adder --lib
͢Δ % cargo new adder --lib Created library ‘adder‘ package src/libs.rs ͕ҎԼͷ༰Ͱ࡞͞ΕΔ 1 #[cfg(test)] 2 mod tests { 3 #[test] 4 fn it_works() { 5 assert_eq!(2 + 2, 4); 6 } 7 } ͱΓ͋͑ͣ #[cfg(test)] ͱ mod tests ͷߦ ແࢹ Katsuki Kobayashi RustBook ษڧձ -
The Anatomy of a Test Function(3/7) 1 #[cfg(test)] 2 mod
tests { 3 #[test] 4 fn it_works() { 5 assert_eq!(2 + 2, 4); 6 } 7 } #[test] ςετؔΛࣔ͢ ڞ௨ॲཧΛॻ͘ඇςετؔಉ͡Ϟδϡʔϧ ʹॻ͚ΔΑ͏ʹͳ͍ͬͯΔ assert_eq!(2 + 2, 4) 2 + 2 ͕ 4 Ͱ͋ΔࣄΛΞαʔτ Katsuki Kobayashi RustBook ษڧձ -
The Anatomy of a Test Function(4/7) % cargo test Compiling
adder v0.1.0 (/home/rare/work/slides/RustBook/samples/adder) Finished dev [unoptimized + debuginfo] target(s) in 0.53s Running target/debug/deps/adder-1d0dfd1494bab5c6 running 1 test test tests::it_works ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Doc-tests adder running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Playground ͰςετͰ͖Δʜʜ ignored, filtered ʹ͍ͭͯ࣍અͰઆ໌ measured ʹ͍ͭͯݱࡏ nightly ͚ͩ ϕϯνϚʔΫςετ༻ Katsuki Kobayashi RustBook ษڧձ -
The Anatomy of a Test Function(5/7) Doc-tests adder ͱ͍͏ग़ྗ͕͋Δ υΩϡϝϯτςετͷ݁Ռ
API ͷυΩϡϝϯτͷίʔσΟϯάྫίϯύΠ ϧ/ςετՄೳ υΩϡϝϯτͱίʔυͷಉظʹཱͭ!! Katsuki Kobayashi RustBook ษڧձ -
The Anatomy of a Test Function(5/7) Doc-tests adder ͱ͍͏ग़ྗ͕͋Δ υΩϡϝϯτςετͷ݁Ռ
API ͷυΩϡϝϯτͷίʔσΟϯάྫίϯύΠ ϧ/ςετՄೳ υΩϡϝϯτͱίʔυͷಉظʹཱͭ!! Chapt. 14 “Documentation Comments” ͰΔͷ Ͱεϧʔ͠·͢ Katsuki Kobayashi RustBook ษڧձ -
The Anatomy of a Test Function(6/7) ςετͷ໊લΛมߋ it_works() Λ exploration()
ʹͯ͠ΈΔ Playground ແࣄʹ݁Ռͷදࣔʹө running 1 test test tests::exploration ... ok Katsuki Kobayashi RustBook ษڧձ -
The Anatomy of a Test Function(7/7) ࣦഊ͢ΔςετΛೖΕͯΈΔ panic! ϚΫϩΛͬͯ another()
ؔΛ࡞Δ Playground 1 #[test] 2 fn another() { 3 panic!("Make␣this␣test␣fail"); 4 } ແࣄࣦഊ͢Δ (લճΑΓ 2 ߲͕ͭ૿͑ͯΔ) test tests::another ... FAILED failures: ---- tests::another stdout ---- thread ’tests::another’ panicked at ’Make this test fail!’, src/lib.rs:10:9 failures: tests::another Katsuki Kobayashi RustBook ษڧձ -
Checking Results with the assert! (1/5) assert! ϚΫϩ true: Կ͠ͳ͍
false: panic! Λίʔϧ Chapt. 5 ͷ Rectanble ߏମΛྫʹ Playground 1 #[derive(Debug)] 2 pub struct Rectangle { 3 length: u32, 4 width: u32, 5 } 6 7 impl Rectangle { 8 pub fn can_hold(&self, other: &Rectangle) -> bool { 9 self.length > other.length && self.width > other.width 10 } 11 } Katsuki Kobayashi RustBook ษڧձ -
Checking Results with the assert! (2/5) 1 #[derive(Debug)] 2 pub
struct Rectangle { 3 length: u32, 4 width: u32, 5 } 6 7 impl Rectangle { 8 pub fn can_hold(&self, other: &Rectangle) -> bool { 9 self.length > other.length && self.width > other.width 10 } 11 } can_hold() ϝιου ϒʔϧΛฦ͢ → assert! Ͱͷςετʹ࠷ద!! length ͕ 8 , width ͕ 7 ͷ Rectangle ͕ length ͕ 5 , width ͕ 1 ͷ Rectangle Λ hold Ͱ ͖ΔࣄΛςετ Katsuki Kobayashi RustBook ษڧձ -
Checking Results with the assert! (3/5) larger_can_hold_smaller() ςετ Playground (use
super::*; ͢Δඞཁ͋Γ) 1 #[test] 2 fn larger_can_hold_smaller() { 3 let larger = Rectangle { length: 8, width: 7 }; 4 let smaller = Rectangle { length: 5, width: 1 }; 5 6 assert!(larger.can_hold(&smaller)); 7 } running 1 test test tests::larger_can_hold_smaller ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out can_hold() true Λฦ͢ͷͰ pass ͢Δ Katsuki Kobayashi RustBook ษڧձ -
Checking Results with the assert! (4/5) smaller_cannot_hold_larger() ςετ Playground 1
#[test] 2 fn smaller_cannot_hold_larger() { 3 let larger = Rectangle { length: 8, width: 7 }; 4 let smaller = Rectangle { length: 5, width: 1 }; 5 6 assert!(!smaller.can_hold(&larger)); // ’!’ ͕ϛι 7 } running 2 tests test tests::larger_can_hold_smaller ... ok test tests::smaller_cannot_hold_larger ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out can_hold() ͕ false Λฦ͢ͷͰ pass ͢Δ Katsuki Kobayashi RustBook ษڧձ -
Checking Results with the assert! (5/5) όάΛࠐΜͰΈΔ length ͷෆ߸͕ٯ Playground
1 impl Rectangle { 2 pub fn can_hold(&self, other: &Rectangle) -> bool { 3 self.length < other.length && self.width > other.width 4 } // ^ 5 } ݟ௨Γ fail ͢Δ running 2 tests test tests::larger_can_hold_smaller ... FAILED test tests::smaller_cannot_hold_larger ... ok failures: ---- tests::larger_can_hold_smaller stdout ---- thread ’tests::larger_can_hold_smaller’ panicked at ’assertion failed: larger. note: Run with ‘RUST_BACKTRACE=1‘ for a backtrace. Katsuki Kobayashi RustBook ษڧձ -
Testing Equality with the assert_eq! (1/4) ςετͷҰൠతͳํ๏ظͱ݁Ռͷൺֱ assert! ϚΫϩʹ ==
Λͯ͠Α͍ assert_eq!, assert_ne! ϚΫϩ͋Δ ͜ΕΒظͱ݁Ռͷ྆ํΛදࣔͯ͘͠ΕΔ Katsuki Kobayashi RustBook ษڧձ -
Testing Equality with the assert_eq! (2/4) add_two() ʹΑΔྫ: (Playground) 1
fn main() {} 2 pub fn add_two(a: i32) -> i32 { 3 a + 2 4 } 5 6 #[cfg(test)] 7 mod tests { 8 use super::*; 9 10 #[test] 11 fn it_adds_two() { 12 assert_eq!(4, add_two(2)); 13 } 14 } test tests::it_adds_two ... ok Katsuki Kobayashi RustBook ษڧձ -
Testing Equality with the assert_eq! (3/4) ࣦഊ͢ΔΑ͏ʹ͢Δ (Playground) 1 fn
main() {} 2 pub fn add_two(a: i32) -> i32 { 3 a + 3 // ͳ͔ͥ 3 Λ͢ 4 } thread ’tests::it_adds_two’ panicked at ’assertion failed: ‘(left == right)‘ left: ‘4‘, right: ‘5‘’, src/lib.rs:11:9 left ͱ right ͕දࣔ͞ΕΔ ଞͷݴޠςετϑϨʔϜϫʔΫͩͱ expected ͱ actual ͱݺΜͩΓ͢Δ Rust ظͱ݁Ռͷॱ൪Λؾʹ͠ͳ͍ͷͰ assert_eq!(add_two(2), 4); ͱͯ͠ OK Katsuki Kobayashi RustBook ษڧձ -
Testing Equality with the assert_eq! (4/4) assert_ne!ϚΫϩ͋Δ 2 ͭͷ͕ҟͳΕ passɺ͚͠Ε
fail ʮ݁Ռ͕ਖ਼֬ʹղΒͳ͍͕ಛఆͷʹͳΒͳ͍ʯ ͱ͍͏ςετʹ͏ ྫ͑ɺ݁Ռ͕࣮ߦ͢Δ࣌ؒʹΑͬͯมԽ͢Δ ͷ assert_eq!ͱ assert_ne! ༻͢Δʹ PartialEq ͱ Debug τϨΠτ͕࣮ ͞Ε͍ͯΔඞཁ͕͋Δ ’==’ ͱ ’! =’ Ͱൺֱ ݁ՌΛσόοάϑΥʔϚοτͰදࣔ͢Δ ಠࣗʹ࣮ͨ͠ߏମ enum ʹ͍ͭͯ #[derive(PartialEq, Debug)] ͱ͚Εେ OK Katsuki Kobayashi RustBook ษڧձ -
Adding Custom Failure Messages(1/5) ࣦഊ࣌ͷϝοηʔδΛՃͰ͖Δ assert!, assert_eq!, assert_ne! ϚΫϩͷҾ ͷଓ͖ʹ
format! ϚΫϩʹ͢ҾΛ͚Δ “{} textholder” Λॻ͍ͯΛදࣔͰ͖Δ Katsuki Kobayashi RustBook ษڧձ -
Adding Custom Failure Messages(2/5) greet ؔʹΑΔྫ (Playground) 1 pub fn
greeting(name: &str) -> String { 2 format!("Hello␣{}!", name) 3 } 4 5 #[cfg(test)] 6 mod tests { 7 use super::*; 8 9 #[test] 10 fn greeting_contains_name() { 11 let result = greeting("Carol"); 12 assert!(result.contains("Carol")); 13 } 14 } ࠓޙͷ༷มԽͷରԠͷͨΊɺग़ྗʹҾ (໊લ) ؚ͕·Ε͍ͯΔ͔Ͱςετ͍ͯ͠Δ Katsuki Kobayashi RustBook ษڧձ -
Adding Custom Failure Messages(3/5) વɺςετ௨Δ running 1 test test tests::greeting_contains_name
... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Ͱɺյ͠·͠ΐ͏ (Playground) 1 pub fn greeting(_name: &str) -> String { 2 String::from("Hello!") // ҾΛؚ·ͳ͍Α͏ʹ 3 } Katsuki Kobayashi RustBook ษڧձ -
Adding Custom Failure Messages(4/5) յΕ·ͨ͠ failures: ---- tests::greeting_contains_name stdout ----
thread ’tests::greeting_contains_name’ panicked at \ ’assertion failed: result.contains("Carol")’, src/lib.rs:12:9 note: Run with ‘RUST_BACKTRACE=1‘ for a backtrace. Ͱ ςετ͕ࣦഊͨ͠ assert ͷ༰͚ͩදࣔ͞ΕΔ result.contains("Carol") greeting() ؔͷग़ྗΛදࣔͨ͠ํ͕ศར Katsuki Kobayashi RustBook ษڧձ -
Adding Custom Failure Messages(5/5) ςετΛมߋ͢Δ (Playground) 1 #[test] 2 fn
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 ษڧձ -
Panic with should_panic (1/8) ݁Ռ͕ਖ਼͍͔͠ͷνΣοΫ͚ͩͰͳ͘ɺΤϥʔ ݅ʹ͍ͭͯͷνΣοΫॏཁ ྫͱͯ͠ Guess type(9 ষ)
Ͱઆ໌͠·͢ ߏମͷ value ϑΟʔϧυ: 1 ∼ 100 new() ϝιουͰ͕ൣғ֎ͳ panic ͢Δ 1 pub struct Guess { 2 value: i32, 3 } 4 impl Guess { 5 pub fn new(value: i32) -> Guess { 6 if value < 1 || value > 100 { 7 panic!("Guess␣value␣must␣be␣between␣1␣and␣100,␣got␣{}.", value); 8 } 9 Guess { 10 value 11 } 12 } 13 } Katsuki Kobayashi RustBook ษڧձ -
Panic with should_panic (2/8) ൣғ֎ͷΛೖΕͯ panic ͤ͞ΔςετΛॻ͘ attribute ΛՃ͢Δ :
should_panic panic ͨ͠Β pass, ͠ͳ͔ͬͨΒ fail (Playground) 1 #[cfg(test)] 2 mod tests { 3 use super::*; 4 5 #[test] 6 #[should_panic] // ॱ൪ٯͰྑ͍༷ 7 fn greater_than_100() { 8 Guess::new(200); 9 } 10 } running 1 test test tests::greater_than_100 ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Katsuki Kobayashi RustBook ษڧձ -
Panic with should_panic (3/8) όάΛࠐΈ·͠ΐ͏ (Playground) 1 impl Guess {
2 pub fn new(value: i32) -> Guess { 3 if value < 1 /* ΑΓେ͖͍݅Λফ͢ 100 */ { 4 panic!("Guess␣value␣must␣be␣between␣1␣and␣100,␣got␣{}.", 5 value); 6 } 7 Guess { 8 value 9 } 10 } 11 } ແࣄʹࣦഊ͢Δ running 1 test test tests::greater_than_100 ... FAILED Katsuki Kobayashi RustBook ษڧձ -
Panic with should_panic (4/8) running 1 test test tests::greater_than_100 ...
FAILED ϝοηʔδ͕ෆ Ճ͑ͯɺshould_panic ෆਖ਼֬ʹͳΓ͕ͪ ςετ͕ظͱҟͳΔ panic Λىͯ͜͠ pass ͢Δ ͏ͪΐͬͱ͔ͭਖ਼֬ʹ͢ΔͨΊʹ expected ύϥϝʔλʔΛ should_panic attribute ʹՃ͢Δ Katsuki Kobayashi RustBook ษڧձ -
Panic with should_panic (5/8) new() ϝιουΛมߋ ॳظ͕େ͖͗͢Δ͔ɺখ͗͢͞Δ͔Ͱɺpanic ࣌ ͷϝοηʔδΛมߋ͢Δ 1
pub fn new(value: i32) -> Guess { 2 if value < 1 { 3 panic!("Guess␣value␣must␣be␣greater␣than␣or␣equal␣to␣1,␣got␣{}.", 4 value); 5 } else if value > 100 { 6 panic!("Guess␣value␣must␣be␣less␣than␣or␣equal␣to␣100,␣got␣{}.", 7 value); 8 } 9 } Katsuki Kobayashi RustBook ษڧձ -
Panic with should_panic (6/8) should_panic ʹ expected ύϥϝʔλ (Playground) panic
࣌ͷϝοηʔδͷ 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 ษڧձ -
Panic with should_panic (7/8) όάΛࠐΉ panic ͷจݴΛೖΕସ͑ͨ (Playground) 1 if
value < 1 { 2 panic!("Guess␣value␣must␣be␣less␣than␣or␣equal␣to␣100,␣got␣{}.", 3 value); 4 } else if value > 100 { 5 panic!("Guess␣value␣must␣be␣greater␣than␣or␣equal␣to␣1,␣got␣{}.", 6 value); 7 } Katsuki Kobayashi RustBook ษڧձ -
Panic with should_panic (8/8) ແࣄࣦഊ͢Δ ύχοΫͷจݴ͕දࣔ දࣔ͞ΕΔ͖จݴ͕ note ʹදࣔ failures:
---- 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 ษڧձ -
Using Result<T,E> (1/3) ͜͜·Ͱɺςετʹ panic! Λ͖ͬͯͨ ςετʹ Result<T, E> ͑Δ
ઌఔݟͨ it_works() ؔΛ Result Ͱॻ͖͑ ޭ࣌ʹ Ok(()) Λฦ͠ɺࣦഊ࣌ʹจݴΛؚΜͩ Err Λฦ͢ (Playground) 1 #[cfg(test)] 2 mod tests { 3 #[test] 4 fn it_works() -> Result<(), String> { 5 if 2 + 2 == 4 { 6 Ok(()) 7 } else { 8 Err(String::from("two␣plus␣two␣does␣not␣equal␣four")) 9 } 10 } 11 } Katsuki Kobayashi RustBook ษڧձ -
Using Result<T,E> (2/3) ग़ྗ test tests::it_works ... ok test result:
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 ษڧձ -
Using Result<T,E> (3/3) ͜ͷํ๏Λ͏߹ #[should_panic] ͑ͳ͍ ସΘΓʹ Err Λฦ͢Α͏ʹ͠·͠ΐ͏
Katsuki Kobayashi RustBook ษڧձ -
Controlling How Tests Are Run (1/2) cargo test ίϚϯυ ςετϞʔυͰίʔυΛίϯύΠϧ࣮͠ߦ
σϑΥϧτͰશͯͷςετΛύϥϨϧͰ࣮ߦ͠ɺ ࣮ߦதʹੜ͞Εͨग़ྗΛΩϟϓνϟ͢Δ ίϚϯυϥΠϯΦϓγϣϯͰڍಈΛมߋͰ͖Δ Φϓγϣϯ 2 छྨ͋Δ cargo test ͷΦϓγϣϯ ςετόΠφϦͷΦϓγϣϯ 2 छྨͷΦϓγϣϯηύϨʔλʔ ”-- (ϋΠ ϑϯ 2 ͭ)“ Ͱׂ͢Δ Katsuki Kobayashi RustBook ษڧձ -
Controlling How Tests Are Run (2/2) % cargo test --help
| 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 ษڧձ -
Running Tests in Parallel or Conse...(1/2) σϑΥϧτͰෳͷςετΛεϨουͰฒ ྻ࣮ߦ͢Δ ϑΟʔυόοΫΛߴʹड͚औΕΔ ϫʔΫεϖʔεͷঢ়ଶڥมʹґଘ͍ͯ͠ͳ
͍ඞཁ͕͋Δ ବͳྫ ͍͔ͭ͘ͷςετ͕ text-output.txt ʹσʔλΛॻ͘ ֤ςετ͕ϑΝΠϧͷσʔλͷʹΑͬͯ assert ͢Δ ବͳྫͷղܾํ๏ ϑΝΠϧ໊Λมߋ͢Δ ಉ࣌ʹ࣮ߦ͞Εͳ͍Α͏ʹ͢Δ Katsuki Kobayashi RustBook ษڧձ -
Running Tests in Parallel or Conse...(2/2) --test-threads ςετόΠφϦ༻ͷΦϓγϣϯ ςετʹ͏εϨουͷΛࢦఆ %
cargo test -- --test-threads=1 Katsuki Kobayashi RustBook ษڧձ -
Showing Function Output(1/5) σϑΥϧτͰςετ͕ pass ͨ͠߹ඪ४ग़ ྗࣺͯΒΕΔ println!() ΛͬͨؔͰͷྫ 1
fn prints_and_returns_10(a: i32) -> i32 { 2 println!("I␣got␣the␣value␣{}", a); 3 10 4 } Katsuki Kobayashi RustBook ษڧձ -
Showing Function Output(2/5) ςετίʔυ (Playground) ͨͩ͠ɺPlayground ͰίϚϯυϥΠϯΦϓγϣϯ͕ͤͳ͍ʜʜ! 1 #[cfg(test)] 2
mod tests { 3 use super::*; 4 5 #[test] 6 fn this_test_will_pass() { 7 let value = prints_and_returns_10(4); 8 assert_eq!(10, value); 9 } 10 11 #[test] 12 fn this_test_will_fail() { 13 let value = prints_and_returns_10(8); 14 assert_eq!(5, value); 15 } 16 } Katsuki Kobayashi RustBook ษڧձ -
Showing Function Output(3/5) ςετ݁Ռ (Կ͚ͭͳ͍) pass ͨ͠ςετͷग़ྗදࣔ͞Εͳ͍ running 2 tests
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 ษڧձ -
Showing Function Output(4/5) cargo test -- --nocapture Ͱ࣮ߦ ޭ࣌ͷදࣔग़ྗ͞ΕΔ ςετग़ྗͱςετͷ݁Ռ͕ΠϯλϦʔϒ͞Εͯ
͠·͏ˡ ฒྻ࣮ߦ͢ΔͨΊ 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 ษڧձ -
Showing Function Output(5/5) cargo test -- --test-threads=1 --nocapture Ͱ࣮ߦ running
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 ษڧձ -
Running a Subset of Tests (1/5) શͯͷ test suite Λ࣮ߦ͢Δͱ͔͔࣌ؒΔ
߹͕͋Δ cargo test ʹ࣮ߦ͍ͨ͠ςετ໊ΛҾͱ͠ ͯ༩͑ΒΕΔ ྫͱͯ͠ add_two() ؔͷςετΛ 3 ͭॻ͘ Katsuki Kobayashi RustBook ษڧձ -
Running a Subset of Tests (2/5) (Playground) 1 #[cfg(test)] 2
mod tests { 3 use super::*; 4 5 #[test] 6 fn add_two_and_two() { 7 assert_eq!(4, add_two(2)); 8 } 9 10 #[test] 11 fn add_three_and_two() { 12 assert_eq!(5, add_two(3)); 13 } 14 15 #[test] 16 fn one_hundred() { 17 assert_eq!(102, add_two(100)); 18 } 19 } Katsuki Kobayashi RustBook ษڧձ -
Running a Subset of Tests (3/5) ී௨ʹ cargo test running
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 ษڧձ -
Running a Subset of Tests (4/5) cargo test add ςετ໊ͷҰ෦͚ͩॻ͍ͯΑ͘ɺώοτͨ͠ς
ετ͕࣮ߦ͞ΕΔ 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 ษڧձ -
Running a Subset of Tests (5/5) ignore attribute --ignored ΦϓγϣϯΛ͚ͨͱ͖͚࣮ͩߦ
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 ษڧձ -
Test Organization ςετෳࡶ ਓʹΑͬͯ༻ޠߏ͕ҟͳΔ Rust ίϛϡχςΟʔͰೋͭͷΧςΰϦͷ໘Ͱ ςετΛߟ͑Δ unit test খ͍͞୯Ґ
(Ϟδϡʔϧͱ͔) ͰϑΥʔΧεͯ͠ ϓϥΠϕʔτͳΠϯλʔϑΣʔεςετ integration test ֎෦ͷίʔυ͕ϥΠϒϥϦΛ͏Α͏ʹςετ ެ։ΠϯλʔϑΣʔεͷΈΛଞͷϞδϡʔϧͱҰ ॹʹ࣮ߦͯ͠ςετ ςετΛॻ͘͜ͱɺϥΠϒϥϦͷஅย͕ɺ ผʑʹɺҰॹʹɺظ௨Γʹಈ͘͜ͱΛ֬ೝ͢ ΔͨΊʹॏཁͰ͋Δ Katsuki Kobayashi RustBook ษڧձ -
Unit Tests (1/5) unit test ͷత ίʔυͷ֤୯Ґ͕ଞͷίʔυ͔ΒͰ͖͍ͯ Δ͔ Ͳͷ෦͕ਖ਼͘͠ಈ͍ͯɺͲͷ෦͕ਖ਼͘͠ಈ͍ ͍ͯͳ͍͔
׳शతʹ src ҎԼͷ֤ϑΝΠϧͰ tests ͱ͍͏໊લͷϞ δϡʔϧΛ࡞ tests ϞδϡʔϧςετؔΛ࣋ͪɺ cfg(test) ͰΞϊςʔτ͞ΕΔ Katsuki Kobayashi RustBook ษڧձ -
Unit Tests (2/5) #[cfg(test)] ίϯύΠϥʹ cargo test ͰͷΈϏϧυ͢ΔΑ͏ ڭ͑Δ ίϯύΠϧ࣌ؒϑΝΠϧαΠζΛ͑Δ
ޙͰΔ integration test src ʹஔ͔ͳ͍͠ɺ #[cfg(test)] ͚ͳ͍ cargo new --lib Ͱੜ͞ΕΔίʔυʹ͍ ͯΔ (Playground) Ϟδϡʔϧதͷϔϧύʔؔʹద༻͞ΕΔ 1 #[cfg(test)] 2 mod tests { 3 #[test] 4 fn it_works() { 5 assert_eq!(2 + 2, 4); 6 } 7 } Katsuki Kobayashi RustBook ษڧձ -
Unit Tests (3/5) ϓϥΠϕʔτؔΛςετ͢Δ͔൱͔ ٞ͞Ε͍ͯΔ ଞͷݴޠͩͱɺ͔ͬͨ͠ΓɺෆՄೳͩͬͨΓ Rust ͷ privacy rule
ͳΒͰ͖ͯ͠·͏ ϓϥΠϕʔτؔ internal_adder() Ͱߟ ͑Δ 1 pub fn add_two(a: i32) -> i32 { 2 internal_adder(a, 2) 3 } 4 5 fn internal_adder(a: i32, b: i32) -> i32 { 6 a + b 7 } Katsuki Kobayashi RustBook ษڧձ -
Unit Tests (4/5) ςετຊମ internal_addr() Λίʔϧ (Playground) 1 #[cfg(test)] 2
mod tests { 3 use super::*; 4 5 #[test] 6 fn internal() { 7 assert_eq!(4, internal_adder(2, 2)); 8 } 9 } Katsuki Kobayashi RustBook ษڧձ -
Unit Tests (5/5) internal_addr() pub ͕͍͍ͯͳ͍ tests ୯ʹผͷϞδϡʔϧͰ͋ΔͷͰ internal_addr()
Λ tests ͷείʔϓʹ࣋ͬͯ ͍͚Δ running 1 test test tests::internal ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out ϓϥΠϕʔτؔςετ͠ͳ͍͖ͷํ ʹಛʹԿڧ͍·ͤΜ Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (1/11) ϥΠϒϥϦͷެ։ API Λଞͷίʔυ͕͏ͷͱ ಉ͡Α͏ʹςετ͢Δ తϥΠϒϥϦͷύʔπ͕ɺҰॹʹਖ਼͘͠ಈ͘ ͔Λςετ͢Δ͜ͱ ୯ମͰಈ࡞͢Δ͚Ͳ
integrate ͞ΕΔͱΛى ͜͢͜ͱ··͋Δ integrate ͞Εͨίʔυͷ test coverage ॏཁ integration test ͷͨΊʹɺ·ͣ tests σΟϨ ΫτϦΛ࡞Δ Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (2/11) tests σΟϨΫτϦ ϓϩδΣΫτͷҰ൪্ͷϨϕϧʹ࡞Δ (src ͱಉ͡ ֊) cargo
͕ͦ͜Λݟͯɺ֤ϑΝΠϧ͝ͱʹ crate Λ ࡞Δ ୯ମςετͷ src/lib.rs Λͪΐͬͱมߋ͠ ͯ tests/integration_test.rs ʹϦωʔϜ 1 use adder; 2 3 #[test] 4 fn internal() { 5 assert_eq!(4, adder::add_two(2)); 6 } Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (3/11) 1 use adder; 2 3 #[test] 4
fn internal() { 5 assert_eq!(4, adder::add_two(2)); 6 } Կ͕มΘͬͨ? use super::*; ͕ use adder; ʹ test ͕ผ crate Ͱ͋ΔͨΊ ϥΠϒϥϦ໊ adder ʹ͢Δ͜ͱ (1 ഊ) Cargo.toml ͷ name Λॻ͚ OK #cfg(test) ͕ͳ͍ tests σΟϨΫτϦʹஔ͍ͨ͜ͱͰ cargo ͕ͦͷΑ͏ ʹѻͬͯ͘ΕΔ Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (3/11) 1 use adder; 2 3 #[test] 4
fn internal() { 5 assert_eq!(4, adder::add_two(2)); 6 } Կ͕มΘͬͨ? use super::*; ͕ use adder; ʹ test ͕ผ crate Ͱ͋ΔͨΊ ϥΠϒϥϦ໊ adder ʹ͢Δ͜ͱ (1 ഊ) Cargo.toml ͷ name Λॻ͚ OK #cfg(test) ͕ͳ͍ tests σΟϨΫτϦʹஔ͍ͨ͜ͱͰ cargo ͕ͦͷΑ͏ ʹѻͬͯ͘ΕΔ Playground ͰͲ͏͠Α͏ͳ͍ orz Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (4/11) % cargo test Finished dev [unoptimized +
debuginfo] target(s) in 0.00s Running target/debug/deps/adder-1d0dfd1494bab5c6 running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Running target/debug/deps/integration_test-22e1bbc18e3cf744 running 1 test test it_adds_two ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Doc-tests adder running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (5/11) ߲͕ 3 ͭදࣔ͞ΕΔ unit test: ͖͞΄Ͳ·Ͱͷ integration
test doc test integration test Running target/debug/deps/integration_test-XXXXXX ͷߦ͔Β࢝·͍ͬͯΔ ͦͷޙɺ֤ςετͷ݁Ռ͕ग़ྗ͞Εɺ࠷ޙʹ summary ͕ग़ͯ͘Δ ͦͯ͠ doc test ͕ଓ͘ tests σΟϨΫτϦʹೖΕͨϑΝΠϧຖʹηΫγϣ ϯ͕Ͱ͖ͯ݁Ռදࣔ͞ΕΔ Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (6/11) cargo test ͷҾͰಛఆͷςετΛ࣮ߦ --test ΦϓγϣϯΛ cargo ʹ͢
--test Λ͚ͭͳ͍ͱɺunit, integration, doc Ͱ֘ ͢ΔؔͷΈ࣮ߦ͠ʹ͍͘ % cargo test --test integration_test Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running target/debug/deps/integration_test-22e1bbc18e3cf744 running 1 test test it_adds_two ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (7/11) ςετػೳ͝ͱʹϑΝΠϧΛ͚͍ͨ߹ tests σΟϨΫτϦͷ֤ϑΝΠϧͦΕͧΕผͷ crate ͱͯ͠ίϯύΠϧ͞ΕΔ ผͷ crate
ͱͯ͠ѻ͏͜ͱʹ͍ͭͯ Ϣʔβʔ͕࣮ࡍʹ crate Λ͏߹ʹ͍ۙঢ়ଶʹͳ ΔͷͰྑ͍ src ҎԼͷϑΝΠϧͱৼ͍Λڞ༗Ͱ͖ͳ͍ ϔϧύʔؔ Chapt. 7 ͰߦͳͬͨΑ͏ʹڞ༗Ϟ δϡʔϧͱͯ͠ extract ͢ΕΑ͍ Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (8/11) ڞ༗ϞδϡʔϧԽͷྫ tests/common.rs Λͭ͘Δ #[test] Λ͚͕ͭͨؔͳ͍ͷͰ setup() ࣮
ߦ͞Εͳ͍ 1 pub fn setup() { 2 // setup code specific to your library’s tests would go here 3 } % cargo test Compiling adder v0.1.0 (/home/rare/work/slides/RustBook/samples/integration Finished dev [unoptimized + debuginfo] target(s) in 0.52s Running target/debug/deps/adder-1d0dfd1494bab5c6 running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Running target/debug/deps/common-9c75c5df2f50cca9 running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (9/11) ςετͷ݁Ռදࣔʹग़͖ͯͯ΄͘͠ͳ͍ ׳शతͳํ๏ͱͯ͠ tests/common.rs ⇒ tests/common/mod.rs ͱ͢Δ tests
σΟϨΫτϦͷαϒσΟϨΫτϦίϯύ Πϧ͞Εͳ͍ (cargo build ͷͳ͠?) ͠ɺςε τ݁Ռʹදࣔ͞Εͳ͍ % cargo test Finished dev [unoptimized + debuginfo] target(s) in 0.01s Running target/debug/deps/adder-1d0dfd1494bab5c6 running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Running target/debug/deps/integration_test-22e1bbc18e3cf744 running 1 test test it_adds_two ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (10/11) setup() Λ͏ʹ? ҎԼͷΑ͏ʹϞδϡʔϧͱͯ͑͠Α͍ mod common; Chapt.
7 ͷͱ͖ͱಉ͡ common::setup() ͰίʔϧͰ͖ΔΑ͏ʹͳΔ 1 use adder; 2 3 mod common; 4 5 #[test] 6 fn it_adds_two() { 7 common::setup(); 8 assert_eq!(4, adder::add_two(2)); 9 } Katsuki Kobayashi RustBook ษڧձ -
Integration Tests (11/11) src/main.rs ͚ͩ͋ͬͯ src/libs.rs ͕ແ ͍ binary crate
ͷςετ? tests σΟϨΫτϦʹ integration test ࡞Εͳ͍ use Λ͔ͭͬͯ src/main.rs Ͱఆٛ͞Ε͍ͯΔ ؔΛείʔϓʹ࣋ͬͯ͜ΒΕͳ͍ library crate ͔͠ଞͷ crate ͔Β͑ͳ͍ Rust ͷϓϩδΣΫτͰ src/main.rs ͔Β src/lib.rs ʹ͋Δϩδο ΫΛ୯ʹίʔϧ͢Δ͚ͩʹ͢Δ ͜ͷߏͩͱॏཁͳػೳ use Ͱ integration test ͢Δ͜ͱ͕Ͱ͖Δ src/main.rs ϥΠϒϥϦ෦͕ਖ਼͘͠ಈ͚ಈ ͘Α͏ͳখ͍͞ίʔυʹ͓ͯ͘͠ Katsuki Kobayashi RustBook ษڧձ -
Summary (1/2) Rust ͷ test feature ظ௨Γʹίʔυ͕ಈ࡞͚ͭͮ͠ΔͨΊʹͲ͏ػ ೳ͢Δ͖͔Λࢦఆ͢Δํ๏Λఏڙ unit test
ϥΠϒϥϦͷ֤ύʔπΛผʑʹςετͰ͖Δ private ͳ࣮ͷςετ͕Ͱ͖Δ integration test ϥΠϒϥϦͷଟ͘ͷύʔπ͕Ұॹʹਖ਼͘͠ಈ͔͘ ςετͰ͖Δ ϥΠϒϥϦͷެ։ API Λ࣮ࡍʹΘΕΔͷͱಉ͡ ํ๏ͰςετͰ͖Δ Katsuki Kobayashi RustBook ษڧձ -
Summary (2/2) Rust ͷܕγεςϜ ownership ϧʔϧ͍͘ ͔ͭͷλΠϓͷόάΛ͙ͷʹཱͭ ςετɺظ͢Δৼ͍ʹؔ࿈͢ΔϩδοΫ ͷόάͷݮͷͨΊʹґવॏཁͰ͋Δ ͊͞ɺ͜ͷ
Chapter ͰֶΜͩ༰Λ ͜Ε·Ͱͷ Chapter ͷ projects ʹΈࠐ͏!! Katsuki Kobayashi RustBook ษڧձ