ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

ios-og


๐Ÿ•น Unit Test(์œ ๋‹› ํ…Œ์ŠคํŠธ)


๐Ÿ™‹ Unit Test(์œ ๋‹› ํ…Œ์ŠคํŠธ)๋ž€?


  • TDD๋ผ๋Š” ๋ง์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜์ง€๋งŒ ์ง€๊ธˆ๊นŒ์ง€ ํ…Œ์ŠคํŠธ ๋‹ค์šด ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณธ ์ ์ด ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์‹œ์ž‘๋ถ€ํ„ฐ ํ…Œ์ŠคํŠธ๋ฅผ ๊ณ ๋ คํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋ ค๊ณ  ํ–ˆ๊ณ , ๊ทธ์— ๋”ฐ๋ผ Xcode์˜ Unit Test์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” Unit Test๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?


    "์œ ๋‹› ํ…Œ์ŠคํŠธ(unit test)๋Š” ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์†Œ์Šค ์ฝ”๋“œ์˜ ํŠน์ • ๋ชจ๋“ˆ์ด ์˜๋„๋œ ๋Œ€๋กœ ์ •ํ™•ํžˆ ์ž‘๋™ํ•˜๋Š”์ง€ ๊ฒ€์ฆํ•˜๋Š” ์ ˆ์ฐจ๋‹ค. ์ฆ‰, ๋ชจ๋“  ํ•จ์ˆ˜์™€ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค(Test case)๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ ˆ์ฐจ๋ฅผ ๋งํ•œ๋‹ค."

    ref: ์œ„ํ‚ค๋ฐฑ๊ณผ - ์œ ๋‹›ํ…Œ์ŠคํŠธ


    ๊ธฐ์กด์—๋Š” ์ฝ”๋”ฉ์„ ํ•˜๋ฉฐ ๊ฐ ๊ธฐ๋Šฅ์ด ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ธฐ ์œ„ํ•ด ์™„์„ฑ ํ›„ ์ž‘๋™์‹œ์ผœ๋ณด๊ณ , ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด print()๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‹€๋ฆฐ ์ง€์ ๋“ค์„ ์ถ”์ ํ•ด๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ ๋‹น์—ฐํžˆ ๊ฐ„ํŽธํ•˜๊ณ  ์ง๊ด€์ ์ด์ง€๋งŒ ํ”„๋กœ์ ํŠธ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก, ํ”„๋กœ๊ทธ๋žจ์˜ ๋ณต์žก์„ฑ์ด ๋Š˜์–ด๋‚ ์ˆ˜๋ก ์‚ฌ์šฉํ•˜๊ธฐ ํž˜๋“  ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์œ ๋‹› ํ…Œ์ŠคํŠธ๋ผ๋Š” ์œ ๋‹› ๋‹จ์œ„์˜ ํ…Œ์ŠคํŠธ, ์ฆ‰ ํ•˜๋‚˜์˜ ํฐ ๋ฉ์–ด๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋ผ ์ž‘์€ ๊ธฐ๋Šฅ ๋‹จ์œ„์˜ ํ…Œ์ŠคํŠธ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์‹คํ–‰ํ•ด๋ณผ ํ•„์š”๊ฐ€ ์žˆ๊ณ , ์ด๋ฅผ Xcode์—์„œ๋Š” Unit Test ๊ธฐ๋Šฅ์œผ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ’ป ์ด๋ฏธ ์กด์žฌํ•˜๋Š” Xcode ํ”„๋กœ์ ํŠธ์— Unit Test ๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•˜๊ธฐ


  • ๋งŒ์•ฝ ์ƒˆ๋กญ๊ฒŒ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋‹จ๊ณ„๋ผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์˜ต์…˜์„ ์ฒดํฌํ•˜์—ฌ ๊ฐ„๋‹จํžˆ ์œ ๋‹› ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.



  • ํ•˜์ง€๋งŒ ์ด๋ฏธ ์ƒ์„ฑ๋œ ํ”„๋กœ์ ํŠธ๋ผ๊ณ  Unit Test๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค! ์šฐ์„  ์•„๋ž˜์˜ ๊ฒฝ๋กœ๋กœ ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.


    File -> New -> Target -> Unit Testing Bundle



    ์œ„์—์„œ Next๋ฅผ ํด๋ฆญํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ • ํ™”๋ฉด์ด ๋‚˜์˜ค๊ณ  ์—ฌ๊ธฐ์—์„œ Finish๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ๋ฉด ํ…Œ์ŠคํŠธ์šฉ ์ƒˆ๋กœ์šด ํด๋”๊ฐ€ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.



    ์ด์ œ ํ…Œ์ŠคํŠธํŒŒ์ผ ์•ˆ์— ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ ๋ฉ”์†Œ๋“œ๋“ค์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


๐Ÿ“ ํ…Œ์ŠคํŠธ ํŒŒ์ผ ์•ˆ์˜ ์ž๋™ ์ƒ์„ฑ ๋ฉ”์†Œ๋“œ


  • setUp()


    override func setUpWithError() throws {
            // Put setup code here. This method is called before the invocation of each test method in the class.
        }

    ์นœ์ ˆํ•˜๊ฒŒ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑ์‹œ ๋ฉ”์†Œ๋“œ ์—ญํ• ์ด ์ ํ˜€์žˆ์Šต๋‹ˆ๋‹ค. override๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ๋ณด๋‹ˆ ์šฐ์„  ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์ˆ˜ํผํด๋ž˜์Šค์ธ XCTestCase์—์„œ ์ด๋ฏธ ์ •์˜๋˜์–ด์žˆ๋Š” ๋ฉ”์†Œ๋“œ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ํ•ด์„ํ•ด๋ณด๋ฉด setUpWithError์˜ ์—ญํ• ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    ์ด ๋ฉ”์†Œ๋“œ๋Š” ์ด ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์žˆ๋Š” ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์ „์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

    ๋‹ค์‹œ๋งํ•˜๋ฉด, ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ๋“ค์ด ํ˜ธ์ถœ๋˜๊ธฐ ์ „ ์ƒํƒœ๋ฅผ ์žฌ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ํ™˜๊ฒฝ ์ดˆ๊ธฐํ™”๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • tearDown()


    override func tearDownWithError() throws {
            // Put teardown code here. This method is called after the invocation of each test method in the class.
        }

    setUpWithError()๋ฉ”์†Œ๋“œ์™€ ๋‹ค๋ฅธ ์ ์€ ๋‹จ ํ•˜๋‚˜, ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ๋“ค์ด ์‹คํ–‰๋œ ํ›„ ๋ถˆ๋Ÿฌ์ง€๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ด '๋ฉ”์†Œ๋“œ๋“ค์ด ์‹คํ–‰๋œ ํ›„'๋ผ๋Š” ์กฐ๊ฑด์ด ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ์ค‘์˜์ ์œผ๋กœ ํ•ด์„๋  ์—ฌ์ง€๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

    1. ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋“ค์„ ์‹คํ–‰ํ•œ ํ›„ ๋งˆ์ง€๋ง‰์— ํ•œ ๋ฒˆ ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ
    2. ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋“ค์ด ๋๋‚  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ

    setUp ๋ฉ”์†Œ๋“œ์ฒ˜๋Ÿผ tearDown ์—ญ์‹œ ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ข…๋ฃŒ๋œ ํ›„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.


  • ์˜ˆ๋ฅผ ๋“ค์–ด, Mock ๊ฐ์ฒด๊ฐ€ ์žˆ์„ ๋•Œ ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ๊ฐ€ Mock์˜ ์ƒํƒœ ๋ณ€ํ™”๋ฅผ ์ผ์œผ์ผœ ๋‹ค๋ฅธ ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์ƒํ™ฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ๋‘ ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ๊ฐ€ ๋…๋ฆฝ์ ์œผ๋กœ ์‹œํ–‰๋  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค.


  • testExample(), testPerformanceExample()


    func testExample() throws {
            // This is an example of a functional test case.
            // Use XCTAssert and related functions to verify your tests produce the correct results.
        }
    
        func testPerformanceExample() throws {
            // This is an example of a performance test case.
            measure {
                // Put the code you want to measure the time of here.
            }
        }

    ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ, ๋‘ ๋ฉ”์†Œ๋“œ๋Š” Example(์˜ˆ์‹œ)์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๋ฉ”์†Œ๋“œ๋Š” ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์‹œ์ด๊ณ , ์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋Š” Performance ๋ฉ”์†Œ๋“œ์ด๋ฏ€๋กœ measure์ด๋ผ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ Closure Trailing์œผ๋กœ ๋˜์–ด์žˆ์–ด์„œ ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ›  Test Assertions


  • Test Assertions์˜ ์˜๋ฏธ๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด assert ๋ผ๋Š” ๋‹จ์–ด ๋œป์„ ์‚ฌ์ „์—์„œ ์ฐพ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    .[assert๏ผ‹ใ€ˆ๋ชฉใ€‰/assert๏ผ‹that์ ˆ] โ€ฆ์„ ์ฃผ์žฅํ•˜๋‹ค,๋‹จ์–ธ[์—ญ์„ค]ํ•˜๋‹ค, ์šฐ๊ธฐ๋‹ค; [assert๏ผ‹ใ€ˆ๋ชฉใ€‰๏ผ‹(to be)ใ€ˆ๋ณดใ€‰] โ€ฆ์ด ๏ผˆโ€ฆ์ด๋ผ๊ณ ๏ผ‰ ๋‹จ์–ธํ•˜๋‹ค

    ์ข…ํ•ฉํ•ด๋ณด๋ฉด, Test Assertions๋Š” 'ํ…Œ์ŠคํŠธ ์ฃผ์žฅ'(?) ์ด๋ผ๋Š” ์•Œ๊ธฐ ์–ด๋ ค์šด ์˜๋ฏธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๊ตณ์ด ์ถ”๊ฐ€ํ•œ๋‹ค๋ฉด 'ํ…Œ์ŠคํŠธ ์ฃผ์žฅ๋ฌธ' ์ •๋„๊ฐ€ ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ช…๋ช…์ด ๋ถ™์€ ์ด์œ ๋Š” ์—ฌ๊ธฐ์— ์†ํ•œ ๋„๊ตฌ๋“ค์„ ํ™•์ธํ•ด๋ณด๋ฉด ์•Œ๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.

  • Assertions์˜ ์ข…๋ฅ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ •์˜๋Š” ์• ํ”Œ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.

    1. Boolean Assertions: ์กฐ๊ฑด์ด ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€๋ฅผ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค(Test a condition that generates a true or false result).

      • XCTAssert()
      • XCTAssertTrue() -> XCTAssert()์™€ ๊ฑฐ์˜ ๊ฐ™์Œ
      • XCTAssertFalse()
    2. Nil and Non-Nil Assertions: ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ’์„ ๊ฐ–๋Š”์ง€ ์•ˆ๊ฐ–๋Š”์ง€๋ฅผ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค(Check whether a test condition has, or doesnโ€™t have, a value). ๋‹ค์‹œ๋งํ•˜๋ฉด Nil์ธ์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

      • XCTAssertNil()
      • XCTAssertNotNil()
      • XCTUnwrap()
    3. Equality and Inequality Assertions: ๋‘๊ฐœ์˜ ๊ฐ’์ด ๊ฐ™์€์ง€ ๋‹ค๋ฅธ์ง€๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค(Check whether two values are equal or unequal.).

      • XCTAssertEqual()
      • XCTAssertNotEqual()
      • XCTAssertIdentical()
      • XCTAssertNotIdentical()
      • ...
    4. Comparable Value Assertions: ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์–ด๋–ค ํ•˜๋‚˜์˜ ๊ฐ’์ด ๋‚˜๋จธ์ง€๋ณด๋‹ค ๋” ํฐ์ง€ ์ž‘์€์ง€๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค(Compare two values to determine whether one is larger or smaller than the other).

      • XCTAssertGreaterThan()
      • XCTAssertGreaterThanOrEqual()
      • XCTAssertLessThan()
      • XCTAssertLessThanOrEqual()
    5. Error Assertions: ํ•จ์ˆ˜๊ฐ€ error๋ฅผ ๋˜์ง€๋Š”์ง€ ์•ˆ๋˜์ง€๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค(Check whether a function call throws, or doesnโ€™t throw, an error).

      • XCTAssertThrowsError()
      • XCTAssertNoThrow()
    6. Unconditional Test Failures: ๋ฌด์กฐ๊ฑด ์ฆ‰์‹œ ์‹คํŒจํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค(Generate a failure immediately and unconditionally).

      • XCFail()
    7. Expected Failures: failing test๊ฐ€ ์ž‘์—…์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๊ฒŒํ•˜๊ธฐ ์œ„ํ•ด ํ…Œ์ŠคํŠธ ์‹คํŒจ๋ฅผ ๋ฏธ๋ฆฌ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋กœ์ง์ƒ ๋‹น์—ฐํžˆ ์•„์ง์€ ์‹คํŒจํ•ด์•ผํ•  ์ž‘์—…์— ๋Œ€ํ•œ ์„ค์ •์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      • XCTExpectFailure()
    8. Methods for Skipping Tests: ํŠน์ • ์กฐ๊ฑด์— ์ง„์ž…ํ•˜๊ธฐ ์œ„ํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ์Šคํ‚ตํ•ฉ๋‹ˆ๋‹ค(Skip tests when meeting specified conditions).

      • XCTSkipIf()
      • XCTSkipUnless()

ref: https://developer.apple.com/documentation/xctest

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๋ฐ˜์‘ํ˜•
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG more
ยซ   2025/02   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
๊ธ€ ๋ณด๊ด€ํ•จ