XCTest is a framework provided by Apple to write and run tests for Swift code. It helps check if your code works as expected by running test cases automatically.
Click to reveal answer
beginner
What is a test case in XCTest?
A test case is a class that inherits from XCTestCase. It contains one or more test methods that check specific parts of your code to make sure they behave correctly.
Click to reveal answer
beginner
How do you write a test method in XCTest?
A test method is a function inside a test case class. It must start with the word 'test' and contain assertions to verify code behavior. For example:
func testAddition() { XCTAssertEqual(2 + 2, 4) }
Click to reveal answer
beginner
What is the purpose of XCTAssert functions?
XCTAssert functions check if a condition is true during a test. If the condition fails, the test fails. Examples include XCTAssertEqual, XCTAssertTrue, XCTAssertNil, etc.
Click to reveal answer
intermediate
What are setUp() and tearDown() methods used for in XCTest?
setUp() runs before each test method to prepare the environment. tearDown() runs after each test method to clean up. They help keep tests independent and organized.
Click to reveal answer
Which class should your test case inherit from in XCTest?
AXCTest
BTestCase
CXCTestCase
DTestSuite
✗ Incorrect
In XCTest, all test cases must inherit from XCTestCase to gain testing capabilities.
What prefix must test method names start with in XCTest?
Atest
Bcheck
Cverify
Dassert
✗ Incorrect
Test methods must start with 'test' so XCTest can recognize and run them automatically.
What does XCTAssertEqual(a, b) do in a test?
AChecks if a is equal to b
BChecks if a is less than b
CChecks if a is not equal to b
DChecks if a is greater than b
✗ Incorrect
XCTAssertEqual checks if two values are equal. If not, the test fails.
When is tearDown() called in the XCTest lifecycle?
ABefore all tests start
BAfter all tests finish
CBefore each test method
DAfter each test method
✗ Incorrect
tearDown() runs after each test method to clean up resources.
Which of these is NOT a valid XCTAssert function?
AXCTAssertTrue
BXCTAssertComplete
CXCTAssertFail
DXCTAssertNil
✗ Incorrect
XCTAssertComplete does not exist in XCTest. The others are valid assertion functions.
Explain how to create a basic test case using XCTest and what components it should include.
Think about the class, methods, and assertions needed.
You got /4 concepts.
Describe the role of setUp() and tearDown() in managing test environment in XCTest.
Consider when these methods run and why they help tests.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the XCTest framework in Swift?
easy
A. To manage app navigation and routing
B. To create user interfaces for iOS apps
C. To write tests that check if your Swift code works correctly
D. To handle network requests and responses
Solution
Step 1: Understand XCTest framework role
XCTest is designed to help developers write tests for their Swift code to ensure it behaves as expected.
Step 2: Compare options with XCTest purpose
Only To write tests that check if your Swift code works correctly describes writing tests to check code correctness, which matches XCTest's main use.
Final Answer:
To write tests that check if your Swift code works correctly -> Option C
Quick Check:
XCTest purpose = write tests [OK]
Hint: XCTest is for testing code correctness only [OK]
Common Mistakes:
Confusing XCTest with UI design tools
Thinking XCTest manages app navigation
Assuming XCTest handles networking
2. Which of the following is the correct way to declare a test method inside an XCTestCase subclass?
easy
A. func testExample() { }
B. func exampleTest() { }
C. func TestExample() { }
D. func example() { }
Solution
Step 1: Recall XCTest method naming rules
Test methods must start with the word 'test' and use camelCase, starting with lowercase.
Step 2: Check each option's method name
func testExample() { } starts with 'test' and uses correct casing. Others either start with the wrong word or uppercase first letter.
Final Answer:
func testExample() { } -> Option A
Quick Check:
Test method name starts with 'test' [OK]
Hint: Test methods always start with 'test' lowercase [OK]
Common Mistakes:
Starting method name with uppercase 'Test'
Placing 'test' at the end of method name
Not starting method name with 'test'
3. What will be the output when running this test method?
class MyTests: XCTestCase {
func testSum() {
let result = 2 + 3
XCTAssertEqual(result, 5)
}
}
medium
A. Test fails with an assertion error
B. Test passes with no errors
C. Compilation error due to missing return
D. Runtime error due to wrong XCTAssert usage
Solution
Step 1: Analyze the test method logic
The method calculates 2 + 3 and stores in result, which equals 5.
Step 2: Check the assertion
XCTAssertEqual compares result (5) with expected value 5, which matches, so assertion passes.
Final Answer:
Test passes with no errors -> Option B
Quick Check:
2 + 3 = 5, XCTAssertEqual passes [OK]
Hint: XCTAssertEqual passes if values are equal [OK]
Common Mistakes:
Thinking test fails when values match
Confusing compilation errors with test failures
Assuming XCTAssertEqual causes runtime errors
4. Identify the error in this test code:
class SampleTests: XCTestCase {
func testMultiply() {
let product = 4 * 5
XCTAssertEqual(product, 20
}
}
medium
A. Missing closing parenthesis in XCTAssertEqual
B. Test method name does not start with 'test'
C. Incorrect multiplication operator used
D. XCTestCase subclass missing override keyword
Solution
Step 1: Check syntax of XCTAssertEqual call
The XCTAssertEqual call is missing a closing parenthesis at the end.
Step 2: Verify other parts
Method name starts with 'test', multiplication operator is correct, and override keyword is not required here.
Final Answer:
Missing closing parenthesis in XCTAssertEqual -> Option A
Quick Check:
Syntax error due to missing ')' [OK]
Hint: Count parentheses carefully in assertions [OK]
5. You want to test a function that returns the maximum number from an array. Which XCTest assertion is best to verify the result?
hard
A. XCTAssertNil to check if the max value is nil
B. XCTAssertThrowsError to check if max throws an error
C. XCTAssertTrue to check if max value is greater than zero
D. XCTAssertEqual to compare expected and actual max values
Solution
Step 1: Understand the function's expected output
The function returns the maximum number from an array, which is a specific value.
Step 2: Choose assertion to compare values
XCTAssertEqual compares two values for equality, perfect to check if returned max matches expected max.
Step 3: Evaluate other options
XCTAssertNil checks for nil, not suitable here. XCTAssertTrue checks a condition but not exact value. XCTAssertThrowsError checks for errors, irrelevant here.
Final Answer:
XCTAssertEqual to compare expected and actual max values -> Option D
Quick Check:
Compare expected and actual values with XCTAssertEqual [OK]
Hint: Use XCTAssertEqual to check exact values [OK]
Common Mistakes:
Using XCTAssertNil when value is never nil
Using XCTAssertTrue without exact value check
Expecting errors when function doesn't throw
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.