Browse Questions
- SOLID is five principles for writing maintainable, extensible code.
- Copy-on-write means a value type shares its underlying storage with copies until one of them is mutated — only then is a real copy made.
- SPM is Apple's built-in dependency manager for Swift.
- Test coverage measures what percentage of your code is executed by tests.
Answer: Test coverage measures what percentage of your code is executed by tests. Xcode reports it per file and per line.
How to enable in Xcode:
- Edit Scheme → Test → Options
- Enable Code Coverage checkbox
- Run tests (
Cmd+U) - View in Report Navigator → Coverage tab
What to aim for:
- 100% coverage doesn't mean bug-free — tests can pass the line without testing all logic branches
- Focus on business logic, use cases, and ViewModels — 80%+ there is a good target
- Don't obsess over coverage on pure UI code or auto-generated code
Code Example:
func discount(for quantity: Int) -> Double {
if quantity >= 100 { return 0.20 } // line covered?
else if quantity >= 10 { return 0.10 } // line covered?
return 0.0 // line covered?
}
// Need 3 tests to cover all branches:
XCTAssertEqual(discount(for: 100), 0.20)
XCTAssertEqual(discount(for: 10), 0.10)
XCTAssertEqual(discount(for: 1), 0.0)