Challenge - 5 Problems
XCTest Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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")
}Attempts:
2 left
💡 Hint
XCTAssert failures report errors but do not stop the test method immediately.
✗ Incorrect
XCTAssert failures record a failure but allow the test method to continue running. This means subsequent lines execute, but the test is marked as failed overall.
📝 Syntax
intermediate2: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.
Attempts:
2 left
💡 Hint
Test methods must start with 'test' and class must inherit XCTestCase.
✗ Incorrect
Option B correctly inherits XCTestCase and defines a method starting with 'test'. Option B lacks inheritance, C method name does not start with 'test', D uses assert instead of XCTAssertTrue.
❓ lifecycle
advanced2: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.
Attempts:
2 left
💡 Hint
Class methods run once per class; instance methods run per test method.
✗ Incorrect
setUp() class method runs once before all tests, setUp() instance method runs before each test, tearDown() instance method runs after each test, and tearDown() class method runs once after all tests.
🔧 Debug
advanced2: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 }
Attempts:
2 left
💡 Hint
Check the expected value in XCTAssertEqual carefully.
✗ Incorrect
The sum function incorrectly returns 6 for inputs 2 and 3 (should be 5), but the test expects 6, so the test passes even though the function under test returns wrong results.
🧠 Conceptual
expert2:00remaining
What is the purpose of XCTestExpectation in asynchronous tests?
Why do we use XCTestExpectation in XCTest when testing asynchronous code?
Attempts:
2 left
💡 Hint
Think about how XCTest knows when async code finishes.
✗ Incorrect
XCTestExpectation allows the test to wait for async code to finish or timeout, so tests can verify results after asynchronous operations complete.