AgentSkillsCN

swift-testing

精通该项目中 Swift/SwiftUI iOS 与 macOS 应用的 XCTest 测试编写。当您为 HabitQuest、AvoidObstaclesGame、CodingReviewer、MomentumFinance、PlannerApp,或 Shared-Kit 编写测试时,可使用此技能。严格遵循项目约定与开发模式。

SKILL.md
--- frontmatter
name: swift-testing
description: >
    Expertise for writing XCTest tests for Swift/SwiftUI iOS and macOS apps in this project.
    Use when writing tests for HabitQuest, AvoidObstaclesGame, CodingReviewer, MomentumFinance,
    PlannerApp, or Shared-Kit. Follows project conventions and patterns.

Swift Testing Expert (Project-Specific)

Testing expertise tailored for the iOS/macOS apps in this monorepo.

Project Test Structure

code
ProjectName/
├── ProjectName/           # Source files
├── ProjectNameTests/      # Unit tests
└── ProjectNameUITests/    # UI tests (if applicable)

Test Conventions

File Naming

  • Test file: [SourceFile]Tests.swift
  • Example: GameScene.swiftGameSceneTests.swift

Test Function Naming

swift
func test_[unit]_[scenario]_[expectedResult]()
// Example:
func test_scoreManager_addPoints_incrementsScore()

Standard Patterns

swift
import XCTest
@testable import ProjectName

final class ComponentTests: XCTestCase {
    var sut: Component!  // System Under Test

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

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

    func test_method_condition_expectedResult() {
        // Arrange

        // Act

        // Assert
    }
}

Async Testing

swift
func test_asyncMethod_succeeds() async throws {
    let result = try await sut.fetchData()
    XCTAssertNotNil(result)
}

Memory Safety

swift
// Use [weak self] in closures
Task { [weak self] in
    guard let self else { return }
    await self.performWork()
}

Running Tests

bash
# Via Xcode
Cmd+U  # Run all tests

# Via command line
xcodebuild test -scheme ProjectName -destination 'platform=iOS Simulator,name=iPhone 16'