Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a simple unit test method in Swift.
iOS Swift
func testExample() {
let result = 2 + 2
XCTAssertEqual(result, [1])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong expected value like 3 or 5.
Confusing the actual result with the expected value.
✗ Incorrect
The test checks if 2 + 2 equals 4, so the expected value is 4.
2fill in blank
mediumComplete the code to launch the app and check if a button exists in UI testing.
iOS Swift
let app = XCUIApplication() app.launch() let button = app.buttons["Submit"] XCTAssertTrue(button.[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isHidden' which checks visibility, not presence.
Using 'isEnabled' which checks if the button can be tapped.
✗ Incorrect
To verify the button is present, we check if it exists.
3fill in blank
hardFix the error in the test assertion to correctly check if a label's text equals "Hello".
iOS Swift
let label = app.staticTexts["greetingLabel"] XCTAssertEqual(label.[1], "Hello")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' which is not a valid property in XCUIElement.
Using 'label' which is a property but not the text content.
✗ Incorrect
In UI tests, the label's text is accessed via the 'value' property.
4fill in blank
hardFill both blanks to create a test that waits for a button to appear before tapping it.
iOS Swift
let app = XCUIApplication() let button = app.buttons["Continue"] let exists = NSPredicate(format: "[1] == true") expectation(for: exists, evaluatedWith: button, handler: nil) waitForExpectations(timeout: [2], handler: nil) button.tap()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isEnabled' instead of 'exists' in the predicate.
Setting the timeout too low or too high.
✗ Incorrect
We wait for the button's 'exists' property to be true, with a timeout of 10 seconds.
5fill in blank
hardFill all three blanks to write a unit test that checks if a function returns true for valid input.
iOS Swift
func testIsValid() {
let input = "test@example.com"
let result = Validator.[1](input: input)
XCTAssert[2](result)
XCTAssertFalse(Validator.[3](input: ""))
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using XCTAssertFalse for the valid input check.
Mixing up function names.
✗ Incorrect
The test calls 'isValidEmail' to check the input, asserts true for valid input, and false for empty input.