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 言語仕様09 Unit test

Last updated at Posted at 2022-03-12

続きまして、 Unit testについてです。

まずはコードから

前提として、unitテストコードが書いてあるファイルをmainファイル取り込んである状態です。

mod tests {
    use super::*; // 階層が違いますので、*で全て取得します。
    #[test] // Unitテストの時に書くおまじないです。
    fn test_a_is_larger() {
        let a = Rectangle {
            width: 5,
            height: 5,
        };
        let b = Rectangle {
            width: 3,
            height: 3,
        };
        assert!(a.compare_area(&b)); // assert!はtureと評価されているかどうか知りたいときにしようします。
    }

    #[test]
    fn test_a_is_smaller() {
        let a = Rectangle {
            width: 3,
            height: 3,
        };
        let b = Rectangle {
            width: 5,
            height: 5,
        };
        assert!(!(a.compare_area(&b)));
    }

    #[test]
    fn test_double() {
        assert_eq!(6, double_value(3)); // assert_eq!は等しいかどうか確かめる
    }

    #[test]
    fn test_contains_name() {
        let res = greeting("rust");
        assert!(res.contains("rust"));
    }
}

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?