Swift Testing #2: @Suite 로 테스트 그룹화

발행: (2025년 12월 5일 오전 07:10 GMT+9)
4 min read
원문: Dev.to

Source: Dev.to

소개

Suite는 테스트 그룹입니다.
@Test가 지정된 테스트를 포함하는 구조체를 만들면, 해당 구조체는 자동으로 테스트 보고서에 “S” 표시가 되어 암시적 Suite임을 나타냅니다.

@Suite 태그를 사용하면 명시적으로 Suite를 만들 수 있으며, 보고서에 지정한 이름으로 표시됩니다.

명시적 Suite 만들기

@Suite("GIVEN some common preconditions")
struct CommonPreconditionsTests {
  @Test
  func specificAction() throws {
    // ...
  }
}

Suite 중첩

공통 기능, 시나리오 및 전제 조건을 더 잘 설명하기 위해 Suite를 중첩할 수 있습니다.
유용한 방법은 BDD의 Gherkin 표기법을 사용하는 것으로, 여러 단계와 레벨을 정의합니다:

  • Feature: 테스트할 기능에 대한 높은 수준의 설명.
  • Scenario: Feature 안의 구체적인 사용 사례.
  • Given: 초기 상태 또는 전제 조건.
  • When: 시나리오를 촉발하는 행동 또는 이벤트.
  • Then: 기대되는 결과 또는 사후 조건.
  • And: 이어지는 단계.

Feature와 Scenario 예시

@Suite("FEATURE: Calculator")
struct CalculatorTests {
  @Suite("SCENARIO: Add two numbers")
  struct AddingTwoNumbers {
    @Test("GIVEN: I have entered 50 in the calculator AND: I have entered 70 in the calculator WHEN: I press add THEN: the result should be 120 on the screen")
    func regularCase() {
      let x = 50
      let y = 70
      let result = x + y
      let expected = 120
      #expect(result == expected)
    }

    @Test("GIVEN: I have entered  in the calculator AND: I have entered  into the calculator WHEN: I press add THEN: the result should be  on the Screen")
    func generalization() {
      let x = 60
      let y = 70
      let result = x + y
      let expected = 130
      #expect(result == expected)
    }
  }
}

결과 테스트 보고서

- FEATURE: Calculator
  - SCENARIO: Add two numbers
    - GIVEN: I have entered 50 in the calculator AND: I have entered 70 in the calculator WHEN: I press add THEN: the result should be 120 on the screen
    - GIVEN: I have entered  in the calculator AND: I have entered  into the calculator WHEN: I press add THEN: the result should be  on the Screen

공통 전제 조건으로 그룹화

여러 테스트가 동일한 전제 조건(GIVEN)을 공유한다면, 별도의 Suite로 묶을 수 있습니다:

@Suite("FEATURE: Calculator")
struct CalculatorTests {
  @Suite("SCENARIO: Subtracting two numbers")
  struct SubtractingTwoNumbers {
    @Suite("GIVEN common pre‑conditions")
    struct CommonPreconditions {
      @Test("WHEN: I press subtract once THEN: the result should be ...")
      func case1() {
        // ...
        #expect(result == expected)
      }

      @Test("WHEN: I press subtract twice THEN: the result should be ...")
      func case2() {
        // ...
        #expect(result == expected)
      }
    }
  }
}

결과 테스트 보고서

- FEATURE: Calculator
  - SCENARIO: Subtracting two numbers
    - GIVEN: common pre‑conditions
      - WHEN: I press subtract once THEN: the result should be ...
      - WHEN: I press subtract twice THEN: the result should be ...

참고 사항

  • XCTAssertXCTestCase 내부에서만 동작하므로, 코드를 Swift Testing으로 옮기는 것만으로는 충분하지 않습니다.
  • @Suite를 사용하면 테스트를 그룹화하고 중첩할 수 있어 보고서 가독성이 향상됩니다.
  • 가능하면 Gherkin 표기법(Feature/Scenario)을 사용해 테스트를 더 명확히 설명하세요.

참고 문헌

Back to Blog

관련 글

더 보기 »