> ## Documentation Index
> Fetch the complete documentation index at: https://docs.parsaa.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Test Generation

> Generate XCTest unit tests for your Swift code

## Overview

Parsaa generates unit tests for your Swift code. Summon it with the **`@tester`** mention in the composer, with a file focused in Xcode. It identifies testable behaviors, edge cases, and error conditions, then writes tests to your test target.

<Info>
  `@tester` is a built-in sub-agent. Configure it in **Settings → Automation → Test Generator**: choose the framework (**Swift Testing** or **XCTest**), the naming convention, whether to generate mocks, and a coverage target — those settings are passed to the agent on each run.
</Info>

## What It Generates

<CardGroup cols={2}>
  <Card title="Test Cases" icon="vial">
    `XCTestCase` subclasses with well-structured test methods covering the behavior of your code.
  </Card>

  <Card title="Setup & Teardown" icon="arrows-rotate">
    `setUp()` and `tearDown()` methods that initialize and clean up test dependencies.
  </Card>

  <Card title="Edge Case Tests" icon="triangle-exclamation">
    Tests for boundary conditions, empty inputs, nil values, and other edge cases your code should handle.
  </Card>

  <Card title="Mocks & Stubs" icon="clone">
    Mock objects and stubs for dependencies, so your tests are isolated and repeatable.
  </Card>
</CardGroup>

## How to Use

<Steps>
  <Step title="Enable the generator">
    Turn on **Test Generator** in **Settings → Automation → Test Generator** and pick your framework and options.
  </Step>

  <Step title="Focus the file in Xcode">
    Open the file you want covered so it becomes the focused file.
  </Step>

  <Step title="Mention @tester">
    In the Parsaa composer, type `@tester`. Parsaa generates tests for the focused file and writes them to disk.
  </Step>

  <Step title="Review & add to your target">
    Review the generated tests, add the file to your test target, and run them to verify they pass.
  </Step>
</Steps>

## Example

**Input** — a function to test:

```swift theme={null}
struct PriceCalculator {
    func calculateTotal(items: [Item], taxRate: Double) -> Double {
        let subtotal = items.reduce(0.0) { $0 + $1.price }
        let tax = subtotal * taxRate
        return subtotal + tax
    }
}
```

**Generated tests:**

```swift theme={null}
import XCTest
@testable import MyApp

final class PriceCalculatorTests: XCTestCase {

    var calculator: PriceCalculator!

    override func setUp() {
        super.setUp()
        calculator = PriceCalculator()
    }

    func testCalculateTotal_withMultipleItems() {
        let items = [
            Item(name: "A", price: 10.0),
            Item(name: "B", price: 20.0)
        ]
        let total = calculator.calculateTotal(items: items, taxRate: 0.1)
        XCTAssertEqual(total, 33.0, accuracy: 0.01)
    }

    func testCalculateTotal_withEmptyItems() {
        let total = calculator.calculateTotal(items: [], taxRate: 0.1)
        XCTAssertEqual(total, 0.0, accuracy: 0.01)
    }

    func testCalculateTotal_withZeroTaxRate() {
        let items = [Item(name: "A", price: 25.0)]
        let total = calculator.calculateTotal(items: items, taxRate: 0.0)
        XCTAssertEqual(total, 25.0, accuracy: 0.01)
    }
}
```

## Best Practices

<Warning>
  **Always review generated tests.** AI-generated tests are a starting point. Verify they test meaningful behavior, not just implementation details. Check that assertions are correct and edge cases are realistic.
</Warning>

<AccordionGroup>
  <Accordion title="Test behavior, not implementation">
    Good tests verify what your code does, not how it does it. If Parsaa generates a test that relies on internal implementation details, refactor it to test the public interface instead.
  </Accordion>

  <Accordion title="Verify assertions">
    Double-check that expected values in assertions are correct. AI can miscalculate expected outputs, especially for complex logic.
  </Accordion>

  <Accordion title="Add your own edge cases">
    Parsaa covers common edge cases, but you know your domain best. Add tests for scenarios specific to your business logic.
  </Accordion>

  <Accordion title="Keep tests isolated">
    Each test should be independent. If Parsaa generates tests that share mutable state, refactor them to use fresh setup in each test method.
  </Accordion>
</AccordionGroup>

<Tip>
  Use test generation as a starting point, then iterate. Generated tests get you to 70-80% coverage quickly — you fill in the domain-specific gaps.
</Tip>
