Activity 1
Test a validation rule
Write tests for valid, empty, and too-long usernames. Give each test a behavior-focused name.
Hint: Include boundary values, not only an average valid value.
Swift Fundamentals
Verify behavior with focused tests and investigate failures systematically with Xcode debugging tools.
Before you begin
Learning stage
A test provides repeatable evidence that code behaves as intended. Good unit tests choose one behavior, arrange a meaningful state, perform an action, and verify an observable result. Test outcomes and boundaries rather than private implementation details so the code can evolve without rewriting every test.
Debugging is a disciplined search for the first point where reality differs from expectation. Reproduce the issue, reduce it, form a hypothesis, inspect state with breakpoints or logs, and verify the fix with a test when possible. The compiler, debugger, console, call stack, and memory tools each answer different questions.
Learning stage
The pure function is easy to test because its result depends only on explicit inputs.
import Testing
func finalPrice(subtotal: Double, discount: Double) -> Double {
subtotal * (1 - discount)
}
@Test("A twenty-percent discount reduces the subtotal")
func appliesDiscount() {
let result = finalPrice(subtotal: 100, discount: 0.20)
#expect(result == 80)
}Learning stage
These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.
Learning stage
Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.
Learning stage
Work through these activities in a small Xcode project. No embedded playground is required on this site.
Activity 1
Write tests for valid, empty, and too-long usernames. Give each test a behavior-focused name.
Hint: Include boundary values, not only an average valid value.
Activity 2
For a crash that happens only with an empty list, write the reproduction, hypothesis, breakpoint location, state to inspect, fix, and regression test.
Hint: Identify the earliest assumption that becomes false.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
One observable behavior
Focused tests make failures easier to interpret and maintain.
QUESTION 2
Reproduce it reliably and record the expected versus actual behavior.
Without a reproduction, changes cannot be evaluated confidently.
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: Explicit inputs, small responsibilities, controlled dependencies, and observable outputs.
Pure transformations are naturally testable. For stateful work, inject clocks, clients, or stores behind focused boundaries instead of reaching directly into global state. Tests should verify the public contract and use controlled alternatives for slow or unpredictable dependencies.
Possible follow-ups
Learning stage
Testing and debugging are complementary evidence-gathering skills. Write focused tests for contracts, reproduce failures before changing code, inspect state systematically, and preserve important fixes with regression coverage.