iOS UI Component Testing

We usually test UI by hand in iOS development. It’s a very inefficient way. In this doc we suggest you write the UI component test in iOS development.

Test by hand: 

  • Use it only in the integration phase.
  • Need play app as a normal user.
  • Low productivity on testing only one UI component.

UI component test: 

  • Use it in debug & development phase.
  • No need to play app as a normal user.
  • High productivity.

What is UI component test: 

Say we are developing an iOS game, the game has a RatingView for show user’s score after a stage is completed.

For testing this RatingView you need to complete the stage( as a normal user ) and check if the score view is correct. 

But in UI component test, we can debug RatingView directly, preventing a lot of repeat operations.

1 Create TestViewController 

We can create many tests in TestViewController. For example:

Then we can call testRatingView in viewDidLoad()

That means once TestViewController starts it will setup the necessary data and just present RatingView for testing.

In a real project, TestViewController can have many UI tests, and TestViewController::viewDidLoad can like this:

Yes, we can comment on all other tests and only leave the 1 test.

2 Switch between TestViewController and App Main ViewController

We need to start TestViewController on app startup, we can set a constant flag isTestMode, and set it = true while testing UI components.

We can create a root view controller and set it as an entry view controller in storyboard. 

Then if isTestMode is set, we can just show TestViewController, otherwise we can start the real app main view controller. 

Increase Efficiency

We can do many things in UI component testing to increase efficiency.

  • Pre-fill form fields.
  • Set up different data models to check different UI effects.
  • Create some test buttons to check UI interactive.

For example, we usually add test buttons for that case, say there is a list view, in general case, and for testing insert items we must enter many fields and create a new item. 

Instead we can create a test button and only show them in test mode. Like this:

When tap Test Insert Item, we call testInsertItem function and that function will create a new item and cause the insert logic to run.

All that tricks need to design the UI component clearly, reduce the dependencies. 

One thought on “iOS UI Component Testing

Leave a Reply

Your email address will not be published. Required fields are marked *