Unit test versus UI test in Xcode
The scope and tooling difference between test types.
Unit tests (XCTest) exercise code logic in-process and fast; UI tests (XCUITest) drive the app through the accessibility layer, slower and end-to-end.
WHAT THIS TESTS This confirms you understand the two layers of Xcode testing, their speed and reliability tradeoffs, and can pick the right tool for a given verification.
A GOOD ANSWER COVERS A unit test, written with the XCTest framework, runs inside the app's process and calls your code directly. It verifies a unit of logic, a function, struct, or class, in isolation, is fast, deterministic, and easy to run in large numbers. A UI test uses the XCUITest API, primarily XCUIApplication and XCUIElement queries, to launch the app as a separate process and drive it the way a user would, tapping buttons and typing, observing results through the accessibility layer. UI tests validate end-to-end flows across screens but are slower, more fragile, and depend on stable accessibility identifiers. The rule of thumb is to unit-test logic and use a small number of UI tests for critical journeys.
COMMON WRONG ANSWERS Saying UI tests can access internal variables directly; they cannot, they only see the UI via accessibility. Claiming unit tests can verify navigation between screens with real taps. Treating the two as interchangeable. Suggesting you should write mostly UI tests, ignoring the test pyramid.
LIKELY FOLLOW-UPS How the test pyramid argues for many unit tests and few UI tests. The role of accessibility identifiers in making UI tests robust. Where integration tests fit. How XCTestExpectation handles async. Why UI tests cannot import @testable internals.
ONE CONCRETE EXAMPLE A sorting algorithm is pure logic, so you write a unit test: call sort on a known unsorted array and XCTAssertEqual the result to the expected sorted array, plus edge cases like empty and single-element inputs; it runs in milliseconds. A login flow spans a username field, a password field, a button, and a navigation result, so you write a UI test: app.launch(), type into the fields by accessibility identifier, tap Sign In, and assert the home screen's element exists. The first verifies correctness of code; the second verifies the user-facing journey works.
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.