TE

Swift Testing

Swift testing conventions

Details

Language / Topic
swiftSwift
Category
Testing

Rules

balanced
- Use XCTest framework with `XCTestCase` subclasses. Prefix test methods with `test`.
- Use `XCTAssertEqual`, `XCTAssertTrue`, `XCTAssertThrowsError` for assertions.
- Use Swift Testing framework (`@Test`, `#expect`) for new tests — use `@Suite` for grouping and `@Test(arguments:)` for parameterized tests.
- Use `setUp()` and `tearDown()` for test lifecycle management. Use `setUpWithError()` for throwing setup.
- Use `XCTestExpectation` with `wait(for:timeout:)` for testing async operations.
- Use protocol-based dependency injection for testable architecture — mock protocols in tests.
- Name tests descriptively: `testLogin_withInvalidCredentials_shouldReturnError`.