관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] 1. Quick, Nimble으로 테스트 쉽게하는 방법 - Quick, Nimble 개념 (#BDD, #Test Suite, 테스트 스위트) 본문

iOS 응용 (swift)

[iOS - swift] 1. Quick, Nimble으로 테스트 쉽게하는 방법 - Quick, Nimble 개념 (#BDD, #Test Suite, 테스트 스위트)

jake-kim 2023. 6. 24. 17:02

1. Quick, Nimble으로 테스트 쉽게하는 방법 - Quick, Nimble 개념

2. Quick, Nimble으로 테스트 쉽게하는 방법 - describe, context, it, beforeEach 실제 코드에 테스트하는 방법 (RxNimble, 비동기 테스트 방법)

Nimble이란?

* Nimble (재빠른)

  • 가독성 높은 테스트 assertion 라이브러리
  • 예상한 결과와 실제 결과를 비교하여 테스트를 수행하고, 테스트가 성공한 경우 상세한 정보를 제공
  • 글을 읽는 것처럼 쉽게 읽히는 assertion들을 제공
    • 대표적으로 expect()와 뒤에 to, toNot, notTo 등이 존재
import Nimble

expect(seagull.squawk).to(equal("Squee!"))
expect(seagull.squawk).toNot(equal("Oh, hello there!"))
expect(seagull.squawk).notTo(equal("Oh, hello there!"))
  • 오류 메시지도 어떤 부분이 잘못되었는지 쉽게 제공
expect(1 + 1).to(equal(3))
// failed - expected to equal <3>, got <2>

expect(1 + 1).to(equal(3), description: "Make sure libKindergartenMath is loaded")
// failed - Make sure libKindergartenMath is loaded
// expected to equal <3>, got <2>
  • toEventually나 toEventuallyNot을 사용하면 async한 기능도 쉽게 테스트가 가능
DispatchQueue.main.async {
    ocean.add("dolphins")
    ocean.add("whales")
}
expect(ocean).toEventually(contain("dolphins", "whales"))
  • 이밖의 유용한 연산자들
// Passes if 'actual' contains 'substring':
expect(actual).to(contain(substring))

// Passes if 'actual' begins with 'prefix':
expect(actual).to(beginWith(prefix))

// Passes if 'actual' ends with 'suffix':
expect(actual).to(endWith(suffix))

// Passes if 'actual' represents the empty string, "":
expect(actual).to(beEmpty())

// Passes if 'actual' matches the regular expression defined in 'expected':
expect(actual).to(match(expected))

Quick이란?

  • Quick은 Nimble과 함께 사용되는 BDD(Behavior-Driven Development) 스타일의 테스트 프레임워크
    • * BDD 방식: 앱의 동작(behavior)에 초점을 맞추고 어떻게 동작할 것인지 기대 결과를 테스트하며 개발하는 방식
  • 테스트를 그룹화하여 더 작은 테스트 케이스로 분할하며 테스팅
  • 테스트를 더 구조화된 방식으로 작성하여 테스트의 구성요소를 더 쉽게 파악할 수 있음

ex) Quick 사용 방법

  • 원래 Quick을 사용하지 않으면, 테스트 클래스에서 XCTestCase를 상속 받지만 Quick을 사용하면 QuickSpec을 상속
import XCTest
@testable import ExTesting

final class ExTestingTests: XCTestCase {
    override func setUpWithError() throws {
    }

    override func tearDownWithError() throws {
    }

    func testExample() throws {
    }

    func testPerformanceExample() throws {
        self.measure {
        }
    }
}

(Quick은 QuickSpec을 상속)

  • describe, context, it 함수의 클로저로 감싸며 테스트
    • describe: *테스트 스위트(Test Suite)를 그룹화하는 역할 (주로 테스트하려는 기능이나 특정 모듈에 대한 설명을 포함)
    • context: 테스트 케이스를 더 구체적인 상황이나 조건에 따라 그룹화하는 역할
    • it: 개별적인 테스트 케이스를 작성하는 곳
import Quick
import Nimble

class TableOfContentsSpec: QuickSpec {
    override class func spec() {
        describe("등록 모듈에서") {
            context("유저가 로그인하면") {
                it("검증 기능 수행") {
                    let you = You(awesome: true)
                    expect{you.submittedAnIssue}.toEventually(beTruthy())
                }
            }
        }
    }
}

Test Suite

  • 테스트 케이스의 집합
  • 여러 테스트를 하나로 묶어주는 개념이며, 테스트를 구성하고 조직화하는데 사용
  • Quickd에서는 이러한 테스트 케이스의 집합을 묶어주는 곳은 describe에서 사용

정리

  • Quick은 BDD 방식에 적합한 테스팅 라이브러리이며 직관적이기 때문에 처음보는 코드도 이해하기가 쉬움
  • Quick을 통해 describe - context - it 단위로 테스트의 범위를 작성
    • describe: 테스트 범위 (test suite)
    • context: 특정 조건
    • it: 기대되는 결과
  • NImble을 통해 실제 테스트하려는 부분 assert를 읽기 쉽게 작성
  • Quick으로 테스트를 읽기 쉽게 만들고 검증 부분은 Nimble에서 제공해주는 함수를 사용

(포스팅 글에서 계속) 

2. Quick, Nimble으로 테스트 쉽게하는 방법 - describe, context, it, beforeEach 실제 코드에 테스트하는 방법

 

* 참고

https://github.com/Quick/Quick

https://github.com/Quick/Nimble

 

Comments