New way of UI Testing / Unit Testing on iOS

Abderrahim Benmakhlouf
3 min readMar 7, 2018

--

A new way to test your UI very quickly with unit tests by mocking the Business logic. Getting UI Tests code coverage with CI/CD.
This new way use a storyboard generator that mock the storyboard by replacing a mocked viewController that inherit the viewController that you want to test.

Why Are We Test (Unit And UI) Our Apps ?

For which reason are we testing our apps? To be Professional ! like Uncle Bob said, “it’s not immaginable that nowadays, a professional developper can write code without testing it”.

As an iOS developer, I was used to use the UI tests in xCode, but has a lot of problems:

  • It runs the simulator for every tests
  • It runs your app from the begining
  • You have to mock the network (with mockingjay or others third library), take too long and too much code to retrieve all the json responses(for the success, server and network errors).
  • You can’t test a specific viewcontroller (or you need to create a big system to init a specifc viewcontroller)
  • You can’t test every behavior on the view because the UI tests in xCode it’s very limited (like a label changes its color, fonts, how many pins on a map depending on a scenario and many other limitations), you need to use AccessibilityLabels and it’s not very user-friendly.
  • Running UI Tests takes a lot of times !
  • And more importantly : 0% code coverage gathered by UI Tests ! (if you want to have Continuous integration and continuous deployment with Jenkins for example)

On one of my project, I had to wait like 2 hours that all my UI Tests runs completly.
Many developers doesn’t use it for (one or all) these problems.

I heard one day on a conference from Robert Martin C. (alias Uncle Bob), as many of you, I’m sure, do know him, he said that a developer considering himself “profesionnel” only if he respects some rules like :
— “I will produce, with each release, a quick, sure, and repeatable proof that every element of the code works as it should”
— “I will fearlessly and relentlessly improve our work at every opportunity. I will never allow it to degrade.”

Here, Uncle Bob, insisted on tests that have to be run very quickly that each professionel developer can, at any time, have a proof that his work works as it should !
I asked one time another developer, how much time it tooks you to run all scenarios on a specific viewcontroller that you need to logged in, and you need to click on a button on the dashboard to be able to test it. He tolds me like «10–15mins» (because for each test the app need to run from the begining) and gathered 0% of code coverage. (Because the xcode/Apple UI test doen’t gather code coverage with slather when you run it outside of xcode like in command tools line).
I told him that I was able to run like 130 tests in 5–6 seconds! and I was able to get the code coverage for all my “UI-Unit” tests. this is what I’m going to demonstrate it for you.

Example Project

You can see an example project on my github.

My Python Script Generator

What is this script and what it really does?

My script generator allows to mock one or multiple viewcontroller(s) and also storyboard reference in a specific storyboard.

As you already know, a viewcontroller in a storyboard is “statically” set in the custom class section and also its Storyboard ID identity.
So, the python script will dynamically update the custom class section and the storyboard ID identity with the mocked one.

And so, on the UI-Unit Tests you will be able to create a new viewcontroller “Mocked” that will inherit the one that you want to test to be able to stub the Business logic part and by the way have all the outlets and methods to be tested.

--

--

Abderrahim Benmakhlouf

 iOS Tech Lead Developer — Professional developer — Extreme programming (XP) Never ship shit