【Rust】ユニットテスト

テストを実行するには、
$ cargo test
を実行する

fn main() {
    println!("This is test code");
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2+2, 4);
    }
}

Running unittests src/main.rs (target/debug/deps/sample-a78b7dbded83e75d)

running 1 test
test tests::it_works … ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

作成したfunctionのテストを行う

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

    #[test]
    fn test_quick_sort() {
        let data = vec![6, 15, 4, 2, 8, 5, 11, 9, 7, 13];
        let expected = vec![2, 4, 5, 6, 7, 8, 9, 11, 13, 15];
        assert_eq!(expected, quick_sort(data));
    }
}

note: to see what the problems were, use the option `–future-incompat-report`, or run `cargo report future-incompatibilities –id 1`
Running unittests src/main.rs (target/debug/deps/sample-a78b7dbded83e75d)

running 1 test
test tests::test_quick_sort … ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

assert!, assert_eq!, assert_ne!がある。

うおおおおおおおおおお、これは超絶勉強になるな…