Swift Quality Control & Assurance 4 — Questions and Answers
Question 1: In Swift, what is 'dependency injection' and why is it important for testability?
- Automatically importing frameworks at build time
- Passing dependencies into an object rather than creating them internally, enabling substitution in tests (Correct answer)
- Injecting code into a running process for debugging
- A method of managing Swift packages
Correct answer: Passing dependencies into an object rather than creating them internally, enabling substitution in tests
Dependency injection passes dependencies (like network clients or databases) from outside, allowing tests to supply mock substitutes instead of real implementations.
Question 2: What is snapshot testing in Swift iOS development primarily used for?
- Recording CPU usage over time
- Capturing and comparing UI renderings to detect visual regressions (Correct answer)
- Testing app performance under heavy load
- Validating network response payloads
Correct answer: Capturing and comparing UI renderings to detect visual regressions
Snapshot testing captures a reference image of a UI component and compares future renders against it to catch unintended visual changes.
Question 3: Which sanitizer in Xcode detects when Swift code accesses memory it no longer owns?
- Thread Sanitizer
- Address Sanitizer (Correct answer)
- Undefined Behavior Sanitizer
- Main Thread Checker
Correct answer: Address Sanitizer
Address Sanitizer detects memory errors like use-after-free and buffer overflows by tracking memory access patterns at runtime.
Question 4: In the context of Swift QA, what does 'regression testing' mean?
- Testing only newly added features
- Re-running existing tests to ensure past bugs have not re-appeared after changes (Correct answer)
- Testing the app on older OS versions only
- Measuring app startup time reduction
Correct answer: Re-running existing tests to ensure past bugs have not re-appeared after changes
Regression testing runs previously passing tests after code changes to verify that existing functionality has not been broken.
Question 5: Which XCTest method is called once before ALL test methods in a test class run?
- setUp()
- setUpWithError()
- class func setUp() (Correct answer)
- tearDown()
Correct answer: class func setUp()
The class-level `class func setUp()` is called once before any instance test methods run, unlike instance `setUp()` which runs before each test.
Question 6: What is the role of 'test doubles' in Swift unit testing quality assurance?
- Running each test twice to check for flakiness
- Objects that stand in for real dependencies during testing (Correct answer)
- Having two developers write the same test independently
- Duplicating tests across multiple test targets
Correct answer: Objects that stand in for real dependencies during testing
Test doubles (stubs, mocks, fakes, spies) are objects that substitute for real dependencies to enable controlled, isolated unit tests.
Question 7: In Xcode, enabling the 'Main Thread Checker' diagnostic helps catch which category of bug?
- Memory leaks caused by strong reference cycles
- UI updates being performed on background threads (Correct answer)
- Force-unwrapping nil optionals
- Integer overflow in arithmetic operations
Correct answer: UI updates being performed on background threads
The Main Thread Checker detects UIKit/AppKit API calls made on background threads, which can cause visual glitches and crashes.
In Swift, what is 'dependency injection' and why is it important for testability?