Setup (Xcode UI Test Target)
XCUITest is built into Xcode. No extra libraries required.
Add a UI Test target
text
1. In Xcode: File → New → Target
2. Choose: UI Testing Bundle
3. Name it: MyAppUITests
4. Click Finish
Xcode creates:
- MyAppUITests/
- MyAppUITests.swift ← your test file
- MyAppUITestsLaunchTests.swiftBasic test structure
swift
import XCTest
final class LoginTests: XCTestCase {
var app: XCUIApplication!
override func setUpWithError() throws {
continueAfterFailure = false
app = XCUIApplication()
app.launch()
}
override func tearDownWithError() throws {
app = nil
}
func testLoginWithValidCredentials() throws {
let emailField = app.textFields["Email"]
emailField.tap()
emailField.typeText("alice@example.com")
let passwordField = app.secureTextFields["Password"]
passwordField.tap()
passwordField.typeText("pass123")
app.buttons["Login"].tap()
XCTAssertTrue(app.staticTexts["Welcome, Alice"].exists)
}
}