ios-testing-strategy
Skill0 starsChanged 3 months agoMITView on GitHub
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.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- Livsy90/iOS-Testing-Strategy-Agent-Skilltheir own version, first seen Jul 21, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- Livsy90/iOS-Testing-Strategy-Agent-Skill★ 14same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository10 other files it gives its agents.
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 4 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
Before writing a single test, find (or create) the seams that let you substitute real dependencies with test doubles:
- Distinctive: line 17
If the code under test reaches a singleton directly, introduce a protocol and inject it — don't test through the singleton.
- Very specific: line 33
If the class under test currently hardcodes LiveUserRepository() internally, that's the first fix: add the initializer parameter above …
- Very specific: line 67
Use Swift Testing (@Test, #expect) for new code in projects on Swift 5.10+ / Xcode 16+.
- Distinctive: line 74
Before implementing the fix/feature, run the test and confirm it fails for the *expected* reason (not a compile error, not a setup bug):
- Very specific: line 94
Don't test that a SwiftUI View's body produces particular view types — that's an implementation detail and breaks on every refactor.
- Very specific: line 96
Don't test third-party framework behavior (e.g.
- Distinctive: line 99
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.
≈ “Common” is an estimate: at least 1 in 5 of the 372,429 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
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.
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.
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.
@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:
- Business logic with branching (pricing, eligibility, state machines).
- Anything that has broken in production before.
- Public API of a module other code depends on.
- Pure UI layout code is usually not worth unit testing — that's what
ios-ui-test-engineerand snapshot tests are for.
6. What NOT to test
- Don't test that a SwiftUI
View'sbodyproduces particular view types — that's an implementation detail and breaks on every refactor. - Don't test third-party framework behavior (e.g. that
URLSessionactually 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.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

