Skip to main content

On This Page

Grouping Tests with @Suite in Swift Testing

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Agrupando pruebas con @Suite

Swift Testing’s @Suite groups tests into hierarchical structures, marked with an “S” in reports. A @Suite("FEATURE: Calculator") example organizes tests by functionality, scenario, and preconditions.

Why This Matters

Traditional XCTest lacks built-in hierarchy, forcing developers to manually manage test organization. @Suite introduces structured grouping via BDD principles, reducing test ambiguity. Misuse—like overnesting—can bloat reports, but proper nesting improves readability and maintenance.

Key Insights

  • “Suite with ‘S’ in reports, 2025”: Explicit @Suite labels appear as “S” in test output.
  • “BDD with Gherkin for test clarity”: Feature/Scenario labels align with Gherkin syntax (e.g., GIVEN, WHEN).
  • “XCTestCase dependency”: XCTAssert requires XCTestCase, unlike Swift Testing’s #expect.

Working Example

@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)
        }
    }
}

Practical Applications

  • Use Case: Organize calculator tests with FEATURE/SCENARIO hierarchies for traceable BDD workflows.
  • Pitfall: Overusing nested suites may obscure test intent in large reports.

References:


Continue reading

Next article

Swift Testing #4: Correr pruebas de forma serial

Related Content