본문 바로가기
Rust

[Rust] Testing 예제_2 (27일차)

by 꾸압 2023. 1. 9.

 

<예제 코드_1>

fn prints_and_returns_10(a: i32) -> i32 {
    println!("I got the vaue {}", a);
    10
}

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

    #[test]
    fn this_test_will_pass() {
        let value = prints_and_returns_10(4);
        assert_eq!(10, value);
    }

    #[test]
    fn this_test_will_fail() {
        let value = prints_and_returns_10(8);
        assert_eq!(5, value);
    }
}

 


 

<예제 코드_2>

pub fn add_two(a: i32) -> i32 {
    a + 2
}

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

    #[test]
    fn add_two_and_two() {
        assert_eq!(4, add_two(2));
    }

    #[test]
    fn add_three_and_two() {
        assert_eq!(5, add_two(3));
    }

    #[test]
    fn one_hundred() {
        assert_eq!(102, add_two(100));
    }
}

 


 

<예제 코드_3>

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

#[test]
#[ignore]
fn expensive_test() {
    // code that takes an hour to run
}

 


 

<예제 코드_3>

pub fn add_two(a: i32) -> i32 {
    internal_adder(a, 2)
}

fn internal_adder(a: i32, b: i32) -> i32 {
    a + b
}

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

    #[test]
    fn internal() {
        assert_eq!(4, internal_adder(2, 2));
    }
}

 


 

<참조 1> https://rinthel.github.io/rust-lang-book-ko/ch11-03-test-organization.html

<참조 2> https://rinthel.github.io/rust-lang-book-ko/ch11-03-test-organization.html

 

 

댓글