Write XCTest unit and UI tests for iOS apps
✓Works with OpenClaudeYou are a Swift testing specialist. The user wants to write XCTest unit and UI tests for iOS apps.
What to check first
- Run
xcodebuild -showsdksto verify iOS SDK is installed - Open your
.xcodeprojand confirm a test target exists (or create one via File → New → Target → Unit Testing Bundle) - Check
Product → Scheme → Edit Schemeand ensure Test phase is configured with your test target
Steps
- Create a test file by right-clicking your test target in Xcode, selecting "New File," and choosing "Unit Test Case Class"
- Import XCTest at the top:
import XCTestand@testable import YourAppNameto access internal classes - Subclass
XCTestCaseand write test methods prefixed withtest(e.g.,func testLoginValidation()) - Use
XCTAssertEqual(_:_:),XCTAssertTrue(_:), andXCTAssertNil(_:)to validate expected outcomes - For async code, use
expectation(description:)andwaitForExpectations(timeout:handler:)to handle completion handlers - Create UI tests by adding a UI Testing Bundle target, then write tests using
XCUIApplication()to launch and interact with the app - Use
XCUIApplication().buttons["LoginButton"].tap()andXCUIApplication().textFields.element.typeText("user@example.com")for UI interactions - Run tests with
Cmd+Uorxcodebuild test -scheme YourScheme -destination 'platform=iOS Simulator,name=iPhone 15'
Code
import XCTest
@testable import MyApp
final class LoginViewModelTests: XCTestCase {
var viewModel: LoginViewModel!
override func setUp() {
super.setUp()
viewModel = LoginViewModel()
}
override func tearDown() {
viewModel = nil
super.tearDown()
}
func testEmailValidation() {
let isValid = viewModel.isValidEmail("test@example.com")
XCTAssertTrue(isValid)
}
func testInvalidEmailValidation() {
let isValid = viewModel.isValidEmail("invalid-email")
XCTAssertFalse(isValid)
}
func testLoginSuccess() {
let expectation = expectation(description: "Login completes")
viewModel.login(email: "test@example.com", password: "password123") { result in
switch result {
case .success(let user):
XCTAssertEqual(user.id, "123")
expectation.fulfill()
case .failure:
XCTFail("Login should succeed")
}
}
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Swift / iOS Skills
Other Claude Code skills in the same category — free to download.
SwiftUI
Build SwiftUI views with state management and navigation
UIKit
Create UIKit view controllers with Auto Layout
Core Data
Set up Core Data with models, contexts, and fetch requests
Swift Networking
Build networking layer with URLSession and Codable
Swift Combine
Use Combine for reactive programming in Swift
Swift Async/Await Concurrency
Use Swift's structured concurrency for async code without callback hell
Swift CoreData Relationships
Model one-to-many and many-to-many relationships in CoreData
Want a Swift / iOS skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.