๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS

[iOS/Swift] XCTestCase ์ƒ์„ฑํ•˜๊ธฐ

by ํ‹ด๋”” 2023. 4. 13.
728x90
๋ฐ˜์‘ํ˜•

์ฐธ๊ณ  ์‚ฌ์ดํŠธ ๋ฐ ์ถœ์ฒ˜

์›๋ฌธ Kodeco์˜ iOS Unit Testing and UI Testing Tutorial์„ ๋ฒˆ์—ญํ•œ yoonbumatae์˜ ๊ธ€์„ ์ •๋ฆฌํ•œ ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•˜์‹œ๋ ค๋ฉด ์•„๋ž˜ ๋‘ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”!

https://www.kodeco.com/21020457-ios-unit-testing-and-ui-testing-tutorial

 

iOS Unit Testing and UI Testing Tutorial

Learn how to add unit tests and UI tests to your iOS apps, and how you can check on your code coverage.

www.kodeco.com

http://yoonbumtae.com/?p=4020 

 

Swift(์Šค์œ„ํ”„ํŠธ): iOS ๋‹จ์œ„ ํ…Œ์ŠคํŠธ(Unit test) ๋ฐ UI ํ…Œ์ŠคํŠธ ํŠœํ† ๋ฆฌ์–ผ - BGSMM

์›๋ฌธ iOS Unit Testing and UI Testing Tutorial   ๋ฒ„์ „ Swift 5, iOS 14, Xcode 12   iOS ๋‹จ์œ„ ํ…Œ์ŠคํŠธ(Unit test) ๋ฐ UI ํ…Œ์ŠคํŠธ ํŠœํ† ๋ฆฌ์–ผ iOS ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋Š” ๊ฑฐ์ฐฝํ•˜์ง€ ์•Š์ง€๋งŒ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ์•ฑ์ด

yoonbumtae.com

 

Unit Test & Class ์ƒ์„ฑ

  • navigator์—์„œ ๋‹ค์ด์•„๋ชฌ๋“œ ๋ชจ์–‘์˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ•˜๋‹จ์˜ + ๋ชจ์–‘์„ ๋ˆŒ๋Ÿฌ ์ƒˆ๋กœ์šด Unit Test์™€ Unit Test์— ํ•„์š”ํ•œ Class๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ

  • Target์˜ ์ด๋ฆ„์„ ์„ค์ •ํ•˜๊ณ  finish๋ฅผ ๋ˆ„๋ฅด๋ฉด TestNavigator์— ์ƒˆ๋กœ์šด Target์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

 

XCTestCase 

func setUpWithError() throws
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์‹œ์ž‘ ๋˜๊ธฐ ์ „์— ๋น„๋™๊ธฐ ์ƒํƒœ๋ฅผ ์ค€๋น„ํ•˜๊ธฐ ์œ„ํ•œ XCTest๋Š” setUp(completion:)์„ ํ˜ธ์ถœํ•จ ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ๋‚˜๋ฉด setUpWithError ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ทธ ๋’ค setUP()์„ ํ˜ธ์ถœ
  • state preparation์ด ์—๋Ÿฌ๋ฅผ ๋˜์งˆ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ ๊ทธ ๋•Œ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ ํ•  ์ˆ˜ ์žˆ๊ณ  XCTest๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ์‹œ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํŒจํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•จ 
  • ๋งŒ์•ฝ ์‹คํŒจ๊ฐ€ ์•„๋‹Œ ๊ฑด๋„ˆ ๋›ฐ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ (๋„คํŠธ์›Œํฌ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ƒํƒœ์—์„œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋งž์ง€ ์•Š์„ ๋•Œ ๋“ฑ) XCTSkip์„ ์‚ฌ์šฉํ•จ
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์„ค์ • ์ฝ”๋“œ๋ฅผ ์—ฌ๊ธฐ์— ์ž…๋ ฅํ•˜๊ณ  ๊ฐ ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „์— ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋จ

 

func tearDownWithError() throws
  • ๊ฐ ํ…Œ์ŠคํŠธ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด XCTest๋Š” tearDown()์„ ํ˜ธ์ถœํ•˜๊ณ  ๊ทธ ๋’ค tearDownWithError()๋ฅผ ํ˜ธ์ถœํ•จ. ๊ทธ ํ›„ TearDown(completion:) ์ด ํ˜ธ์ถœ ๋จ
  • ์ƒํƒœ ๊ฐ’์„ ์ง€์šฐ๋Š” ๊ณผ์ •์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ๋  ๊ฒฝ์šฐ tearDown(completion:) ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ ํ•˜์—ฌ ์‚ฌ์šฉํ•จ
  • XCTest๋Š” ์˜ค๋ฅ˜ ๋ฐœ์ƒํ•˜๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํŒจํ•œ ๊ฒƒ์œผ๋กœ ํ‘œ์‹œํ•œ๋‹ค. ๋งŒ์•ฝ ์‹คํŒจ๊ฐ€ ์•„๋‹Œ ๊ฑด๋„ˆ๋›ฐ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ XCTSkip์„ ๋ฐœ์ƒ์‹œํ‚ด
  • tearDown ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ. ๊ฐ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ ์ดํ›„์— ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ ๋จ
    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.
        // Any test you write for XCTest can be annotated as throws and async.
        // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
        // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
    }
  • ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜๋Š” test~ ๋กœ ์‹œ์ž‘ํ•˜๋„๋ก ์ž‘์„ฑํ•จ
  • ํ•ด๋‹น ํ•จ์ˆ˜ ์˜†์— ๋‹ค์ด์•„๋ชฌ๋“œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์‹คํ–‰๋จ

 

  func testPerformanceExample() throws {
    // This is an example of a performance test case.
    measure(metrics: [
      XCTClockMetric(), // ๊ฒฝ๊ณผ ์‹œ๊ฐ„ ์ธก์ •
      XCTCPUMetric(), // cpu ์‹œ๊ฐ„, ์ฃผ๊ธฐ ๋ฐ ๋ช…๋ น์–ด ์ˆ˜๋ฅผ ํฌํ•จํ•œ cpuํ™œ๋™ ์ถ”์ 
      XCTStorageMetric(), // ํ…Œ์ŠคํŠธ๋œ ์ฝ”๋“œ๊ฐ€ ์Šคํ† ๋ฆฌ์ง€์— ์“ฐ๋Š” ๋ฐ์ดํ„ฐ ์–‘
      XCTMemoryMetric() // ์‚ฌ์šฉ๋œ ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์–‘
    ]) {
      sut.check(guess: 100)
    }
  }

 

 

 

Photo by Edward Jenner from Pexels: https://www.pexels.com/photo/man-doing-a-sample-test-in-the-laboratory-4033148/

 
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€