Challenge - 5 Problems
XCTest Mastery
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 XCTest code snippet. What will be the result when this test runs?
Swift
func testSum() { let result = 2 + 3 XCTAssertEqual(result, 5) print("Test passed") }
Attempts:
2 left
💡 Hint
XCTAssertEqual checks if two values are equal. If they are, the test continues.
✗ Incorrect
The assertion XCTAssertEqual(result, 5) passes because 2 + 3 equals 5. The print statement runs, outputting 'Test passed'.
🧠 Conceptual
intermediate1:30remaining
What is the purpose of the setUp() method in XCTest?
In XCTest, what does the setUp() method do?
Attempts:
2 left
💡 Hint
Think about preparing fresh conditions for every test.
✗ Incorrect
setUp() runs before each test method to ensure each test starts with a clean state.
🔧 Debug
advanced2:00remaining
Why does this XCTest fail to compile?
Identify the reason this XCTest code does not compile.
Swift
class MyTests: XCTestCase { func testExample() { XCTAssertTrue(5 > 3) } func setUp() { print("Setup called") } }
Attempts:
2 left
💡 Hint
Check method overriding rules in Swift classes.
✗ Incorrect
setUp() overrides a method in XCTestCase and must be marked with override to compile.
📝 Syntax
advanced1:30remaining
Which option correctly defines a test method in XCTest?
Choose the correct syntax for a test method in an XCTestCase subclass.
Attempts:
2 left
💡 Hint
Test methods should not return values or be async/throws by default.
✗ Incorrect
Test methods in XCTest are void functions without async or throws unless explicitly supported.
🚀 Application
expert2:30remaining
How many tests will run and what is the output?
Given this XCTestCase subclass, how many tests will run and what will be printed?
Swift
class SampleTests: XCTestCase { override func setUp() { super.setUp() print("Setup") } func testOne() { print("Test One") XCTAssertTrue(true) } func testTwo() { print("Test Two") XCTAssertTrue(false) } func helper() { print("Helper") } }
Attempts:
2 left
💡 Hint
Only methods starting with 'test' run as tests. setUp runs before each test.
✗ Incorrect
Only testOne and testTwo run. setUp prints before each. testTwo fails due to XCTAssertTrue(false). helper() is not a test.