agentsclimarketplace

Swift testing

Skill Mattakushi432/Claude-Code-Skills-Custom-DevTools-Pack/plugins/devtools-pack/skills/swift-testing

When to activate: Swift Testing framework, XCTest, unit tests, async tests, test macros, parameterized tests, Swift mockingFrom its SKILL.md

Install
npx -y skills add Mattakushi432/Claude-Code-Skills-Custom-DevTools-Pack --skill swift-testing

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

SKILL.md

4.0 KB, 914 tokens by cl100k_base, as published. Nobody here has run it

Swift Testing

Swift Testing Framework (Swift 6+)

Prefer the new @Test / #expect API over XCTest for new code.

import Testing

struct MathTests {
    @Test func addition() {
        #expect(2 + 2 == 4)
    }

    @Test("Subtraction is inverse of addition")
    func subtraction() {
        let result = 10 - 3
        #expect(result == 7)
    }

    @Test func throws() async throws {
        #expect(throws: ValidationError.self) {
            try validate("")
        }
    }
}

Parameterized Tests

@Test("Validates email addresses", arguments: [
    ("[email protected]", true),
    ("invalid-email",    false),
    ("[email protected]",           true),
    ("@noDomain.com",   false),
])
func emailValidation(email: String, isValid: Bool) {
    #expect(validateEmail(email) == isValid)
}

Suites and Tags

@Suite("User registration")
struct RegistrationTests {
    @Test(.tags(.critical)) func successfulRegistration() async throws { ... }
    @Test(.tags(.edge))     func duplicateEmail() async throws { ... }
    @Test(.disabled("Flaky on CI — see #1234")) func raceCondition() { ... }
}

extension Tag {
    @Tag static var critical: Self
    @Tag static var edge: Self
}

Async Tests

@Test func loadsUserAsync() async throws {
    let service = UserService(client: MockHTTPClient())
    let user = try await service.load(id: UUID())
    #expect(user.name == "Alice")
}

XCTest Patterns (legacy / UIKit integration tests)

import XCTest

final class CartTests: XCTestCase {
    var sut: Cart!

    override func setUp() {
        super.setUp()
        sut = Cart()
    }

    override func tearDown() {
        sut = nil
        super.tearDown()
    }

    func testAddItem_increasesCount() {
        let item = Item(name: "Book", price: 9.99)
        sut.add(item)
        XCTAssertEqual(sut.items.count, 1)
    }

    func testAsync() async throws {
        let result = try await sut.checkout()
        XCTAssertTrue(result.success)
    }
}

Protocol-Based Mocking

protocol HTTPClient: Sendable {
    func data(for request: URLRequest) async throws -> (Data, URLResponse)
}

struct MockHTTPClient: HTTPClient {
    var result: Result<Data, Error>

    func data(for request: URLRequest) async throws -> (Data, URLResponse) {
        let data = try result.get()
        let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)!
        return (data, response)
    }
}

// Usage in test
let mock = MockHTTPClient(result: .success(encodedUser))
let service = UserService(client: mock)

Test Structure (AAA)

@Test func createsOrderWithCorrectTotal() {
    // Arrange
    let items = [Item(price: 10), Item(price: 20)]
    let cart = Cart(items: items)

    // Act
    let order = cart.checkout(tax: 0.1)

    // Assert
    #expect(order.subtotal == 30)
    #expect(order.tax == 3)
    #expect(order.total == 33)
}

Snapshot Testing (third-party)

// Using swift-snapshot-testing
import SnapshotTesting

class ViewSnapshotTests: XCTestCase {
    func testButtonAppearance() {
        let button = PrimaryButton(title: "Buy Now")
        assertSnapshot(of: button, as: .image(on: .iPhone13))
    }
}

Common Anti-Patterns

  • Testing implementation details — test behavior and outputs, not private methods
  • Shared mutable test state — reset in setUp/tearDown or use isolated instances
  • sleep in async tests — use await with proper async APIs instead
  • Skipping tearDown — always clean up to avoid test pollution
  • XCTAssert in new code — prefer #expect with the Swift Testing framework

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 326,758. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.