Swift testing uses the built-in Testing framework (Swift 6+) and XCTest in Xcode projects—assertions, async tests, and package test targets via SPM.
XCTest pattern (local Xcode)
import XCTest
final class MathTests: XCTestCase {
func testAdd() {
XCTAssertEqual(add(2, 3), 5)
}
}
Playground simulation
The sandbox simulates test ideas with plain functions and print()—run swift test locally on SPM packages.
Important interview questions and answers
- Q: Unit vs UI tests?
A: Unit tests verify logic quickly; UI tests automate simulator interactions—slower, run in CI nightly or pre-release. - Q: Testing async code?
A: Use async test methods orawaitinside XCTest; Testing framework supports native async expectations.
Self-check
- What CLI command runs SPM tests?
- What does XCTAssertEqual verify?
Tip: Run swift test on SPM packages—add XCTest targets for logic and keep UI tests separate.
Interview prep
- Unit vs UI tests?
Unit for logic; UI tests automate simulator—slower, fewer in CI.
- Async tests?
async test methods or await inside XCTest.