Gemstones — Test Cases iOS

iOS Test Cases

Why to write Test Cases?

What to Test?

What are the Test Cases?

A Unit Test Case tests a specific function under specific context.

A UI Test Case tests a User Interface. It is also called Integration Testing.

What to test in Unit Test Cases?

What to test in UI Test Cases

When to write the Test Cases?

How to add Test Cases?

Xcode supports Unit and UI testing.

iOS — Ways to add Test Cases using xCode

Steps to write Test Cases?

1. Fail -> 2. Pass -> 3. Refactor

1. Given -> 2. When -> 3. Then

import XCTest
class UnitTestingTests: XCTestCase {
var sessionUnderTest: URLSession!

override func setUp() {
sessionUnderTest = URLSession(configuration:URLSessionConfiguration.default)

func test_APIComplition() {
//given let url = URL(string: "") let promise = expectation(description: "Complition handler invoked") var statusCode: Int?
var responseError: Error?
//when let dataTask = sessionUnderTest.dataTask(with: url!) { (data, response, error) in statusCode = (response as? HTTPURLResponse)?.statusCode
responseError = error
waitForExpectations(timeout: 5, handler: nil)

XCTAssertEqual(statusCode, 200)
import XCTestclass BasicFlow: XCTestCase {func testHelloWorld() {//Given
var strHelloWorld: String?
strHelloWorld = "Hello world"
//Then//this test will fail
//XCTAssertEqual(strHelloWorld, "Hello word")
//this test will pass
XCTAssertEqual(strHelloWorld, "Hello world")

import UIKitclass MainViewController: UIViewController { override func viewDidAppear(_ animated: Bool) {
let tempInt = square(value: 2)
print("square --> \(tempInt)")

func square(value: Int) -> Int {
return value * value
import XCTest //import your project here
@testable import UnitTesting
class BasicFlow: XCTestCase { func testSquareInt() {

let value:Int = 2
let squareVal = MainViewController().square(value: value)
XCTAssertEqual(squareVal, 4)

You have to write code in UITestCase class.

import XCTestclass UnitTestingUITests: XCTestCase { override func setUp() {// Put setup code here. This method is called before the invocation of each test method in the class.// In UI tests it is usually best to stop immediately when a failure occurs.  continueAfterFailure = false// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.  XCUIApplication().launch()
func test_LoginSuccess() { let validUsername = "abc"
let validPassword = "abc"
let app = XCUIApplication()
let txtUsername = app.textFields["username"]
//this checks textfield is exists or not XCTAssertTrue(txtUsername.exists)
let txtPassword = app.textFields["password"]
//select Simultor --> Hardware --> Keyboard --> Keep On "Toggle keyboard on" app.buttons["Login"].tap()//Invalid Login Case
app.alerts["Invalid credential"].buttons["Okay"].tap()
//Valid Login Case
//app.alerts["Logged In"].buttons["Okay"].tap()
Automation — UI Test Case for Login Scenario



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store