ios-testing
Skill6 starsChanged 9 months agoMITView on GitHub
iOS testing expert skill covering Swift Testing framework (@Test, #expect, #require, @Suite, parameterized tests, traits), XCTest (assertions, async testing, performance testing, XCTestExpectation), UI Testing (XCUIApplication, XCUIElement, Page Object pattern, accessibility identifiers), snapshot testing (swift-snapshot-testing), mocking strategies (protocol-based mocks, URLProtocol for network, test doubles), and testing patterns for SwiftUI, SwiftData, Combine, and async/await code. Use this skill whenever the user writes tests, creates test classes, needs mocking strategies, or asks about testing iOS code. Triggers on: test, @Test, #expect, XCTest, XCTestCase, unit test, UI test, integration test, mock, stub, spy, fake, snapshot test, test coverage, TDD, testing, assert, XCTAssert, Swift Testing, @Suite, parameterized test, test plan, test double, URLProtocol mock, ViewInspector, or any iOS testing question.
4 other projects use a version of this.
Adopted by
Most recently taken up by
- conjure-3301/skillstheir own version, first seen Aug 17, 2026
- ahmed3elshaer/everything-claude-code-mobiletheir own version, first seen Jun 14, 2026
- rusel95/ios-agent-skillstheir own version, first seen Apr 16, 2026
- jx1100370217/my-openclaw-skillstheir own version, first seen Feb 3, 2026
Other versions4 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 4
- ahmed3elshaer/everything-claude-code-mobile★ 66same name
- rusel95/ios-agent-skills★ 11same name
- jx1100370217/my-openclaw-skills★ 4same name
- conjure-3301/skills★ 0same 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 repository9 other files it gives its agents.
- AGENTS.mdAGENTS.md
- ios-accessibilitySkill
- ios-architectureSkill
- ios-concurrencySkill
- ios-dataSkill
- ios-networkingSkill
- ios-performanceSkill
- ios-securitySkill
- ios-swiftuiSkill
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 22
Use protocol-based dependency injection for testability.
- Very specific: line 23
Use URLProtocol for network mocking (NOT mocking URLSession directly).
- Very specific: line 24
Use isStoredInMemoryOnly: true for SwiftData test containers.
- Very specific: line 28
Never use sleep() in tests -- use expectations, confirmations, or Clock injection.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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 Skill
Core Rules
- Use Swift Testing (
@Test,#expect) for ALL new unit tests -- it is the modern framework (Xcode 16+). - Keep XCTest only for UI tests and performance tests -- Swift Testing does not support these yet.
- Both frameworks can coexist in the same target -- migrate incrementally, never rewrite working tests.
- Use protocol-based dependency injection for testability.
- Use
URLProtocolfor network mocking (NOT mocking URLSession directly). - Use
isStoredInMemoryOnly: truefor SwiftData test containers. - Name tests descriptively:
@Test("Login succeeds with valid credentials"). - One assertion per test is ideal, but pragmatic grouping is fine.
- Test behavior, not implementation details.
- Never use
sleep()in tests -- use expectations, confirmations, orClockinjection.
Framework Choice
| Test Type | Framework | Why |
| Unit tests (new) | Swift Testing | Modern, less boilerplate, parameterized tests |
| Unit tests (existing) | XCTest | Don't rewrite working tests without reason |
| UI tests | XCTest | Swift Testing doesn't support XCUIApplication |
| Performance tests | XCTest | measure {} not available in Swift Testing |
| Snapshot tests | XCTest + swift-snapshot-testing | Point-Free library, XCTest integration |
Test Organization
MyAppTests/ # Unit test target
Models/
UserTests.swift
OrderTests.swift
ViewModels/
LoginViewModelTests.swift
ProfileViewModelTests.swift
Services/
APIClientTests.swift
AuthServiceTests.swift
Helpers/
Mocks/
MockAPIClient.swift
MockAuthService.swift
TestData/
UserFixtures.swift
JSONFixtures.swift
MyAppUITests/ # UI test target
Screens/ # Page Objects
LoginScreen.swift
HomeScreen.swift
Flows/
OnboardingFlowTests.swift
PurchaseFlowTests.swift
Helpers/
XCUIApplication+Launch.swiftQuick Start: Swift Testing
import Testing
@testable import MyApp
@Suite("AuthService")
struct AuthServiceTests {
let sut: AuthService
let mockAPI: MockAPIClient
init() {
mockAPI = MockAPIClient()
sut = AuthService(api: mockAPI)
}
@Test("Login succeeds with valid credentials")
func loginSuccess() async throws {
mockAPI.loginResult = .success(User.fixture)
let user = try await sut.login(email: "test@example.com", password: "pass123")
#expect(user.email == "test@example.com")
#expect(mockAPI.loginCallCount == 1)
}
@Test("Login fails with invalid credentials")
func loginFailure() async {
mockAPI.loginResult = .failure(AuthError.invalidCredentials)
await #expect(throws: AuthError.invalidCredentials) {
try await sut.login(email: "bad@example.com", password: "wrong")
}
}
@Test("Password validation", arguments: [
("short", false),
("validPass1!", true),
("nouppercase1!", false),
("NOLOWERCASE1!", false),
])
func passwordValidation(password: String, isValid: Bool) {
#expect(sut.isValidPassword(password) == isValid)
}
}Quick Start: XCTest (UI Tests)
import XCTest
final class LoginUITests: XCTestCase {
var app: XCUIApplication!
override func setUp() {
super.setUp()
continueAfterFailure = false
app = XCUIApplication()
app.launchArguments = ["--uitesting", "--reset-state"]
app.launch()
}
func test_login_withValidCredentials_showsHome() {
let loginScreen = LoginScreen(app: app)
loginScreen
.typeEmail("user@example.com")
.typePassword("password123")
.tapLogin()
let homeScreen = HomeScreen(app: app)
XCTAssertTrue(homeScreen.welcomeLabel.waitForExistence(timeout: 5))
}
}Protocol-Based Mocking Pattern
// 1. Define protocol
protocol APIClientProtocol: Sendable {
func login(email: String, password: String) async throws -> User
func fetchProfile(id: String) async throws -> Profile
}
// 2. Production implementation
final class APIClient: APIClientProtocol {
func login(email: String, password: String) async throws -> User { /* real impl */ }
func fetchProfile(id: String) async throws -> Profile { /* real impl */ }
}
// 3. Mock for tests
final class MockAPIClient: APIClientProtocol, @unchecked Sendable {
var loginResult: Result<User, Error> = .failure(TestError.notConfigured)
var loginCallCount = 0
var loginReceivedArgs: [(email: String, password: String)] = []
func login(email: String, password: String) async throws -> User {
loginCallCount += 1
loginReceivedArgs.append((email, password))
return try loginResult.get()
}
var fetchProfileResult: Result<Profile, Error> = .failure(TestError.notConfigured)
var fetchProfileCallCount = 0
func fetchProfile(id: String) async throws -> Profile {
fetchProfileCallCount += 1
return try fetchProfileResult.get()
}
}URLProtocol Network Mocking
final class MockURLProtocol: URLProtocol {
static var requestHandler: ((URLRequest) throws -> (HTTPURLResponse, Data))?
override class func canInit(with request: URLRequest) -> Bool { true }
override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }
override func startLoading() {
guard let handler = Self.requestHandler else {
client?.urlProtocolDidFinishLoading(self)
return
}
do {
let (response, data) = try handler(request)
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
client?.urlProtocol(self, didLoad: data)
client?.urlProtocolDidFinishLoading(self)
} catch {
client?.urlProtocol(self, didFailWithError: error)
}
}
override func stopLoading() {}
}
// Usage in test:
let config = URLSessionConfiguration.ephemeral
config.protocolClasses = [MockURLProtocol.self]
let session = URLSession(configuration: config)
let apiClient = APIClient(session: session)
MockURLProtocol.requestHandler = { request in
let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)!
let data = try JSONEncoder().encode(User.fixture)
return (response, data)
}SwiftData Testing
@Test("Saving a user persists it")
func saveUser() throws {
let config = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try ModelContainer(for: User.self, configurations: config)
let context = ModelContext(container)
let user = User(name: "Test", email: "test@example.com")
context.insert(user)
try context.save()
let descriptor = FetchDescriptor<User>()
let users = try context.fetch(descriptor)
#expect(users.count == 1)
#expect(users.first?.name == "Test")
}Combine Testing
@Test("Publisher emits values correctly")
func publisherEmitsValues() async {
let viewModel = CounterViewModel()
var received: [Int] = []
let cancellable = viewModel.$count.sink { received.append($0) }
viewModel.increment()
viewModel.increment()
#expect(received == [0, 1, 2])
cancellable.cancel()
}async/await Confirmation (Replaces XCTestExpectation)
@Test("Notification triggers callback")
func notificationCallback() async {
await confirmation("callback received") { confirm in
let observer = NotificationObserver {
confirm()
}
NotificationCenter.default.post(name: .testNotification, object: nil)
}
}
@Test("Delegate called exactly 3 times")
func delegateCalledThreeTimes() async {
await confirmation("delegate called", expectedCount: 3) { confirm in
let delegate = MockDelegate(onCall: { confirm() })
let sut = DataLoader(delegate: delegate)
await sut.loadBatch(count: 3)
}
}Test Fixtures Pattern
extension User {
static var fixture: User {
User(id: "test-id", name: "Test User", email: "test@example.com")
}
static func fixture(
id: String = "test-id",
name: String = "Test User",
email: String = "test@example.com"
) -> User {
User(id: id, name: name, email: email)
}
}Common Mistakes to Avoid
- Don't test private methods -- test public behavior instead.
- Don't mock what you don't own -- wrap third-party APIs in your own protocol.
- Don't use
sleep()orTask.sleep()for timing -- useClockinjection or expectations. - Don't share mutable state between tests -- use struct-based
@Suitewithinit(). - Don't forget
@MainActorisolation -- if your SUT is@MainActor, your test must be too. - Don't use
XCTAssertin Swift Testing -- use#expectand#require. - Don't force-unwrap in tests -- use
#requireorXCTUnwrap. - Don't test Apple frameworks -- trust that
UserDefaults.setworks. - Don't write tests after the fact just for coverage -- write tests that catch real bugs.
- Don't ignore flaky tests -- use
withKnownIssueto mark them, then fix root cause.
Migration: XCTest to Swift Testing
| XCTest | Swift Testing |
class MyTests: XCTestCase | @Suite struct MyTests |
func testSomething() | @Test func something() |
override func setUp() | init() |
override func tearDown() | deinit |
XCTAssertEqual(a, b) | #expect(a == b) |
XCTAssertNil(x) | #expect(x == nil) |
XCTAssertThrowsError(expr) | #expect(throws: ErrorType.self) { expr } |
XCTUnwrap(optional) | try #require(optional) |
XCTestExpectation + wait | confirmation { } |
XCTSkipIf(condition) | .enabled(if: !condition) trait |
measure { } | No equivalent -- keep in XCTest |
| UI tests with XCUIApplication | No equivalent -- keep in XCTest |
References
- references/swift-testing.md -- Swift Testing framework deep dive
- references/xctest.md -- XCTest assertions, async, performance
- references/ui-testing.md -- XCUIApplication, Page Object, accessibility
- references/mocking.md -- Protocol mocks, URLProtocol, snapshot testing
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.

