0
0
Swiftprogramming~20 mins

XCTest framework basics in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XCTest Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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")
}
ACompilation error due to missing import XCTest
BTest failed with XCTAssertEqual failure
CRuntime error: undefined variable 'result'
DTest passed
Attempts:
2 left
💡 Hint
XCTAssertEqual checks if two values are equal. If they are, the test continues.
🧠 Conceptual
intermediate
1:30remaining
What is the purpose of the setUp() method in XCTest?
In XCTest, what does the setUp() method do?
AIt runs once before all tests in the class to set up shared resources.
BIt runs after each test method to clean up resources.
CIt runs before each test method to prepare the test environment.
DIt runs once after all tests in the class to tear down shared resources.
Attempts:
2 left
💡 Hint
Think about preparing fresh conditions for every test.
🔧 Debug
advanced
2: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")
    }
}
AXCTAssertTrue requires a message parameter.
BsetUp() must be marked with override keyword.
CtestExample() must return a Bool value.
DXCTestCase class cannot have more than one method.
Attempts:
2 left
💡 Hint
Check method overriding rules in Swift classes.
📝 Syntax
advanced
1:30remaining
Which option correctly defines a test method in XCTest?
Choose the correct syntax for a test method in an XCTestCase subclass.
Afunc testAddition() { XCTAssertEqual(1+1, 2) }
Bfunc testAddition() throws { XCTAssertEqual(1+1, 2) }
Cfunc testAddition() -> Bool { XCTAssertEqual(1+1, 2); return true }
Dfunc testAddition() async { XCTAssertEqual(1+1, 2) }
Attempts:
2 left
💡 Hint
Test methods should not return values or be async/throws by default.
🚀 Application
expert
2: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")
    }
}
A2 tests run; output: Setup, Test One, Setup, Test Two; testTwo fails
B3 tests run; output: Setup, Test One, Setup, Test Two, Setup, Helper; testTwo fails
C2 tests run; output: Setup, Test One, Setup, Test Two; all tests pass
D1 test runs; output: Setup, Test One; testOne passes
Attempts:
2 left
💡 Hint
Only methods starting with 'test' run as tests. setUp runs before each test.