An AI agent writes code faster than you can read it. That makes tests the main way you know whether the code works. The good news for iOS is that almost everything you need runs from the terminal, which is exactly where Claude Code works. This post covers the three layers: unit tests, UI tests and runs on a real device.
For the wider setup, see Claude Code for mobile app development.
Pick the right framework for each layer
Apple now ships two testing frameworks, and the split is clear. Apple’s XCTest page says: “Xcode 16 and later includes Swift Testing” and suggests you “Consider using Swift Testing for new unit test development.” The same page adds: “Continue to use XCTest for user interface tests.”
So the rule for Claude Code is simple:
- Unit tests: Swift Testing (
import Testing,@Test,#expect,#require). - UI tests: XCTest with XCUIAutomation.
- Mixed targets are fine. Apple says “A test target can contain tests using both Swift Testing and XCTest, however don’t mix API from the two frameworks in the same test.”
Write this into your project’s CLAUDE.md so the agent does not guess. Without it, models often fall back to older XCTest patterns for everything.
Give Claude Code the commands
Claude Code cannot press Product > Test in Xcode, but it does not need to. Apple documents the terminal equivalent:
xcodebuild test -scheme SampleApp
To run a single test, Apple says to pass its identifier to -only-testing, in the form test_target/test_type/test_function:
xcodebuild test -scheme SampleApp \
-only-testing SampleAppTests/SampleAppTests/testEmptyArrayWhenNoOverlappingNotes
Add a -destination so the run does not depend on whatever Xcode last selected. The xcodebuild man page describes a destination as “a set of comma-separated key=value pairs”, for example 'platform=iOS Simulator,name=iPhone 16'. Use a simulator name that exists on your Mac (xcrun simctl list devices prints them).
A short CLAUDE.md block is enough:
## Tests
- Unit tests use Swift Testing. UI tests use XCTest.
- Run all: xcodebuild test -scheme MyApp -destination 'platform=iOS Simulator,name=iPhone 16'
- Run one: add -only-testing MyAppTests/<Type>/<function>
- Never mark a failing test as skipped to make the run pass.
The last line matters. An agent under pressure to get a green run will sometimes disable the test instead of fixing the bug. Say plainly that it may not. See the CLAUDE.md guide for Flutter for how to structure the rest of the file.
Unit tests: ask for behavior, not coverage
Apple’s migration guide shows the Swift Testing shape:
@Test func engineWorks() throws {
let engine = FoodTruck.shared.engine
try #require(engine.parts.first != nil)
#expect(engine.batteryLevel > 0)
try engine.start()
#expect(engine.isRunning)
}
#require stops the test when a precondition fails. #expect records a failure and keeps going. Apple’s migration table maps try XCTUnwrap(x) to try #require(x) and XCTFail("…") to Issue.record("…"), which helps when you ask Claude Code to convert an older suite.
Prompts that produce useful tests describe behavior: “Write tests for the streak calculator: a missed day resets to zero, two entries on one day count once, time zones do not split a day.” A prompt like “add tests for StreakCalculator” tends to produce tests that restate the implementation.
A good loop is: write the failing test first, run it with -only-testing, then let Claude Code change the code until it passes. The test is your spec, and it stops the agent from quietly changing what “done” means.
UI tests: stable identifiers first
Apple describes UI testing as a way to “manipulate your app’s views and controls, as if a person is interacting with your interface.” XCUIApplication is “A proxy that can launch, monitor, and terminate a test application.”
UI tests break when they find elements by visible text, because copy changes. Before asking for UI tests, ask Claude Code to add accessibility identifiers to the controls the test needs. Then the test finds app.buttons["onboarding.continue"] instead of a label that a copy edit will rename.
Keep the UI suite small. One test per critical journey (onboarding, paywall, the core action, sign out) is plenty. UI tests are slow, and an agent that runs the whole suite after every edit burns time and context.
Simulators from the terminal
xcrun simctl lets Claude Code set up simulator state without clicking through Settings:
xcrun simctl list devices
xcrun simctl boot "iPhone 16"
xcrun simctl privacy booted grant photos com.example.myapp
xcrun simctl openurl booted myapp://settings
xcrun simctl push booted com.example.myapp payload.json
The privacy subcommand can “Grant access without prompting”, which saves a lot of flaky permission dialogs in UI tests. openurl tests deep links, and push sends “a simulated push notification” from a JSON file. Note the help text warns that “Some permission changes will terminate the application if running.”
For more on driving the simulator, see Claude Code with the iOS Simulator.
Real devices
Simulators miss things: camera, real push delivery, performance, Face ID flows. Before a device run works from the terminal:
- Connect the iPhone, tap Trust on the “Trust this computer?” dialog.
- Turn on Developer Mode. Flutter’s iOS setup page explains that “Apple requires enabling Developer Mode on the device”, under Settings > Privacy & Security > Developer Mode, followed by a restart.
- Make sure signing works once in Xcode.
After that, the destination changes to the device:
xcodebuild test -scheme MyApp -destination 'platform=iOS,name=My iPhone'
Do the trust and signing steps yourself. They involve your Apple account, and they only happen once.
Flutter apps on iOS
If your iOS app is built with Flutter, the commands change but the layers stay the same. Flutter’s testing overview defines a unit test as one that “tests a single function, method, or class”, a widget test as one that “tests a single widget”, and an integration test as one that “tests a complete app or a large part of an app.” Its advice: “a well-tested app has many unit and widget tests, tracked by code coverage, plus enough integration tests to cover all the important use cases.”
Integration tests live in an integration_test/ folder and run on a connected iOS device with:
flutter test integration_test/app_test.dart
One limit to know: Flutter’s integration_test package “can’t interact with native platform UI, such as permission dialogs, notifications, or platform views.” Combine it with simctl privacy grants, or test those flows by hand. See testing a Flutter app with Claude Code for the full Flutter loop.
Keep test output out of the main context
A full xcodebuild test log is long. Claude Code’s cost guide suggests delegating “Running tests, fetching documentation, or processing log files” to subagents “so the verbose output stays in the subagent’s context while only a summary returns to your main conversation.” A small test-runner subagent that returns only failing test names and messages keeps your main session focused.
When you need the details later, Apple notes that xcodebuild “outputs an Xcode Test Results (.xcresults) bundle” you can open in Xcode.
A practical checklist
- CLAUDE.md names the frameworks and exact commands.
- Every bug fix starts with a failing test.
- UI controls have accessibility identifiers.
- Simulator permissions are granted with
simctl, not tapped. - A device run happens before every TestFlight build.
- Run the mobile app QA checklist before release, then ship through TestFlight beta testing.
MobileKit’s pipeline has a QA stage that runs a checklist like this one for Flutter apps.
Sources read on the day this post was written: Apple: XCTest, Apple: Running tests and interpreting results, Apple: Migrating a test from XCTest, Apple: XCUIAutomation, Flutter: Testing overview, Flutter: Integration tests, Flutter: iOS setup and Claude Code: Manage costs.