๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Rust] Start Rust (Day 20) - Writing Automated Tests ๋ณธ๋ฌธ

Development Study/Rust

[Rust] Start Rust (Day 20) - Writing Automated Tests

f1r3_r41n 2023. 8. 19. 12:31
728x90
๋ฐ˜์‘ํ˜•

๐Ÿฆ€ Rust Day 20

๐Ÿณ๏ธ Writing Automated Tests

  • ๋Ÿฌ์ŠคํŠธ๋Š” ์ž๋™ํ™”๋œ ์†Œํ”„ํŠธ์›จ์–ด ํ…Œ์ŠคํŠธ์˜ ์ž‘์„ฑ์„ ์–ธ์–ด ์ฐจ์›์—์„œ ์ง€์›ํ•จ
  • ๊ฐœ๋ฐœ์ž๋Š” ํ•จ์ˆ˜์— ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ  ๊ฒฐ๊ณผ๊ฐ’์ด ์ž˜ ๋ฆฌํ„ด๋˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ
    • ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ๋งˆ๋‹ค ์ด ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์›๋ž˜ ์ž˜ ๋™์ž‘ํ•˜๋˜ ๊ธฐ๋Šฅ์ด ์—ฌ์ „ํžˆ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

1๏ธโƒฃ ํ…Œ์ŠคํŠธ์˜ ์ž‘์„ฑ

  • ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜์˜ ๋ณธ๋ฌธ์€ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•จ
    • 1 ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋‚˜ ์ƒํƒœ๋ฅผ ์„ค์ •
    • 2 ํ…Œ์ŠคํŠธํ•  ์ฝ”๋“œ๋ฅผ ์‹คํ–‰
    • 3 ์˜๋„ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š”์ง€ ๊ฒ€์ฆ

๐Ÿค” ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜ ์‚ดํŽด๋ณด๊ธฐ

  • ํ…Œ์ŠคํŠธ๋Š” test ํŠน์„ฑ์„ ์ ์šฉํ•œ ๋Ÿฌ์ŠคํŠธ ํ•จ์ˆ˜
    • ํŠน์„ฑ(attributes) : ๋Ÿฌ์ŠคํŠธ ์ฝ”๋“œ์— ๋Œ€ํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
      • (Ex) derive ํŠน์„ฑ
  • ํ•จ์ˆ˜๋ฅผ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜๋กœ ์ „ํ™˜ํ•˜๋ ค๋ฉด fn ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ ์œ„์— #[test] ํŠน์„ฑ์„ ์ ์šฉํ•˜๋ฉด ๋จ
  • cargo test ๋ช…๋ น์„ ์ด์šฉํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋Ÿฌ์ŠคํŠธ๋Š” test ํŠน์„ฑ์ด ์ ์šฉ๋œ ํ•จ์ˆ˜๋“ค์„ ์‹คํ–‰ํ•˜๋Š” ํ…Œ์ŠคํŠธ ์‹คํ–‰๊ธฐ(test runner) ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ๊ฐ ํ…Œ์ŠคํŠธ์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ๋ณด๊ณ ํ•จ
// src/lib.rs
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        let result = 2 + 2;
        assert_eq!(result, 4);
    }
}
  • cargo new ๋ช…๋ น์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•œ ํ…Œ์ŠคํŠธ ๋ชจ๋“ˆ๊ณผ ํ•จ์ˆ˜
  • #[test] : ํ•จ์ˆ˜๊ฐ€ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜์ž„์„ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ ํ…Œ์ŠคํŠธ ์‹คํ–‰๊ธฐ๋Š” ์ด ํŠน์„ฑ์„ ์ด์šฉํ•ด ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜์ธ์ง€๋ฅผ ํŒ๋‹จํ•จ
    • ํ…Œ์ŠคํŠธ ๋ชจ๋“ˆ ๋‚ด์—์„œ ๊ณตํ†ต์˜ ์‹œ๋‚˜๋ฆฌ์˜ค๋‚˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ผ๋ฐ˜ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ํ•ด๋‹น ํŠน์„ฑ์„ ์ด์šฉํ•ด ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜์ธ์ง€๋ฅผ ๋ฐ˜๋“œ์‹œ ํ‘œ๊ธฐํ•ด์•ผ ํ•จ
  • cargo test : ํ”„๋กœ์ ํŠธ ์•ˆ์˜ ๋ชจ๋“  ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น
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 adder

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
  • passed & failed : ์„ฑ๊ณตํ•œ ํ…Œ์ŠคํŠธ ๋ฐ ์‹คํŒจํ•œ ํ…Œ์ŠคํŠธ ๊ฐœ์ˆ˜
  • ignored : ๋ฌด์‹œํ•˜๋„๋ก ํ‘œ์‹œ๋œ ํ…Œ์ŠคํŠธ
  • filtered out : ํ…Œ์ŠคํŠธ์˜ ์‹คํ–‰์— ํ•„ํ„ฐ ์ ์šฉ ์œ ๋ฌด
  • measured : ์„ฑ๋Šฅ ์ธก์ •ํ•˜๋Š” ๋ฒค์น˜๋งˆํฌ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๊ฒƒ
  • Doc-tests : ๋ฌธ์„œ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ
// src/lib.rs
#[cfg(test)]
mod tests {
    #[test]
    fn exploration() {
        assert_eq!(2+2, 4);
    }

    #[test]
    fn another() {
        panic!("failed...");
    }
}
  • ํ…Œ์ŠคํŠธ๋Š” ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜์˜ ์–ด๋”˜๊ฐ€์—์„œ ํŒจ๋‹‰์ด ๋ฐœ์ƒํ•˜๋ฉด ์‹คํŒจํ•จ
    • ๊ฐ ํ…Œ์ŠคํŠธ๋Š” ์ƒˆ๋กœ์šด ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰๋˜๋ฉฐ ์ฃผ ์Šค๋ ˆ๋“œ๋Š” ์–ด๋–ค ํ…Œ์ŠคํŠธ ์Šค๋ ˆ๋“œ๊ฐ€ ์ค‘๋‹จ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๋ฉด ํ•ด๋‹น ํ…Œ์ŠคํŠธ๋Š” ์‹คํŒจํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•จ
running 2 tests
test tests::exploration ... ok
test tests::another ... FAILED

failures:

---- tests::another stdout ----
thread 'tests::another' panicked at 'failed...', src/lib.rs:10:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::another

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

error: test failed, to rerun pass '--lib'

๐Ÿค” assert! ๋งคํฌ๋กœ๋ฅผ ์ด์šฉํ•ด ๊ฒฐ๊ณผ ํ™•์ธํ•˜๊ธฐ

  • assert! : ํ…Œ์ŠคํŠธ์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ํ‰๊ฐ€ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ํ™œ์šฉํ•˜๋Š” ๋งคํฌ๋กœ
    • ๋ถˆ๋ฆฌ์–ธ์œผ๋กœ ํ‰๊ฐ€๋˜๋Š” ํ‘œํ˜„์‹์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋จ
    • ํ•ด๋‹น ๋งคํฌ๋กœ์— true ๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ณ„๋‹ค๋ฅธ ์ถ”๊ฐ€ ๋™์ž‘ ์—†์ด ํ…Œ์ŠคํŠธ ์„ฑ๊ณต
    • ์ „๋‹ฌ๋œ ๊ฐ’์ด false ์ผ ๊ฒฝ์šฐ ํŒจ๋‹‰์ด ๋ฐœ์ƒํ•˜์—ฌ ํ…Œ์ŠคํŠธ์— ์‹คํŒจํ•จ
// src/lib.rs
#[derive(Debug)]
pub struct Rectangle {
    length: u32,
    width: u32,
}

impl Rectangle {
    fn can_hold(&self, other: &Rectangle) -> bool {
        self.length > other.length && self.width > other.width
    }
}

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

    #[test]
    fn larger_can_hold_smaller() {
        let larger = Rectangle { length: 8, width: 7 };
        let smaller = Rectangle { length: 5, width: 1 };
        assert!(larger.can_hold(&smaller));
    }

    #[test]
    fn larger_cannot_hold_smaller() {
        let larger = Rectangle { length: 8, width: 7 };
        let smaller = Rectangle { length: 5, width: 1 };
        assert!(!smaller.can_hold(&larger));
    }
}
  • use super::*; : tests ๋ชจ๋“ˆ์€ ์ค‘์ฒฉ๋œ ๋ชจ๋“ˆ์ด๋ฏ€๋กœ ๋ฐ”๊นฅ ๋ชจ๋“ˆ์˜ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ค‘์ฒฉ๋œ ๋ชจ๋“ˆ์˜ ๋ฒ”์œ„๋กœ ์˜ฎ๊ฒจ์™€์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— * ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๊นฅ ๋ชจ๋“ˆ์— ์ •์˜ํ•œ ๋ชจ๋“  ํƒ€์ž…์„ ๋ฒ”์œ„๋กœ ๊ฐ€์ ธ์˜ด
running 2 tests
test tests::larger_can_hold_smaller ... ok
test tests::larger_cannot_hold_smaller ... ok

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

๐Ÿค” assert_eq! ์™€ assert_ne! ๋งคํฌ๋กœ๋ฅผ ์ด์šฉํ•œ ๋™๋“ฑ ๋น„๊ต ํ…Œ์ŠคํŠธ

  • assert_eq! & assert_ne! : ๊ฐ๊ฐ ๋‘ ๊ฐœ์˜ ์ธ์ˆ˜๊ฐ€ ๊ฐ™์€์ง€ ํ˜น์€ ๋‹ค๋ฅธ์ง€๋ฅผ ๋น„๊ตํ•จ
    • ๊ฒ€์ฆ์ด ์‹คํŒจํ•˜๋ฉด ์™œ ์‹คํŒจํ–ˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ๋‘ ๊ฐ’์„ ์ถœ๋ ฅํ•ด ์คŒ
    • ๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ๊ฐ == ์™€ != ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•จ
      • ๋งคํฌ๋กœ์— ์ „๋‹ฌ๋˜๋Š” ๊ฐ’๋“ค์€ PartialEq ์™€ Debug ํŠธ๋ ˆ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ
        • ๋ชจ๋“  ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•๊ณผ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ํƒ€์ž…๋“ค์€ ํ•ด๋‹น ํŠธ๋ ˆ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์Œ
        • ์ง์ ‘ ์„ ์–ธํ•œ ๊ตฌ์กฐ์ฒด์™€ ์—ด๊ฑฐ์ž๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ํ•ด๋‹น ํŠธ๋ ˆ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ๋™๋“ฑ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Œ
        • ๋‘ ํŠธ๋ ˆ์ดํŠธ๋Š” ์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๊ตฌ์กฐ์ฒด๋‚˜ ์—ด๊ฑฐ์ž๋ฅผ ์„ ์–ธ ์‹œ #[derive(PartialEq, Debug)] ์• ๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด ์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋จ
// src/lib.rs
pub fn add_two(a: i32) -> i32 {
    a+2
}

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

    #[test]
    fn it_add_two() {
        assert_eq!(4, add_two(2));
    }
}
running 1 test
test tests::it_add_two ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
// src/lib.rs
pub fn add_two(a: i32) -> i32 {
    a+3
}

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

    #[test]
    fn it_add_two() {
        assert_eq!(4, add_two(2));
    }
}
running 1 test
test tests::it_add_two ... FAILED

failures:

---- tests::it_add_two stdout ----
thread 'tests::it_add_two' panicked at 'assertion failed: `(left == right)`
  left: `4`,
 right: `5`', src/lib.rs:11:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::it_add_two

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

๐Ÿค” ์‚ฌ์šฉ์ž ์ •์˜ ์‹คํŒจ ๋ฉ”์‹œ์ง€

  • assert! & assert_eq! & assert_ne! ๋งคํฌ๋กœ์˜ ์„ ํƒํ˜• ์ธ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์‹คํŒจ ๋ฉ”์‹œ์ง€์— ์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ
    • ํ•„์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‹ค์Œ ํ˜•์‹ํ™”๋œ ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Œ
// src/lib.rs
pub fn greeting(name: &str) -> String {
    format!("Hi, {}!", name)
}

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

    #[test]
    fn greeting_contains_name() {
        let r = greeting("ํ™”๋น„");
        assert!(
            r.contains("ํžˆํžˆ"),
            "ํ•จ์ˆ˜ ๊ฒฐ๊ณผ์— ์ด๋ฆ„์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์Œ.\n๊ฒฐ๊ด๊ฐ’: {}", r
        );
    }
}
running 1 test
test tests::greeting_contains_name ... FAILED

failures:

---- tests::greeting_contains_name stdout ----
thread 'tests::greeting_contains_name' panicked at 'ํ•จ์ˆ˜ ๊ฒฐ๊ณผ์— ์ด๋ฆ„์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์Œ.
๊ฒฐ๊ด๊ฐ’: Hi, ํ™”๋น„!', src/lib.rs:12:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::greeting_contains_name

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

๐Ÿค” should_panic ๋งคํฌ๋กœ๋ฅผ ์ด์šฉํ•ด ํŒจ๋‹‰์˜ ๋ฐœ์ƒ ์—ฌ๋ถ€ ๊ฒ€์ฆํ•˜๊ธฐ

  • should_panic ํŠน์„ฑ : ํ•จ์ˆ˜ ๋‚ด์˜ ์ฝ”๋“œ๊ฐ€ ํŒจ๋‹‰์ด ๋ฐœ์ƒํ•˜๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ์„ฑ๊ณตํ•˜๊ณ  ํŒจ๋‹‰์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด ์‹คํŒจํ•˜๋„๋ก ํ•จ
    • ๋‹จ์ˆœํžˆ ํŒจ๋‹‰์ด ๋ฐœ์ƒํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ๋งŒ์„ ์ง€์‹œํ•˜๋ฏ€๋กœ ๋‹ค์†Œ ๋ชจํ˜ธํ•จ
    • ์˜๋„์™€๋Š” ๋‹ค๋ฅธ ์›์ธ์œผ๋กœ ํŒจ๋‹‰์ด ๋ฐœ์ƒํ•ด๋„ ์„ฑ๊ณตํ•จ
    • ํ•ด๋‹น ํŠน์„ฑ์ด ์ ์šฉ๋œ ํ…Œ์ŠคํŠธ์˜ ๋™์ž‘์„ ์กฐ๊ธˆ ๋” ์ •ํ™•ํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ์„ ํƒํ˜• ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋จ
// src/lib.rs
pub struct Guess {
    v: u32,
}

impl Guess {
    pub fn new(v: u32) -> Guess {
        if v < 1 {
            panic!(
                "๋ฐ˜๋“œ์‹œ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฐ’ ์‚ฌ์šฉ. ์ง€์ •๊ฐ’: {}", v
            );
        } else if v > 100 {
            panic!(
                "๋ฐ˜๋“œ์‹œ 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฐ’ ์‚ฌ์šฉ. ์ง€์ •๊ฐ’: {}", v
            );
        }

        Guess { v }
    }
}

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

    #[test]
    #[should_panic(expected = "๋ฐ˜๋“œ์‹œ 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฐ’ ์‚ฌ์šฉ.")]
    fn greater_than_100() {
        Guess::new(200);
    }
}
  • Guess::new ํ•จ์ˆ˜๊ฐ€ ํŒจ๋‹‰์„ ๋ฐœ์ƒํ•  ๋•Œ ํ•จ๊ป˜ ์ถœ๋ ฅํ•  ๋ฉ”์‹œ์ง€์˜ ์ผ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•˜๋ฏ€๋กœ ๋ฌด์‚ฌํžˆ ์„ฑ๊ณตํ•จ
  • ํ•ด๋‹น ํŠน์„ฑ์˜ expected ๋ฉ”์‹œ์ง€๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ํ…Œ์ŠคํŠธ๊ฐ€ ์‹คํŒจํ•จ
running 1 test
test tests::greater_than_100 - should panic ... ok

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

2๏ธโƒฃ ํ…Œ์ŠคํŠธ ์‹คํ–‰ ์ œ์–ดํ•˜๊ธฐ

  • cargo test ๋ช…๋ น์˜ ๊ธฐ๋ณธ ๋™์ž‘์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด ๋ช…๋ น์ค„ ์˜ต์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Œ
    • ๋ช…๋ น์ค„ ์˜ต์…˜์˜ ์ผ๋ถ€๋Š” cargo test ๋ช…๋ น์— ์ ์šฉ๋˜๋ฉด ๋‚˜๋จธ์ง€๋Š” ๊ฒฐ๊ณผ ํ…Œ์ŠคํŠธ ๋ฐ”์ด๋„ˆ๋ฆฌ์˜ ์ƒ์„ฑ์— ์ ์šฉ๋จ
    • ์˜ต์…˜ ์ ์šฉ ์‹œ ์ด ๋‘ ์ข…๋ฅ˜์˜ ์˜ต์…˜์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ๋ช…๋ น์— ์ ์šฉ๋˜๋Š” ์ธ์ˆ˜๋“ค์„ ๋‚˜์—ดํ•˜๊ณ  ํ…Œ์ŠคํŠธ ๋ฐ”์ด๋„ˆ๋ฆฌ์— ์ ์šฉ๋˜๋Š” ์˜ต์…˜๋“ค์€ ๊ตฌ๋ถ„์ž -- ๋‹ค์Œ์— ๋‚˜์—ดํ•˜๋ฉด ๋จ

๐Ÿค” ํ…Œ์ŠคํŠธ๋ฅผ ๋ณ‘๋ ฌ ํ˜น์€ ์ง๋ ฌ๋กœ ์‹คํ–‰ํ•˜๊ธฐ

  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์‹คํ–‰ ์‹œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์Šค๋ ˆ๋“œ(thread) ๋ฅผ ์ด์šฉํ•ด ๋ณ‘๋ ฌ(parallel) ๋กœ ์‹คํ–‰ํ•จ
    • ์‹ ์†ํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ ํ…Œ์ŠคํŠธ๊ฐ€ ๋™์‹œ์— ์‹คํ–‰๋˜๋ฏ€๋กœ ๊ฐ ํ…Œ์ŠคํŠธ๋Š” ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋‚˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋“ฑ ๊ณตํ†ต์ ์œผ๋กœ ์ ์šฉ๋˜๋Š” ํ™˜๊ฒฝ์„ ํฌํ•จํ•œ ๊ณต์œ  ์ƒํƒœ(shared state) ์— ๋Œ€ํ•ด ๋…๋ฆฝ์ ์ด์–ด์•ผ ํ•จ
  • ํ…Œ์ŠคํŠธ๋ฅผ ๋ณ‘๋ ฌ๋กœ ์‹คํ–‰ํ•˜๊ธฐ๋ฅผ ์›์น˜ ์•Š๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•  ์Šค๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์กฐ๊ธˆ ๋” ์ •๋ฐ€ํ•˜๊ฒŒ ์ œ์–ดํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด --test-threads ํ”Œ๋ž˜๊ทธ์— ํ…Œ์ŠคํŠธ ๋ฐ”์ด๋„ˆ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ์Šค๋ ˆ๋“œ์˜ ์ˆซ์ž๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋จ
    • cargo test -- --test-threads=1
      • ํ…Œ์ŠคํŠธ ์Šค๋ ˆ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ 1๋กœ ์„ค์ •
      • ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๋งŒ์„ ์ด์šฉํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ณ‘๋ ฌ๋กœ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์‹คํ–‰ ์‹œ๊ฐ„์€ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€๋งŒ, ํ…Œ์ŠคํŠธ ๊ฐ„์˜ ๊ฐ„์„ญ์„ ์—†์•จ ์ˆ˜ ์žˆ์Œ

๐Ÿค” ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ ๋ณด์—ฌ์ฃผ๊ธฐ

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ํ…Œ์ŠคํŠธ์— ์„ฑ๊ณตํ•˜๋ฉด ๋Ÿฌ์ŠคํŠธ์˜ ํ…Œ์ŠคํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํ‘œ์ค€ ์ถœ๋ ฅ์— ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅํ•˜์ง€ ์•Š์Œ
    • ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜ ๋‚ด์—์„œ println! ๋งคํฌ๋กœ๋ฅผ ํ˜ธ์ถœํ•ด๋„ ํ…Œ์ŠคํŠธ๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด ์ถœ๋ ฅ์„ ํ„ฐ๋ฏธ๋„์—์„œ ํ™•์ธํ•  ์ˆ˜ ์—†๊ณ  ๋‹จ์ง€ ํ…Œ์ŠคํŠธ๊ฐ€ ์„ฑ๊ณตํ–ˆ๋‹ค๋Š” ๋ฉ”์‹œ์ง€๋งŒ ๋ณด์ž„
    • ํ…Œ์ŠคํŠธ ์‹คํŒจ ์‹œ ํ…Œ์ŠคํŠธ ์‹คํŒจ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ์ถœ๋ ฅํ•œ ๋‚ด์šฉ๋„ ํ™•์ธ ๊ฐ€๋Šฅ
  • ์„ฑ๊ณตํ•œ ํ…Œ์ŠคํŠธ์˜ ์ถœ๋ ฅ๊ฐ’๋„ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด --nocapture ํ”Œ๋ž˜๊ทธ๋ฅผ ์ง€์ •ํ•ด ์ถœ๋ ฅ ๊ฐ€๋กœ์ฑ„๊ธฐ ๋™์ž‘์„ ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Œ
    • cargo test -- --nocapture

๐Ÿค” ์ด๋ฆ„์„ ์ด์šฉํ•ด ํ…Œ์ŠคํŠธ ์ผ๋ถ€๋งŒ ์‹คํ–‰ํ•˜๊ธฐ

  • cargo test ๋ช…๋ น์— ์‹คํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ํ…Œ์ŠคํŠธ์˜ ์ด๋ฆ„์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ด์„œ ์›ํ•˜๋Š” ํ…Œ์ŠคํŠธ๋งŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ
    • cargo test [test_name]
      • ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ๋งŒ ์‹คํ–‰
    • cargo test [keyword]
      • ํ…Œ์ŠคํŠธ ์ด๋ฆ„์˜ ์ผ๋ถ€๋งŒ ํ‚ค์›Œ๋“œ๋กœ ์ง€์ • ์‹œ ์ง€์ •๋œ ํ‚ค์›Œ๋“œ์— ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์‹คํ–‰

๐Ÿค” ์ด๋ฆ„์„ ์ด์šฉํ•ด ํ…Œ์ŠคํŠธ ์ผ๋ถ€๋งŒ ์‹คํ–‰ํ•˜๊ธฐ

  • #[test] ๋‹ค์Œ์— #[ignore] ํŠน์„ฑ ์ ์šฉ ์‹œ ํ•ด๋‹น ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜ ์‹คํ–‰์—์„œ ์ œ์™ธ ๊ฐ€๋Šฅ
  • cargo test -- --ignored
    • ์ œ์™ธ๋œ ํ…Œ์ŠคํŠธ๋งŒ์„ ์‹คํ–‰

3๏ธโƒฃ ํ…Œ์ŠคํŠธ์˜ ์กฐ์งํ™”

  • ๋‹จ์œ„ ํ…Œ์ŠคํŠธ(unit tests) : ์ž‘๊ณ  ์ง‘์ค‘์ ์ด๋ฉฐ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ๋ชจ๋“ˆ์„ ๋…๋ฆฝ์ ์œผ๋กœ ํ…Œ์ŠคํŠธํ•˜๊ณ  ๋น„๊ณต๊ฐœ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ…Œ์ŠคํŠธํ•  ์ˆ˜๋„ ์žˆ์Œ
  • ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ(integration tests) : ์™„์ „ํžˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์™ธ์˜ ๊ฒƒ์„ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ
    • ์™ธ๋ถ€ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ๊ณต๊ฐœ ์ธํ„ฐํŽ˜์ด์Šค๋งŒ์„ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๊ณ  ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ชจ๋“ˆ์„ ํ™œ์šฉํ•  ์ˆ˜๋„ ์žˆ์Œ

๐Ÿค” ๋‹จ์œ„ ํ…Œ์ŠคํŠธ

  • ์ฝ”๋“œ์˜ ๊ฐ ๋‹จ์œ„๋ฅผ ๋‚˜๋จธ์ง€ ์ฝ”๋“œ์™€๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ํ…Œ์ŠคํŠธํ•ด์„œ ์ฝ”๋“œ๊ฐ€ ์˜๋„๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”์ง€๋ฅผ ๋น ๋ฅด๊ฒŒ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ
  • src ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๊ฐ ํŒŒ์ผ์— ํ…Œ์ŠคํŠธํ•  ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์ž‘์„ฑ
    • ๊ฐ ํŒŒ์ผ์— tests ๋ผ๋Š” ๋ชจ๋“ˆ์„ cfg(test) ๋ผ๋Š” ํŠน์„ฑ๊ณผ ํ•จ๊ป˜ ์„ ์–ธํ•˜๊ณ , ๊ทธ ์•ˆ์— ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜๋“ค์„ ์ž‘์„ฑ

(1) Tests ๋ชจ๋“ˆ๊ณผ #[cfg(test)] ํŠน์„ฑ

  • tests ๋ชจ๋“ˆ์˜ #[cfg(test)] ํŠน์„ฑ์€ ๋Ÿฌ์ŠคํŠธ์—๊ฒŒ ์ด ๋ชจ๋“ˆ์€ cargo test ๋ช…๋ น์„ ์‹คํ–‰ํ•  ๋•Œ๋งŒ ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•˜๊ณ  ์‹คํ–‰ํ•˜๋ผ๊ณ  ์ง€์‹œํ•˜๋Š” ๊ฒƒ
    • ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๊ฐ€ ์ปดํŒŒ์ผ ๊ฒฐ๊ณผ์— ํฌํ•จ๋˜์ง€ ์•Š๋„๋ก ๊ผญ ์ง€์ •ํ•ด์•ผ ํ•จ
    • ์ปดํŒŒ์ผ ๋˜๋Š” ์ฝ”๋“œ์—๋Š” #[test] ํŠน์„ฑ์ด ์ ์šฉ๋œ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜๋Š” ๋ฌผ๋ก  ๋ชจ๋“ˆ ๋‚ด์˜ ๋„์›€ ํ•จ์ˆ˜(helper functions) ๋“ค๋„ ํฌํ•จ๋จ
  • cfg ํŠน์„ฑ : ์„ค์ •(configuration) ์˜ ์•ฝ์ž
    • ๋Ÿฌ์ŠคํŠธ์—๊ฒŒ ์ดํ›„์˜ ์ฝ”๋“œ๋Š” ํŠน์ • ์„ค์ • ์˜ต์…˜์ด ์ง€์ •๋˜์—ˆ์„ ๋•Œ๋งŒ ํฌํ•จํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์คŒ

(2) ๋น„๊ณต๊ฐœ ํ•จ์ˆ˜์˜ ํ…Œ์ŠคํŠธ

  • ๋‹ค๋ฅธ ์–ธ์–ด๋Š” ๋น„๊ณต๊ฐœ ํ•จ์ˆ˜์˜ ํ…Œ์ŠคํŠธ๊ฐ€ ๋งค์šฐ ์–ด๋ ต๊ฑฐ๋‚˜ ์•„์˜ˆ ๋ถˆ๊ฐ€๋Šฅํ•จ
  • ๋Ÿฌ์ŠคํŠธ์˜ ๋น„๊ณต๊ฐœ ๊ทœ์น™์€ ๋น„๊ณต๊ฐœ ํ•จ์ˆ˜์˜ ํ…Œ์ŠคํŠธ๋ฅผ ํ—ˆ์šฉํ•จ
// src/lib.rs
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));
    }
}
running 1 test
test tests::internal ... ok

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

๐Ÿค” ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ

  • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์˜์—ญ ๋ฐ”๊นฅ์—์„œ ์ง„ํ–‰๋จ
  • ๋‹ค๋ฅธ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ
    • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ณต๊ฐœ API๋งŒ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋œป
  • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ
  • ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด tests ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•จ

(1) tests ๋””๋ ‰ํ„ฐ๋ฆฌ

  • tests ๋””๋ ‰ํ„ฐ๋ฆฌ๋Š” src ๋””๋ ‰ํ„ฐ๋ฆฌ์ฒ˜๋Ÿผ ์ตœ์ƒ์œ„ ์ˆ˜์ค€์— ์ƒ์„ฑ
    • ์นด๊ณ ๋Š” ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ํŒŒ์ผ์„ ํƒ์ƒ‰
    • ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ ์•ˆ์— ์›ํ•˜๋Š” ๋งŒํผ์˜ ํ…Œ์ŠคํŠธ ํŒŒ์ผ์„ ์ƒ์„ฑ
    • ๊ฐ ํŒŒ์ผ์„ ๋ณ„๊ฐœ์˜ ํฌ๋ ˆ์ดํŠธ๋กœ ์ปดํŒŒ์ผ
// tests/integration_test.rs
use adder;

#[test]
fn it_adds_two() {
    assert_eq!(4, adder::add_two(2));
}
  • tests ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๊ฐ ํ…Œ์ŠคํŠธ๋Š” ๋ณ„๊ฐœ์˜ ํฌ๋ ˆ์ดํŠธ๋กœ ์ปดํŒŒ์ผ๋˜๋ฏ€๋กœ ๊ฐ ํŒŒ์ผ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•จ
  • #[cfg(tests)] ํŠน์„ฑ์„ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Œ
    • ์นด๊ณ ๋Š” tests ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ํŠน๋ณ„ํžˆ ๋‹ค๋ฃจ๋ฉฐ, ์ด ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ํŒŒ์ผ๋“ค์€ cargo test ๋ช…๋ น์„ ์‹คํ–‰ํ•  ๋•Œ๋งŒ ์ปดํŒŒ์ผํ•จ
running 1 test
test tests::internal ... ok

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

     Running tests/integration_test.rs (target/debug/deps/integration_test-7c809cc164d12514)

running 1 test
test it_adds_two ... ok

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

   Doc-tests adder

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
  • cargo test ๋ช…๋ น ์‹คํ–‰ ๊ฒฐ๊ณผ
  • ๋‹จ์œ„ ํ…Œ์ŠคํŠธ, ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ, ๋ฌธ์„œ ํ…Œ์ŠคํŠธ ๋“ฑ ์„ธ ๊ฐœ์˜ ์„น์…˜์ด ์ถœ๋ ฅ๋จ
running 1 test
test it_adds_two ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
  • cargo test --test integration_test ๋ช…๋ น ์‹คํ–‰ ๊ฒฐ๊ณผ
  • ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๋ฉด ํŠน์ • ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋งŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ

(2) ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ์˜ ์„œ๋ธŒ ๋ชจ๋“ˆ

  • ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ์˜ ์–‘์ด ๋Š˜์–ด๋‚˜๋ฉด tests ๋””๋ ‰ํ„ฐ๋ฆฌ์— ํ•˜๋‚˜ ์ด์ƒ์˜ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌ์กฐ์ ์œผ๋กœ ์œ ์ง€ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋จ
  • tests ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์„œ๋ธŒ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ž‘์„ฑํ•œ ํŒŒ์ผ๋“ค์€ ๋ณ„๊ฐœ์˜ ํฌ๋ ˆ์ดํŠธ๋กœ ์ปดํŒŒ์ผ๋˜์ง€ ์•Š์œผ๋ฉฐ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์— ๋ณ„๊ฐœ์˜ ์„น์…˜์œผ๋กœ ๋‚˜ํƒ€๋‚˜์ง€๋„ ์•Š์Œ
  • ์˜ˆ๋ฅผ ๋“ค์–ด, tests/common/mod.rs ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ํ›„ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ํŒŒ์ผ์—์„œ ๋ชจ๋“ˆ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋จ

(3) ๋ฐ”์ด๋„ˆ๋ฆฌ ํฌ๋ ˆ์ดํŠธ์˜ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ

  • ํ˜„์žฌ ์ง„ํ–‰ ์ค‘์ธ ํ”„๋กœ์ ํŠธ๊ฐ€ src/lib.rs ํŒŒ์ผ์ด ์•„๋‹Œ src/main.rs ํŒŒ์ผ์„ ๊ฐ€์ง„ ๋ฐ”์ด๋„ˆ๋ฆฌ ํฌ๋ ˆ์ดํŠธ๋ผ๋ฉด tests ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ด src/main.rs ํŒŒ์ผ์˜ ํ•จ์ˆ˜๋ฅผ use ๋ฌธ์œผ๋กœ ๊ฐ€์ ธ์™€ ํ…Œ์ŠคํŠธํ•˜๋Š” ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์—†์Œ
    • ์˜ค์ง ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํฌ๋ ˆ์ดํŠธ๋งŒ ํ•จ์ˆ˜๋ฅผ ์™ธ๋ถ€์— ๋…ธ์ถœํ•ด์„œ ๋‹ค๋ฅธ ํฌ๋ ˆ์ดํŠธ๊ฐ€ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Œ
    • ๋ฐ”์ด๋„ˆ๋ฆฌ ํฌ๋ ˆ์ดํŠธ๋Š” ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์‹คํ–‰ํ•  ๋ฟ์ž„
  • src/lib.rs ํŒŒ์ผ์— ์ž‘์„ฑ๋œ ๋กœ์ง์„ src/main.rs ํŒŒ์ผ์—์„œ use ๋ฌธ์„ ์ด์šฉํ•ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํฌ๋ ˆ์ดํŠธ์˜ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•˜์—ฌ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•จ

Summary

  • ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์„ ๊ฐœ๋ณ„์ ์œผ๋กœ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ์€ ๋ฌผ๋ก  ๋น„๊ณต๊ฐœ ํ•จ์ˆ˜๋„ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Œ
  • ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋‹ค์–‘ํ•œ ๋ถ€๋ถ„์ด ๋‹ค๋ฅธ ์ฝ”๋“œ์™€ ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ๊ฒ€์ฆํ•˜๋ฉฐ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ณต์šฉ API๋ฅผ ์™ธ๋ถ€ ์ฝ”๋“œ๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ…Œ์ŠคํŠธํ•จ
  • ํ…Œ์ŠคํŠธ๋Š” ๋กœ์ง์˜ ๋ฒ„๊ทธ๋ฅผ ์ค„์ด๊ณ  ์ฝ”๋“œ๊ฐ€ ์˜๋„๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ๊ฒ€์ฆํ•˜๋Š” ๋ฐ ๋งค์šฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•จ
728x90
๋ฐ˜์‘ํ˜•
Comments