1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RustでUnit Testを書く

Posted at

概要

RustでUnit Testを書く方法を学ぶための作業手順メモです。

参考

プロジェクトの作成

Unit Testにフォーカスするためにライブラリプロジェクトを作成し、
空の状態で cargo test を実行してみます。

% cargo new unit_test_hello_world --lib
     Created library `unit_test_hello_world` package
% cd unit_test_hello_world

% cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests src/lib.rs (target/debug/deps/unit_test_hello_world-c10baac44515af32)

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

   Doc-tests unit_test_hello_world

running 0 tests

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

src/lib.rs に次のようなファイルが生成されているため、
プロジェクト生成の直後の状態でもテストが1件実行されています。

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

生成されたコードについて調べたこと

  • #[cfg(test)] アトリビュートを付けたモジュールは cargo test を実行したときのみコンパイルされる。
  • テストケースとなる関数には #[test] アトリビュートを付ける

簡単な関数を作り、そのテストを記載する

pub fn my_div(x: i64, y: i64) -> i64 {
    x / y
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_my_div() {
        assert_eq!(2, super::my_div(4, 2));
        assert_eq!(2, super::my_div(5, 2));
        assert_eq!(3, super::my_div(6, 2));
    }
}

実行結果

% cargo test
   Compiling unit_test_hello_world v0.1.0 (/.../unit_test_hello_world)
    Finished test [unoptimized + debuginfo] target(s) in 0.30s
     Running unittests src/lib.rs (target/debug/deps/unit_test_hello_world-c10baac44515af32)

running 1 test
test tests::test_my_div ... ok

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

   Doc-tests unit_test_hello_world

running 0 tests

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

エラーケースを追加

Result型の勉強も兼ねてエラーケースの追加してみます。

pub fn my_div(x: i64, y: i64) -> Result<i64, &'static str> {
    if y != 0 {
        Ok(x / y)
    } else {
        Err("divider must not 0")
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_my_div() {
        assert_eq!(Ok(2), super::my_div(4, 2));
        assert_eq!(Ok(2), super::my_div(5, 2));
        assert_eq!(Ok(3), super::my_div(6, 2));
        assert_eq!(Err("divider must not 0"), super::my_div(6, 0));
    }
}

実行結果は次の通り。

% cargo test
   Compiling unit_test_hello_world v0.1.0 (/.../unit_test_hello_world)
    Finished test [unoptimized + debuginfo] target(s) in 0.32s
     Running unittests src/lib.rs (target/debug/deps/unit_test_hello_world-c10baac44515af32)

running 1 test
test tests::test_my_div ... ok

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

   Doc-tests unit_test_hello_world

running 0 tests

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?