Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a test case class in XCTest.
iOS Swift
import XCTest class MyTests: [1] { }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using UIViewController instead of XCTestCase.
Forgetting to inherit from any class.
✗ Incorrect
Test case classes in XCTest must inherit from XCTestCase to gain testing capabilities.
2fill in blank
mediumComplete the code to define a test method that XCTest will run.
iOS Swift
func [1]() { XCTAssertEqual(2 + 2, 4) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Naming test methods without the 'test' prefix.
Using spaces or special characters in method names.
✗ Incorrect
Test methods must start with 'test' so XCTest recognizes and runs them automatically.
3fill in blank
hardFix the error in the assertion to check if a value is true.
iOS Swift
XCTAssert[1](isReady) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using XCTAssertFalse when expecting true.
Using XCTAssertEqual for boolean checks.
✗ Incorrect
To assert a boolean is true, use XCTAssertTrue in XCTest.
4fill in blank
hardFill both blanks to set up and tear down test resources.
iOS Swift
override func [1]() { super.[1]() // setup code here } override func [2]() { super.[2]() // teardown code here }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using viewDidLoad instead of setUp or tearDown.
Not calling super in overridden methods.
✗ Incorrect
setUp() runs before each test, tearDown() runs after each test to prepare and clean up.
5fill in blank
hardFill all three blanks to write a test that expects an error to be thrown.
iOS Swift
func testError() [3] { XCTAssertThrows[1](try someFunctionThat[2](), "Expected error") // Additional checks can be done here }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using XCTAssertThrow instead of XCTAssertThrowsError.
Omitting 'try' before the function call.
Not marking the test method with 'throws'.
✗ Incorrect
XCTAssertThrowsError checks if a function throws an error; the function call uses 'try' and the method name uses 'throws'.