Authors and runs XCTest UI + unit tests for macOS desktop apps - the Apple-first-party test framework that ships with Xcode. Covers the `XCTestCase` subclass + `test*` method-naming convention, `XCUIApplication` / `XCUIElement` / `XCUIElementQuery` for UI tests, accessibility-identifier-based locators (the stable replacement for label-based queries), `XCTAssert*` macros, `measureBlock:` for performance regressions, and `xcodebuild test` for CI execution. Use when the macOS app is built with Xcode and the test target is in-tree alongside the app - for cross-OS sharing see Appium Mac2 driver as a separate path.
70
88%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
View guide
High
Do not use without reviewing
XCTest is the first-party test framework bundled with Xcode, used for unit,
performance, and UI tests. Per Apple's Testing with Xcode - UI Testing
chapter, "UI Testing in Xcode rests on two core technologies: the
XCTest framework and Accessibility," layered via three classes -
XCUIApplication, XCUIElement, and XCUIElementQuery (appleuit).
This skill wraps XCTest for macOS desktop apps. iOS / iPadOS use the same APIs with different launch + simulator semantics and are out of scope here.
Strategic frame: desktop-test-strategy-reference places macOS in the
three-OS landscape (UIA on Windows, XCTest on macOS, AT-SPI on Linux); the
locator strategy converges on accessibility identifiers across all three.
xcodebuild test).XCTestCase base).In Xcode: File → New → Target → UI Testing Bundle. The
generated test class inherits from XCTestCase and ships with a
boilerplate setUp that launches the app
(appleuit):
import XCTest
final class CheckoutUITests: XCTestCase {
override func setUpWithError() throws {
continueAfterFailure = false // recommended by Apple - UI steps depend on prior steps
XCUIApplication().launch()
}
func testCheckoutHappyPath() throws {
// Test body - see Step 3
}
}Per appleuit:
"Set continueAfterFailure to NO ensures tests stop on first failure (recommended since UI test steps are dependent)."
Per applewt, a test method must "begin with the prefix test", take
no parameters, and return void. Lifecycle order:
+ (void)setUp) - once before all tests.setUp → test method → tearDown.+ (void)tearDown) - once after all tests.The portable lesson per
desktop-test-strategy-reference:
prefer accessibilityIdentifier over visible labels. Set the
identifier in app code:
// In app code (SwiftUI)
Button("Sign In") { … }
.accessibilityIdentifier("signInButton")
// Or in AppKit
signInButton.setAccessibilityIdentifier("signInButton")Then in tests:
func testCheckoutHappyPath() throws {
let app = XCUIApplication()
app.launch()
// Query → Interact → Assert (the canonical XCUI pattern per [appleuit])
app.textFields["emailField"].tap()
app.textFields["emailField"].typeText("user@example.com")
app.secureTextFields["passwordField"].tap()
app.secureTextFields["passwordField"].typeText("s3cret")
app.buttons["signInButton"].tap()
// Wait + assert
let welcomeHeading = app.staticTexts["welcomeHeading"]
XCTAssertTrue(welcomeHeading.waitForExistence(timeout: 5))
XCTAssertEqual(welcomeHeading.label, "Welcome, user@example.com")
}Per appleuit, the canonical pattern is:
"Use an XCUIElementQuery to find an XCUIElement. Synthesize an event and send it to the XCUIElement. Use an assertion to compare the state of the XCUIElement against an expected reference state."
waitForExistence(timeout:) is the documented predicate-polling
primitive used in place of fixed sleeps (stable identifier in Apple's
XCUIElement reference; cited inline by name).
Per applewt, XCTAssert macros fall into five categories:
| Category | Macros |
|---|---|
| Equality | XCTAssertEqual, XCTAssertEqualObjects, XCTAssertNotEqual, XCTAssertGreaterThan, XCTAssertEqualWithAccuracy |
| Boolean | XCTAssertTrue, XCTAssertFalse |
| Nil | XCTAssertNil, XCTAssertNotNil |
| Exception | XCTAssertThrows, XCTAssertThrowsSpecific, XCTAssertNoThrow |
| Unconditional fail | XCTFail |
All accept an optional format string for the failure message (applewt).
Per applewt, performance tests "run a code block 10 times, collecting average execution time and standard deviation":
func testAdditionPerformance() throws {
self.measure {
var sum = 0
for i in 0..<100_000 { sum += i }
XCTAssertEqual(sum, 4_999_950_000)
}
}Per applewt: "Performance tests report failure on first run until a baseline is set. Baselines are stored per-device- configuration." Practical implication: the first CI run on a new Mac architecture (Intel → Apple Silicon migration) fails until the baseline is committed.
Per appleuit, Xcode's "UI Recording" workflow generates test code from interactive use:
XCTAssert assertions to validate behaviour.Treat recordings as a starting point - the generated locator
chain tends to rely on label paths rather than
accessibilityIdentifier. Refactor to identifier-based queries (per
the desktop-test-strategy-reference
locator table) before checking in.
From the command line:
# Run the full test bundle
xcodebuild test \
-project MyApp.xcodeproj \
-scheme MyApp \
-destination 'platform=macOS' \
-resultBundlePath build/result.xcresult
# Run a single test class
xcodebuild test \
-project MyApp.xcodeproj \
-scheme MyApp \
-destination 'platform=macOS' \
-only-testing:MyAppUITests/CheckoutUITests
# Run a single test method
xcodebuild test \
-project MyApp.xcodeproj \
-scheme MyApp \
-destination 'platform=macOS' \
-only-testing:MyAppUITests/CheckoutUITests/testCheckoutHappyPath-destination 'platform=macOS' targets the host Mac. -resultBundlePath
writes a .xcresult bundle that contains attachments (screenshots
on failure, performance metrics, logs) - the canonical artefact for
post-mortem.
Query the .xcresult bundle with xcrun xcresulttool (JSON summary, attachment
extraction), then convert to JUnit XML via the open-source xcresultparser for
junit-xml-analysis. Commands:
references/ci-and-results.md.
Hosted macOS runners are interactive, so xcodebuild test UI launches need no
extra display setup; self-hosted headless Macs need an attached console or VNC
session (XCTest UI cannot run under launchd alone). Full workflow:
references/ci-and-results.md.
| Anti-pattern | Why it fails | Fix |
|---|---|---|
Querying by visible label (app.buttons["Sign In"]) | Localisation collapses the locator (Spanish, Japanese builds break) | accessibilityIdentifier per Step 3 (per desktop-test-strategy-reference) |
XCUIApplication().launch() inside every test method | Per-method app launch is slow + redundant | Launch in setUp (appleuit) |
Thread.sleep(2.0) between actions | Flaky on slow CI, slow on fast | waitForExistence(timeout:) predicate polling (Step 3) |
continueAfterFailure = true for UI tests | First failure cascades into confusing follow-on failures | continueAfterFailure = false per appleuit |
| Mixing UI + unit + performance in one test method | Result attribution is opaque | One method per behaviour; share setup via setUp (applewt) |
| Performance baseline committed from a developer Mac | Baselines are device-specific; CI runner is a different device | Commit baselines from the CI runner that will gate the PR (applewt) |
| Recording-and-keep workflow without identifier refactor | Generated label-path locators are brittle | Refactor recordings to accessibilityIdentifier (Step 6) |
XCUIApplication() without .launch() | The query tree is empty; element lookups time out | Always launch() before any query (appleuit) |
desktop-test-strategy-reference
matrix).developer.apple.com/documentation/xctest SPA shell returns
without body content via automated fetch; this skill cites
Apple's stable library/archive testing-with-xcode chapter
(appleuit, applewt) for prose and treats
per-API surface (XCUIApplication, XCUIElement, XCUIElementQuery,
waitForExistence(timeout:)) as stable identifiers in Apple's
XCUIElement reference. Document this in the PR description if the
reviewer asks for a click-through link.XCUICoordinate interactions; complex multi-app
flows often need Apple Mac2 driver via Appium for parity with
Windows / Linux test sources.desktop-test-strategy-reference.XCTestObservationCenter
for in-process observation rather than out-of-process file diffs.desktop-test-strategy-reference.winappdriver (Windows UIA),
at-spi-linux (Linux AT-SPI),
qt-test-framework (Qt in-process).junit-xml-analysis.