The #[test] attribute
by
Daichi Mukai
×
Copy
Open
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
The The #[test] #[test] attribute attribute
Slide 2
Slide 2 text
//! test.rs #[test] fn my_test() { assert!(2 + 2 == 4); }
Slide 3
Slide 3 text
$ rustc --test test.rs $ ./test running 1 test test my_test ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0
Slide 4
Slide 4 text
どういう仕組み︖ どういう仕組み︖ $ rustc --emit=asm --test rust.rs $ wc -l test.rs 4 test.rs $ wc -l test.s 2406 test.s
Slide 5
Slide 5 text
HIR HIR Rust のコンパイル︓ 1. Rust source 2. HIR 3. MIR 4. LLVM IR 5. Machine code HIR を⾒てみる $ rustc +nightly --test -Z unpretty=hir test.rs
Slide 6
Slide 6 text
STEP1. RE-EXPORT STEP1. RE-EXPORT どうやって crate root から test_my_priv_func を⾒ るか︖ mod my_priv_func { fn my_priv_func() -> bool { true } #[test] fn test_my_priv_func() { assert!(my_priv_func()); } }
Slide 7
Slide 7 text
pub にして re-exportする mod my_priv_func { fn my_priv_func() -> bool { true } // 正確には少し違う pub fn test_my_priv_func() { assert!(my_priv_func()); } pub mod __test_reexports { pub use super::test_my_priv_func; } }
Slide 8
Slide 8 text
STEP2. HARNESS GENERATION STEP2. HARNESS GENERATION テスト⽤の main #[main] pub fn main() { extern crate test; test::test_main_static(&[&path::to::test1, /*...*/]); }
Slide 9
Slide 9 text
STEP3. TEST OBJECT STEP3. TEST OBJECT GENERATION GENERATION test::TestDesc で configuration を指定できる pub struct TestDesc { pub name: TestName, pub ignore: bool, pub should_panic: ShouldPanic, pub allow_fail: bool, }