This quiz focuses on the essential principles and practical concepts of unit testing in iOS applications using XCTest. Assess your understanding of test structure, assertions, test practices, and the basics needed for reliable, maintainable code in Swift-based projects.
Which class do you typically subclass to create a test case for unit testing in iOS using XCTest?
Explanation: XCTestCase is the standard base class provided for writing unit tests using XCTest. It provides essential methods and lifecycle hooks for running and organizing your tests. UIViewController and TestClass are unrelated to test cases, while UnitTestBase is not a recognized base class in this context.
How should the name of a test method in XCTest typically start to be recognized and executed as a test?
Explanation: Test methods in XCTest must begin with the prefix 'test' to be automatically discovered and run by the test runner. The prefixes 'run', 'verify', or 'assert' are not recognized by XCTest for this purpose and would not make the method executable as a test.
What is the main role of the setUp() method in an XCTestCase subclass?
Explanation: The setUp() method is called before each individual test method, ensuring that the environment is properly prepared for the next test. Cleaning up is handled by the tearDown() method, not setUp(). setUp() does not run tests or display results.
Which assertion would you use to verify that two values are equal in an XCTest unit test?
Explanation: XCTAssertEqual is specifically designed to assert that two provided values are the same. XCTAssertNil checks if a value is nil, XCTAssertTrue asserts a boolean is true, and XCTAssertFail unconditionally fails a test, so they are not correct in this context.
What happens if a test case contains multiple test methods in XCTest?
Explanation: In XCTest, each test method in a test case runs independently and results are shown for each. The other options incorrectly describe the behavior: only the first is not run, methods are not merged, and passes do not cause failures.
What is a key difference between setUpWithError() and setUp() in XCTest?
Explanation: setUpWithError() supports throwing errors, allowing better error handling during setup. setUp() is called before each test but cannot throw. Neither method runs after tests nor only once per class, and setUpWithError() does not require parameters.
Which XCTest feature allows you to properly test asynchronous code?
Explanation: Expectations let you wait for asynchronous operations to complete in XCTest, ensuring the test waits for a callback or completion. Assertions simply verify conditions, breakpoints are for debugging, and stubs are used for controlling dependencies, not waiting.
Why would you override the tearDown() method in an XCTestCase subclass?
Explanation: tearDown() is called after each test method to tear down or reset anything set up during the test. It does not handle setup operations, long-running tasks after all tests, or verify outputs; those responsibilities belong elsewhere.
If you want to confirm that a condition is not true in your test, which assertion should you use?
Explanation: XCTAssertFalse checks if a boolean condition is false. XCTAssertNil checks for nil values, XCTAssertEqual compares equality, and XCTAssertThrows checks if an expression throws an error. Only XCTAssertFalse matches the requirement.
What does test coverage measure in the context of Xcode's unit testing tools?
Explanation: Test coverage refers to the proportion of your codebase that is executed when running your tests. It does not measure the number of assertions, test duration, or bugs detected directly. High coverage can indicate good test completeness, though not necessarily test quality.