Unit Testing — KIF and FBSnapshot

It’s been a while since I’ve talked about unit testing, the importance of writing them in our apps and testing framework like Quick and Nimble. Test help us keeping features working as expected and avoiding that a new feature breaks an old one. I’m here today to talk about a new framework: KIF.

This post has thoughts about KIF, advantages, cons, when to use it and an example.

KIF

Is a unit testing framework that helps developers to create UI tests (yes, UI tests) and tests your app flows e.g your entire login process like entering valid credentials if your keyboard dismisses when the user touches the background, swipe tableView cells, what happens when the alert of invalid username pops and more. It is written in objc and uses XCTestCase but there’s a KIF-Quick if you would like to use it with Quick.

It brings functions to help perform events like tap(), doubleTap() and swipeLeft(). Uses accessibility to identify objects to interact with e.g. you can retrieve an object using its accessibilityIdentifier or accessibilityLabel and then perform an action using tap()

viewTester().usingIdentifier("btn_login").tap()

You should be asking yourself — Why should I use KIF if Apple provides a nice UI test framework — The answer is simple: mocks! KIF let you inject mocked objects to create a powerful local test flow of your app in other words you are free to mock services response and test your entire application flow with no need of internet connection, API response or even data mass.

Wow! That’s great. So why should I write unit tests if KIF can handle that and more? That’s a good question! Indeed, KIF can handle unit tests and more but it needs to run on the simulator and it takes time. So if you decide to only write tests using KIF they will take a lot more time to run than singles unit tests.

How to use it

KIF has an easy install processing though cocoapods, just add pod KIF to your podfile run pod install and it’s done. KIF is written in Obj-C so you need to create a bridging header and add KIF to it.

#import <KIF/KIF.h>

In this example, we are going to test a simple login flow with success and failure cases. Combining the power of KIF with Snapshot (if you don’t know what is Snapshot you should read this) we are going to validate all our UI.

Login flow

If user input right values (in this case "user" for username and "123123" for password) the home screen will be displayed else an alert error will be shown.

Don’t forget to test target and set the accessibility label in every UI object that will have interaction.

DefaultFlowSpec test

In this file we create a window, retrieve the initial viewController from our storyboard and add it as rootViewController of our window. After that, we need to test our success and failure flow.

Success flow

Our success flow is based on right inputs. First, the user will set user in username field, after that set 123123 in password field we are going to check UI at this moment, then click the login button and check if the homeScreen is displayed.

Success flow

Failure flow

In our Failure flow we are going to press login button, check if an error alert is shown, press ok on alert and check if alert is gone.

Failure flow

Conclusion

The best way is to combine both unit tests and KIF to get a fast and complete test. Keep testing your methods and functions using unit test it is faster. Use KIF to tests UI and connections between layers like model, view, and controller in an MVC architecture.

I wrote some post that shows how to implement some unit tests.

Ps: If you like this post, share it on twitter, recommend it on medium, or both =). This really helps me to reach more people. Thanks a lot.