何も考えずにRustに入門する4

Rustにはそもそもユニットテストフレームワークがあるようだ. "cargo test"とするとテストが実行される.

今回は自前で"src/lib.rs"を用意したので, このままだと何もテストされない. というわけでユニットテストを書いてみる.

#[macro_use] extern crate log;

pub mod example {
    pub fn hello() {
        debug!("This is a debug message.");
        error!("This is an error message");
        println!("Hello, world!");
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn test_hello() {
            hello();
        }
    }
}

"#[test]"をつけるとテスト時に呼び出されるようになる. このテストでは"hello"の中で"panic!"が呼ばれないことを確認する. あとは"assert!"なり"assert_eq!"なりをがしがし書いていけば良い.