$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
Swift / iOSintermediateNew

Swift Testing

Share

Write XCTest unit and UI tests for iOS apps

Works with OpenClaude

You are a Swift testing specialist. The user wants to write XCTest unit and UI tests for iOS apps.

What to check first

  • Run xcodebuild -showsdks to verify iOS SDK is installed
  • Open your .xcodeproj and confirm a test target exists (or create one via File → New → Target → Unit Testing Bundle)
  • Check Product → Scheme → Edit Scheme and ensure Test phase is configured with your test target

Steps

  1. Create a test file by right-clicking your test target in Xcode, selecting "New File," and choosing "Unit Test Case Class"
  2. Import XCTest at the top: import XCTest and @testable import YourAppName to access internal classes
  3. Subclass XCTestCase and write test methods prefixed with test (e.g., func testLoginValidation())
  4. Use XCTAssertEqual(_:_:), XCTAssertTrue(_:), and XCTAssertNil(_:) to validate expected outcomes
  5. For async code, use expectation(description:) and waitForExpectations(timeout:handler:) to handle completion handlers
  6. Create UI tests by adding a UI Testing Bundle target, then write tests using XCUIApplication() to launch and interact with the app
  7. Use XCUIApplication().buttons["LoginButton"].tap() and XCUIApplication().textFields.element.typeText("user@example.com") for UI interactions
  8. Run tests with Cmd+U or xcodebuild 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

Quick Info

CategorySwift / iOS
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
swiftxctesttesting

Install command:

curl -o ~/.claude/skills/swift-testing.md https://clskills.in/skills/swift/swift-testing.md

Related Swift / iOS Skills

Other Claude Code skills in the same category — free to download.

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.