iOS Development iOS Testing and Debugging 1 — Questions and Answers
Question 1: What framework does Apple provide natively for unit testing in iOS apps?
- XCTest (Correct answer)
- JUnit
- Mocha
- TestNG
Correct answer: XCTest
XCTest is Apple's built-in testing framework for writing unit, integration, and UI tests in iOS.
Question 2: Which method in XCTestCase is called before each individual test method runs?
- tearDown()
- setUp() (Correct answer)
- testStart()
- prepareTest()
Correct answer: setUp()
setUp() is called before each test method in XCTestCase to prepare a clean test environment.
Question 3: What does the XCTAssertEqual function verify in an XCTest unit test?
- That a value is not nil
- That a boolean expression is true
- That two values are equal (Correct answer)
- That a function throws an error
Correct answer: That two values are equal
XCTAssertEqual checks that two values are equal and fails the test if they differ.
Question 4: What is the primary purpose of a mock object in iOS unit testing?
- To simulate live network calls with real data
- To replace real dependencies with controlled test doubles (Correct answer)
- To automatically generate random test data
- To record and replay UI interactions
Correct answer: To replace real dependencies with controlled test doubles
Mock objects replace real dependencies with controlled substitutes so the code under test can be verified in isolation.
Question 5: Which Xcode keyboard shortcut runs all unit and UI tests in the current scheme?
- Cmd+B
- Cmd+U (Correct answer)
- Cmd+R
- Cmd+T
Correct answer: Cmd+U
Cmd+U triggers a full test run for all test targets in the active Xcode scheme.
Question 6: What does code coverage measure in the context of iOS testing?
- The total number of lines of code in the app
- The percentage of source code executed during tests (Correct answer)
- The number of test cases written for a target
- The memory consumed during a test run
Correct answer: The percentage of source code executed during tests
Code coverage measures what percentage of your source code is actually executed when the test suite runs.
Question 7: What is the defining principle of Test-Driven Development (TDD) in iOS?
- Writing tests after the app is fully implemented
- Writing tests only for UI components
- Writing failing tests before writing the implementation code (Correct answer)
- Using third-party tools to auto-generate test cases
Correct answer: Writing failing tests before writing the implementation code
TDD requires writing a failing test first, then writing the minimum production code to make it pass.
What framework does Apple provide natively for unit testing in iOS apps?