Challenge - 5 Problems
XCTest Assertions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this XCTest assertion?
Consider the following Swift test code snippet using XCTest assertions. What will happen when this test runs?
Swift
func testSum() { let result = 2 + 3 XCTAssertEqual(result, 5) }
Attempts:
2 left
💡 Hint
XCTAssertEqual checks if two values are equal and passes if they are.
✗ Incorrect
The expression 2 + 3 equals 5, so XCTAssertEqual(result, 5) passes and the test succeeds.
❓ Predict Output
intermediate2:00remaining
What happens when XCTAssertTrue fails?
Look at this Swift test function. What is the result when it runs?
Swift
func testIsEven() { let number = 3 XCTAssertTrue(number % 2 == 0) }
Attempts:
2 left
💡 Hint
XCTAssertTrue expects the condition to be true to pass.
✗ Incorrect
3 % 2 equals 1, which is not zero, so the condition is false and the assertion fails.
🔧 Debug
advanced2:00remaining
Identify the error in this XCTest assertion usage
This Swift test code is intended to check if two strings are equal. What error will occur when running this test?
Swift
func testGreeting() { let greeting = "Hello" XCTAssertEqual(greeting, "hello") }
Attempts:
2 left
💡 Hint
XCTAssertEqual compares values exactly, including case for strings.
✗ Incorrect
The strings differ in case, so XCTAssertEqual fails the test with an assertion failure.
📝 Syntax
advanced2:00remaining
Which option contains a syntax error in XCTest assertion?
Which of the following Swift test code snippets will cause a syntax error?
Attempts:
2 left
💡 Hint
Check for misplaced keywords or missing parentheses.
✗ Incorrect
Option C has incorrect syntax: 'else { return }' is invalid inside XCTAssertEqual call.
🚀 Application
expert3:00remaining
How many assertions will fail in this test function?
Analyze the following Swift test function. How many assertions will fail when this test runs?
Swift
func testMultipleAssertions() { XCTAssertEqual(10, 10) XCTAssertTrue(5 > 10) XCTAssertEqual("Swift", "swift") XCTAssertTrue(3 == 3) }
Attempts:
2 left
💡 Hint
Check each assertion's condition carefully.
✗ Incorrect
XCTAssertEqual(10, 10) passes; XCTAssertTrue(5 > 10) fails; XCTAssertEqual("Swift", "swift") fails due to case; XCTAssertTrue(3 == 3) passes. So 2 fail.