What is the purpose of XCTAssertEqual in Swift testing?
XCTAssertEqual checks if two values are equal. If they are not, the test fails. It helps verify that the code produces the expected result.
Click to reveal answer
beginner
How does XCTAssertTrue work in Swift tests?
XCTAssertTrue checks if a condition is true. If the condition is false, the test fails. It is used to confirm that a boolean expression is true.
Click to reveal answer
beginner
Example: What will XCTAssertEqual(3 + 2, 5) do in a test?
It will pass because 3 + 2 equals 5, so the two values are equal.
Click to reveal answer
beginner
Example: What happens if XCTAssertTrue(2 > 3) is used in a test?
The test will fail because 2 > 3 is false, but XCTAssertTrue expects a true condition.
Click to reveal answer
beginner
Why are assertions like XCTAssertEqual and XCTAssertTrue important in testing?
They help catch mistakes early by checking if the code behaves as expected. This makes the code more reliable and easier to fix if something goes wrong.
Click to reveal answer
What does XCTAssertEqual(a, b) check in a test?
AIf a and b are both true
BIf a and b are equal
CIf a is less than b
DIf a is greater than b
✗ Incorrect
XCTAssertEqual checks if the two values a and b are equal.
What happens if XCTAssertTrue(condition) receives a false condition?
AThe test crashes
BThe test passes
CThe test is skipped
DThe test fails
✗ Incorrect
XCTAssertTrue expects a true condition; if false, the test fails.
Which assertion would you use to check if two strings are the same?
AXCTAssertEqual
BXCTAssertNil
CXCTAssertTrue
DXCTAssertFalse
✗ Incorrect
XCTAssertEqual is used to check if two values, including strings, are equal.
If you want to check that a boolean variable is true, which assertion is best?
AXCTAssertTrue(variable)
BXCTAssertEqual(variable, true)
CXCTAssertFalse(variable)
DXCTAssertNil(variable)
✗ Incorrect
XCTAssertTrue directly checks if a boolean variable is true.
Why use assertions in unit tests?
ATo slow down the program
BTo make code harder to read
CTo check if code works as expected
DTo skip testing
✗ Incorrect
Assertions verify that code behaves correctly, helping catch errors early.
Explain how XCTAssertEqual and XCTAssertTrue help in testing Swift code.
Think about what each assertion checks and why failing tests are useful.
You got /4 concepts.
Describe a simple example where you would use XCTAssertTrue in a test.
Imagine checking if something is true in your app.
You got /4 concepts.
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
Step 1: Understand XCTAssertEqual purpose
XCTAssertEqual is used to compare two values for equality in tests.
Step 2: Match the function behavior
It returns success if both values are equal, otherwise it fails the test.
Final Answer:
It checks if a and b are equal. -> Option A
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
Step 1: Identify the correct assertion for boolean true
XCTAssertTrue is designed to check if a condition is true.
Step 2: Compare options
XCTAssertTrue(isReady) uses XCTAssertTrue directly on isReady, which is the simplest and correct syntax.
Final Answer:
XCTAssertTrue(isReady) -> Option D
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
Step 1: Calculate the expression
5 + 3 equals 8, so result is 8.
Step 2: Check XCTAssertEqual condition
XCTAssertEqual compares result (8) with 8, which are equal, so the test passes.
Final Answer:
The test will pass because 5 + 3 equals 8. -> Option A
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
Step 1: Check the value of isActive
isActive is set to false.
Step 2: Understand XCTAssertTrue behavior
XCTAssertTrue expects its argument to be true; since isActive is false, the test will fail.
Final Answer:
The test will fail because isActive is false but XCTAssertTrue expects true. -> Option C
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
Step 1: Understand the function output
isEven(4) returns true because 4 is even.
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.
Step 3: Determine correct choice
Both assertions correctly test that the function returns true.
Final Answer:
Both A and B are correct -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.