Bird
Raised Fist0
Swiftprogramming~20 mins

Assertions (XCTAssertEqual, XCTAssertTrue) in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
XCTest Assertions Master
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 Swift test code snippet using XCTest assertions. What will happen when this test runs?
Swift
func testSum() {
    let result = 2 + 3
    XCTAssertEqual(result, 5)
}
AThe test passes without any failure.
BThe test fails with an assertion failure because 2 + 3 is not equal to 5.
CThe test causes a runtime error due to incorrect assertion usage.
DThe test is skipped because XCTAssertEqual is not a valid assertion.
Attempts:
2 left
💡 Hint
XCTAssertEqual checks if two values are equal and passes if they are.
❓ Predict Output
intermediate
2: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)
}
AThe test fails with an assertion failure because 3 is not even.
BThe test is ignored because XCTAssertTrue is deprecated.
CThe test causes a compile-time error due to invalid syntax.
DThe test passes because 3 is an even number.
Attempts:
2 left
💡 Hint
XCTAssertTrue expects the condition to be true to pass.
🔧 Debug
advanced
2: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")
}
AThe test causes a runtime crash due to type mismatch.
BThe test fails because XCTAssertEqual is case-sensitive and "Hello" != "hello".
CThe test passes because XCTAssertEqual ignores case differences.
DThe test causes a compile-time error due to missing import XCTest.
Attempts:
2 left
💡 Hint
XCTAssertEqual compares values exactly, including case for strings.
📝 Syntax
advanced
2:00remaining
Which option contains a syntax error in XCTest assertion?
Which of the following Swift test code snippets will cause a syntax error?
AXCTAssertEqual(5, 5)
BXCTAssertTrue(3 > 2)
CXCTAssertEqual(4, 4 else { return })
DXCTAssertTrue(true)
Attempts:
2 left
💡 Hint
Check for misplaced keywords or missing parentheses.
🚀 Application
expert
3: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)
}
A1
B0
C3
D2
Attempts:
2 left
💡 Hint
Check each assertion's condition carefully.

Practice

(1/5)
1. What does XCTAssertEqual(a, b) check in a Swift test?
easy
A. It checks if a and b are equal.
B. It checks if a is greater than b.
C. It checks if a is less than b.
D. It checks if a is not equal to b.

Solution

  1. Step 1: Understand XCTAssertEqual purpose

    XCTAssertEqual is used to compare two values for equality in tests.
  2. Step 2: Match the function behavior

    It returns success if both values are equal, otherwise it fails the test.
  3. Final Answer:

    It checks if a and b are equal. -> Option A
  4. Quick Check:

    XCTAssertEqual checks equality = B [OK]
Hint: XCTAssertEqual means values must be the same [OK]
Common Mistakes:
  • Confusing XCTAssertEqual with greater or less than checks
  • Thinking it checks inequality
  • Mixing it up with XCTAssertTrue
2. Which of the following is the correct syntax to check if a boolean variable isReady is true using XCTest?
easy
A. XCTAssertFalse(isReady)
B. XCTAssertEqual(isReady == true)
C. XCTAssert(isReady == true)
D. XCTAssertTrue(isReady)

Solution

  1. Step 1: Identify the correct assertion for boolean true

    XCTAssertTrue is designed to check if a condition is true.
  2. Step 2: Compare options

    XCTAssertTrue(isReady) uses XCTAssertTrue directly on isReady, which is the simplest and correct syntax.
  3. Final Answer:

    XCTAssertTrue(isReady) -> Option D
  4. Quick Check:

    Use XCTAssertTrue for true checks = A [OK]
Hint: Use XCTAssertTrue(variable) to check if true [OK]
Common Mistakes:
  • Using XCTAssertFalse instead of XCTAssertTrue
  • Using XCTAssertEqual unnecessarily for booleans
  • Writing XCTAssert without True or False
3. What will happen when this test runs?
let result = 5 + 3
XCTAssertEqual(result, 8)
medium
A. The test will pass because 5 + 3 equals 8.
B. The test will fail because 5 + 3 does not equal 8.
C. The test will cause a syntax error.
D. The test will crash at runtime.

Solution

  1. Step 1: Calculate the expression

    5 + 3 equals 8, so result is 8.
  2. Step 2: Check XCTAssertEqual condition

    XCTAssertEqual compares result (8) with 8, which are equal, so the test passes.
  3. Final Answer:

    The test will pass because 5 + 3 equals 8. -> Option A
  4. Quick Check:

    5 + 3 = 8, XCTAssertEqual passes = A [OK]
Hint: Calculate values, then check equality assertion [OK]
Common Mistakes:
  • Assuming test fails without checking actual values
  • Confusing syntax error with test failure
  • Thinking XCTAssertEqual compares types only
4. Identify the error in this test code:
let isActive = false
XCTAssertTrue(isActive)
medium
A. No error; the test will pass.
B. XCTAssertTrue cannot be used with boolean variables.
C. The test will fail because isActive is false but XCTAssertTrue expects true.
D. Syntax error due to missing parentheses.

Solution

  1. Step 1: Check the value of isActive

    isActive is set to false.
  2. Step 2: Understand XCTAssertTrue behavior

    XCTAssertTrue expects its argument to be true; since isActive is false, the test will fail.
  3. Final Answer:

    The test will fail because isActive is false but XCTAssertTrue expects true. -> Option C
  4. Quick Check:

    False value with XCTAssertTrue causes failure = D [OK]
Hint: XCTAssertTrue fails if condition is false [OK]
Common Mistakes:
  • Thinking test passes when condition is false
  • Confusing syntax error with test failure
  • Believing XCTAssertTrue only works with expressions
5. You want to test a function isEven(_ number: Int) -> Bool that returns true if a number is even. Which assertion correctly tests that isEven(4) returns true?
hard
A. XCTAssertEqual(isEven(4), true)
B. Both A and B are correct
C. XCTAssertTrue(isEven(4))
D. XCTAssertFalse(isEven(4))

Solution

  1. Step 1: Understand the function output

    isEven(4) returns true because 4 is even.
  2. Step 2: Check assertion options

    XCTAssertEqual(isEven(4), true) uses XCTAssertEqual to compare with true, which works.
    XCTAssertTrue(isEven(4)) uses XCTAssertTrue directly on the result, which also works.
  3. Step 3: Determine correct choice

    Both assertions correctly test that the function returns true.
  4. Final Answer:

    Both A and B are correct -> Option B
  5. Quick Check:

    Both XCTAssertEqual and XCTAssertTrue work for true checks = C [OK]
Hint: XCTAssertEqual and XCTAssertTrue both check true values [OK]
Common Mistakes:
  • Choosing only one assertion as correct
  • Using XCTAssertFalse incorrectly
  • Confusing equality with boolean checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard