Skip to content
Skills Library
Programming · Testing

ios-testing-strategy

/ios-testing-strategy

Step-by-step procedure for adding tests to existing Swift/iOS code, whether untested legacy code or new TDD work. Use when asked to "add test coverage", "write tests for this", "make this testable", or when starting TDD on new Swift code.

Licence
MIT
Last repository change
01.09.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 01.09.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Step-by-step procedure for adding tests to existing Swift/iOS code, whether untested legacy code or new TDD work. Use when asked to "add test coverage", "write tests for this", "make this testable", or when starting TDD on new Swift code.

Triggers from the description

add test coveragewrite tests for thismake this testable
Original author description

Step-by-step procedure for adding tests to existing Swift/iOS code, whether untested legacy code or new TDD work. Use when asked to "add test coverage", "write tests for this", "make this testable", or when starting TDD on new Swift code.

How it works

  1. 1. Identify the seams
  2. 2. Write a test double, not a framework
  3. 3. Write the test against behavior, not implementation
  4. 4. Verify red before green
  5. 5. Prioritize by risk, not by percentage
  6. 6. What NOT to test
Original SKILL.md headings · 97 lines, 505 words
SKILL.md preview
---
name: ios-testing-strategy
description: Step-by-step procedure for adding tests to existing Swift/iOS code, whether untested legacy code or new TDD work. Use when asked to "add test coverage", "write tests for this", "make this testable", or when starting TDD on new Swift code.
---

# iOS Testing Strategy

A concrete procedure, not just principles. Follow these steps in order.

## 1. Identify the seams

Before writing a single test, find (or create) the seams that let you
substitute real dependencies with test doubles:

- Networking, persistence (Core Data/SwiftData contexts), date/time,
  and any singleton (`.shared`) are the usual suspects.
- If the code under test reaches a singleton directly, introduce a
  protocol and inject it — don't test through the singleton.

```swift
protocol UserRepository {
    func fetchUser(id: String) async throws -> User
}

final class LiveUserRepository: UserRepository { /* real network call */ }

final class ProfileViewModel {
    private let repository: UserRepository
    init(repository: UserRepository) { self.repository = repository }
}
```

If the class under test currently hardcodes `LiveUserRepository()`
internally, that's the first fix: add the initializer parameter above
(default it to the live implementation so call sites don't all need to
change at once).

## 2. Write a test double, not a framework

A hand-written struct/class conforming to the protocol is almost always
enough. Reach for a mocking library only if the project already has one.

```swift
final class StubUserRepository: UserRepository {
    var result: Result<User, Error> = .success(.mock)
    func fetchUser(id: String) async throws -> User { try result.get() }
}
```

## 3. Write the test against behavior, not implementation

Assert on outputs and observable state changes, not on which private
methods got called.

```swift
@Test func profileLoadShowsFetchedUserName() async throws {
    let repository = StubUserRepository()
    repository.result = .success(User(id: "1", name: "Ada"))
    let viewModel = ProfileViewModel(repository: repository)

    await viewModel.load(id: "1")

    #expect(viewModel.displayName == "Ada")
}
```

Use Swift Testing (`@Test`, `#expect`) for new code in projects on Swift
5.10+ / Xcode 16+. Use XCTest for projects that haven't adopted Swift
Testing yet — check the existing test target before picking one; don't
mix frameworks in the same target without a reason.

## 4. Verify red before green

Before implementing the fix/feature, run the test and confirm it fails
for the *expected* reason (not a compile error, not a setup bug):

Run: `xcodebuild test -scheme <Scheme> -destination 'platform=iOS Simulator,name=iPhone 16' -only-testing:<Target>/<TestClass>/<testName>`

Then implement the minimal code to pass, and re-run the same command to
confirm green.

## 5. Prioritize by risk, not by percentage

Coverage percentage is a lagging indicator. Prioritize, in order:

1. Business logic with branching (pricing, eligibility, state machines).
2. Anything that has broken in production before.
3. Public API of a module other code depends on.
4. Pure UI layout code is usually not worth unit testing — that's what
   `ios-ui-test-engineer` and snapshot tests are for.

## 6. What NOT to test

- Don't test that a SwiftUI `View`'s `body` produces particular view
  types — that's an implementation detail and breaks on every refactor.
- Don't test third-party framework behavior (e.g. that `URLSession`
  actually makes a network call) — test that *your code* calls it
  correctly, via the seam from step 1.
- Don't write a test for a private method by making it internal/public
  just to reach it — test through the public behavior that uses it.

Versions

The database has no other skills named ios-testing-strategy.

Database snapshot: 13.11.2026 · package version 10d57c1cbf1c (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100test-generator
    Generate test templates for unit tests, integration tests, and UI tests using Swift Testing and XCTest. Use when adding tests to iOS/macOS apps.
  • 90testing
    TDD and testing skills for iOS/macOS apps. Covers characterization tests, TDD workflows, test contracts, snapshot tests, test infrastructure, and deterministic quality gates (fitness functions, coverage ratchet, mutation testing). Use for test-driven development, adding tests to…
  • 100tdd-testing
    Guide for Test-Driven Development in the DEVS platform. Use this when asked to write tests, implement TDD, or add test coverage for lib/ and stores/ code.
  • 100tdd
    Build behavior test-first. Use when a step adds behavior, when adding tests to existing code, or when deciding what to mock or whether a test earns its place. Not for planning the build (write-plan) or spec sign-off (verify-specs).
  • 100write-tests
    Write unit and integration tests. Use when the user says "write tests for this", "add test coverage", "how do I test this", "TDD", "red green refactor", "unit test this function", "test this module", "I need tests for", or has code that lacks tests - even if they don't explicitly say "write tests".
  • 90Swift Unit Testing Skill
    Design, write, migrate, and review Swift unit and integration tests using Swift Testing by default, with XCTest retained for UI automation, performance metrics, Objective-C interoperability, and legacy suites. Use for @Test, #expect, #require, parameterized tests, async tests,…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.