0
0
Swiftprogramming~5 mins

Assertions (XCTAssertEqual, XCTAssertTrue) in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
What happens if XCTAssertTrue(condition) receives a false condition?
AThe test crashes
BThe test passes
CThe test is skipped
DThe test fails
Which assertion would you use to check if two strings are the same?
AXCTAssertEqual
BXCTAssertNil
CXCTAssertTrue
DXCTAssertFalse
If you want to check that a boolean variable is true, which assertion is best?
AXCTAssertTrue(variable)
BXCTAssertEqual(variable, true)
CXCTAssertFalse(variable)
DXCTAssertNil(variable)
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
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.