Swift Quality Control & Assurance 5 — Questions and Answers
Question 1: What is the recommended way to test asynchronous Swift code using async/await in XCTest?
- Use XCTestExpectation and waitForExpectations
- Mark the test function as async and use await directly (Correct answer)
- Run the async code on a background thread with DispatchQueue
- Use a semaphore to block until the async task completes
Correct answer: Mark the test function as async and use await directly
Swift 5.5+ XCTest supports async test functions natively — mark the test `async` and use `await` directly without needing XCTestExpectation.
Question 2: What is 'boundary value analysis' in software quality assurance for Swift apps?
- Testing only the most common user inputs
- Testing values at the edges of valid input ranges where bugs are most likely (Correct answer)
- Analyzing memory boundaries during runtime
- Testing app behavior when storage boundaries are reached
Correct answer: Testing values at the edges of valid input ranges where bugs are most likely
Boundary value analysis focuses test inputs on the minimum, maximum, and just-outside-range values where off-by-one errors commonly occur.
Question 3: In a Swift CI/CD pipeline, what is the purpose of a 'gate' on test results?
- To slow down builds to prevent server overload
- To block merging or deploying code if tests fail (Correct answer)
- To require manual approval before tests run
- To limit how many tests can run simultaneously
Correct answer: To block merging or deploying code if tests fail
A quality gate blocks the pipeline from proceeding to merge or deploy if the test suite reports failures, enforcing code quality standards.
Question 4: Which Xcode instrument is best suited for detecting Swift memory leaks caused by retain cycles?
- Time Profiler
- Leaks instrument (Correct answer)
- Network instrument
- Core Data instrument
Correct answer: Leaks instrument
The Leaks instrument in Xcode Instruments specifically detects memory that has been allocated but can no longer be reached, including retain cycle leaks.
Question 5: What does 'XCTAssertNil' test for in a Swift unit test?
- That an optional contains a value
- That a given expression evaluates to nil (Correct answer)
- That a string is empty
- That a collection has no elements
Correct answer: That a given expression evaluates to nil
XCTAssertNil fails the test if the provided expression is not nil, verifying that an optional is in its absent (nil) state.
Question 6: In Swift testing, what is the 'Arrange-Act-Assert' (AAA) pattern?
- A method for organizing test files into three directories
- A structure where you set up state, perform an action, then verify the result (Correct answer)
- A CI pipeline with three sequential stages
- An Xcode template for generating three test variants
Correct answer: A structure where you set up state, perform an action, then verify the result
AAA structures each test into three phases: Arrange (set up preconditions), Act (execute the code under test), and Assert (verify outcomes).
Question 7: When should you use XCTSkip in a Swift test?
- When a test is expected to always fail
- When a test should be conditionally skipped based on runtime conditions (Correct answer)
- To permanently disable a broken test
- To run a test only in release builds
Correct answer: When a test should be conditionally skipped based on runtime conditions
XCTSkip throws an error that marks the test as skipped rather than failed, useful for conditions like unavailable OS features or specific device requirements.
What is the recommended way to test asynchronous Swift code using async/await in XCTest?