0
0
iOS Swiftmobile~20 mins

XCTest framework in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XCTest Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when a failing XCTAssert is encountered?
In an XCTest case, what is the behavior when an XCTAssert condition fails during a test method execution?
iOS Swift
func testExample() {
  XCTAssert(1 + 1 == 3)
  print("This line is reached")
}
AThe test continues running, prints the message, but marks as failed at the end.
BThe test immediately stops and marks as failed; the print statement is not executed.
CThe test passes because print executes, ignoring the failed XCTAssert.
DThe test crashes and stops the entire test suite.
Attempts:
2 left
💡 Hint
XCTAssert failures report errors but do not stop the test method immediately.
📝 Syntax
intermediate
2:00remaining
Which code correctly defines a test case class?
Select the correct Swift code snippet that defines a valid XCTestCase subclass with one test method.
A
class MyTests: XCTestCase {
  func exampleTest() {
    XCTAssertTrue(true)
  }
}
B
class MyTests: XCTestCase {
  func testExample() {
    XCTAssertTrue(true)
  }
}
C
class MyTests {
  func testExample() {
    XCTAssertTrue(true)
  }
}
D
class MyTests: XCTestCase {
  func testExample() {
    assert(true)
  }
}
Attempts:
2 left
💡 Hint
Test methods must start with 'test' and class must inherit XCTestCase.
lifecycle
advanced
2:00remaining
What is the order of XCTest lifecycle methods?
Arrange the following XCTest lifecycle methods in the order they are called when running multiple test methods in a test case class.
A2, 3, 1, 4
B2, 1, 3, 4
C1, 2, 4, 3
D1, 2, 3, 4
Attempts:
2 left
💡 Hint
Class methods run once per class; instance methods run per test method.
🔧 Debug
advanced
2:00remaining
Why does this test always pass even if the code is wrong?
Given the test below, why does it always pass even if the function under test returns wrong results? func testSum() { let result = sum(2, 3) XCTAssertEqual(result, 6) }
iOS Swift
func sum(_ a: Int, _ b: Int) -> Int {
  return a + b + 1
}
AThe test passes because XCTAssertEqual is comparing to the wrong expected value (6 instead of 5).
BThe test passes because sum function is correct and returns 6.
CThe test passes because the test method is not named starting with 'test'.
DThe test passes because XCTAssertEqual does not check equality.
Attempts:
2 left
💡 Hint
Check the expected value in XCTAssertEqual carefully.
🧠 Conceptual
expert
2:00remaining
What is the purpose of XCTestExpectation in asynchronous tests?
Why do we use XCTestExpectation in XCTest when testing asynchronous code?
ATo mock asynchronous functions without running real code.
BTo automatically retry failed tests multiple times for flaky network calls.
CTo pause the test until the asynchronous operation completes or times out, ensuring correct test timing.
DTo speed up test execution by skipping asynchronous code.
Attempts:
2 left
💡 Hint
Think about how XCTest knows when async code finishes.