Unit and UI testing in iOS

Ronak Patel
3 min readSep 21, 2020

--

Introduction:

XCTest is Xcode’s testing framework.

It is a base class used to create and run unit tests, performance tests, and UI tests for your Xcode project.

It consists 2 main important methods,

1)setUp():- setUp is called before each of the test case is executed.

2)tearDown():- tearDown() is called after the execution of the test case for cleaning up the data ,so that it won’t create any error.

Types of XCTest:

1)Unit Testing

It is used to test the small unit of code.

It includes both standard Unit testing and Integration testing.

2)UI Testing

UI Test will be able to make sure that everything in user interface is behaving correctly.

It always perform on the top of your app’s UI.

It is also called as a black box testing because it won’t rely on any knowledge of the function or class of your app.

3)Performance Testing

Performance test runs multiple time over a given test to look at the average time , memory usage etc.

Basic Unit Test Assertions:

  • There are total 6 types of Assertions.

Boolean Assertions:- Test a condition that generates a true or false result

Nil and Non Nil Assertions:- Check whether a test condition is nil or non-nil.

Comparable Values Assertions:- Compare two values to determine whether one is larger or smaller than the other.

Equality and Non Equality Assertions:- Check whether two values are equal or unequal.

Error Assertions:- Check whether a function call throws (or does not throw) an error.

Failing Unconditionally:- Generate a failure immediately and unconditionally.

3Key Classes of UITesting:

1.XCUIApplication

A proxy for an application that can be launched and terminated.

  1. func launch() :- Launches the application
  2. func activate():- Activates the application
  3. func terminate():- Terminates the application

2.XCUIElement

A UI element in an application.

XCUIElement provides keyboard- and mouse-like interactions such as typing, hovering, clicking, and scrolling.

XCUIElement provides gestural interactions such as tapping, pressing, swiping, pinching, and rotating.

3.XCUIElementQuery

A query for locating UI elements i.e count , elementAtIndex() etc.

While doing UITest, we want to see the flow of app then we put the debugger and checks the view hierarchy , labels, textfields ,buttons, images in the testExample().

UITest provides a tool UIRecording , by enable the uirecording at first time we have to run the testfunc() so that it will record all the thing that we have done on the UI i:e tapping , swipe gestures , labels , buttons etc.

From second time , we only have to run the testfunc() , it will automatically check the actions in the function which is already recorded.

On the basis of this recording UITest will performed. If this recording runs perfectly then the test is passed else test is failed.

Performance Testing

Measure block is used to check the average running time of both unit and ui test.

It will run the test multiple times i:e 10 times to calculate the average.

Xcode also provides a tool to check the coverage of code written in the view controller in your app.

  1. For ViewController

2. For TestVC

If you have any doubt then you can visit, https://docs.google.com/presentation/d/11xDocxBzmryqOrTLj8x8T3kQnlk6DsofIljizjYP6S0/edit#slide=id.g6c40f4cfb5_0_5

Thanks.

--

--