# Swift Testing

> WHEN writing tests in Swift with the Swift Testing framework; NOT XCTest.

- **Type:** Skill
- **Install:** `agentstack add skill-mintuz-skills-swift-testing`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [mintuz](https://agentstack.voostack.com/s/mintuz)
- **Installs:** 0
- **Category:** [AI & ML](https://agentstack.voostack.com/c/ai-and-ml)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [mintuz](https://github.com/mintuz)
- **Source:** https://github.com/mintuz/skills/tree/main/plugins/app/skills/swift-testing

## Install

```sh
agentstack add skill-mintuz-skills-swift-testing
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Swift Testing Framework: Basics

Guidance for starting with Swift Testing (Testing framework) and writing clear, macro-driven tests.

## Core Concepts

- Import `Testing` to unlock macros; tests are plain functions annotated with `@Test`.
- Name tests freely; use `@Test("Display Name")` to set the navigator title.
- `#expect` is the primary assertion; pass a boolean expression to assert truthy outcomes.
- Async/throwing tests are supported via `async`/`throws` on the test function.
- Works alongside XCTest in the same project.

## Example: Simple Test

```swift
import Testing

func add(_ a: Int, _ b: Int) -> Int { a + b }

@Test("Verify addition function") func verifyAdd() {
    let result = add(1, 2)
    #expect(result == 3)
}
```

## Expecting Throws

Use `#expect(throws:)` to verify a thrown error. Inspect the error via the closure overload when you need to assert the specific case.

```swift
@Test func verifyThrowingFunction() {
    #expect(throws: MyError.self) {
        try throwingFunction()
    }

    #expect {
        try throwingFunction()
    } throws: { error in
        guard let myError = error as? MyError else { return false }
        return myError == .invalidInput
    }
}
```

## Require vs Expect

- `#require` throws immediately when the condition is false, halting the test early.
- Handy for unwrapping optionals before continuing with more assertions.

```swift
@Test func verifyOptionalFunc() throws {
    let result = try #require(optionalFunc()) // unwrap or fail fast
    #expect(result > 0)
}
```

## Recording Issues

Use `Issue.record("message")` to log and exit gracefully when continuing the test is pointless.

```swift
@Test func verifyOptionalFunc() throws {
    guard let result = optionalFunc() else {
        Issue.record("optional result is nil")
        return
    }
    #expect(result > 0)
}
```

## Best Practices Checklist

- [ ] Prefer `@Test`-annotated free functions; no need for XCTest naming conventions.
- [ ] Use `@Test("Name")` to keep navigator titles readable.
- [ ] Default to `#expect` for assertions; add multiple expects per test when logical.
- [ ] Use `#require` to guard preconditions/unwrap optionals before further checks.
- [ ] Assert thrown errors with `#expect(throws:)`, including specific case checks.
- [ ] Mix Swift Testing with XCTest during migration; convert incrementally.
- [ ] Keep tests small and focused; one behavior per test function.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [mintuz](https://github.com/mintuz)
- **Source:** [mintuz/skills](https://github.com/mintuz/skills)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-mintuz-skills-swift-testing
- Seller: https://agentstack.voostack.com/s/mintuz
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
