Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Why and What to Test

Why and What to Test

In this lesson — part of Testing Fundamentals — you'll learn why and what to test in Swift and why it matters in real work.

Why it matters

Tests prove your code works and keep it working as you change it.

Key ideas

  • What to test
  • Arrange-Act-Assert
  • Running a test suite
  • Good vs. brittle tests

In practice

Here's how it looks in idiomatic Swift:

import Testing

@Test func addsNumbers() {
    let sum = 2 + 3
    #expect(sum == 5)
}

Swift note: The current standard is the Swift Testing framework: annotate functions with @Test and assert via the #expect macro (XCTest with XCTAssertEqual is the older alternative).

Try it yourself

Exercise: In Swift, write three tests for a function that reverses a string.

Recap

You now understand why and what to test and can apply it in Swift. Mark this lesson complete and continue to the next one.