Setup your iOS Testing Env
Testing under iOS platform has improved a lot since early days, back then it was challenging to run unit tests outside of Xcode, generate code coverage reports and so on. Apple did eventually increase test support throughout its environment making our lives easier. Nonetheless, nowadays things still far from perfect, however with the help of some friends I mean tools this battle can be over sooner than you think.
Today I’m going to write about some great tools that can help you in the journey of testing your code inside apple’s platform. I’ve been using these tools in several different projects, and they helped me to achieve incredible results. This post goal is to help you to incorporate them into your day to day code and of course, harvest the benefits of doing so.
There is nothing wrong with XCTest, but after a couple of days working with it, you begin to notice a lot of repetition and wonder why it is so verbose.
XCTest has a few issues in my opinion:
- The tests need to be declared as functions starting with the word test.
- The is no support for nested tests.
- It is not possible to create different contexts.
- There is not support for contextualized setups and teardowns.
- Big test files look very messy.
Quick is a framework built on top of XCTest that addresses the problems mentioned above. It has a cleaner syntax, support for arranging your tests in different contexts, contextualized setups and teardowns, among other things that make the process of writing tests a breeze.
Nimble is a matcher framework created by the Quick family. What is a matcher framework you might ask? It’s something that makes easier for you to write assertions. In other words, instead of writing something like that:
XCTAssertEqual(1 + 1, 2, "expected one plus one to equal two")
You would write this:
expect(1 + 1).to(equal(2))
Nimble-Snapshots is a project with one goal, making trivial to work with facebook’s ios snapshot test cases. Wait a minute, what is this facebook project?
ios-snapshot-test-case - Snapshot view unit tests for iOSgithub.com
What it does?
A “snapshot test case” takes a configured
CALayerand uses the
renderInContext:method to get an image snapshot of its contents. It compares this snapshot to a "reference image" stored in your source code repository and fails the test if the two images don't match.
At Facebook we write a lot of UI code. As you might imagine, each type of feed story is rendered using a subclass of
UIView. There are a lot of edge cases that we want to handle correctly:
What if there is more text than can fit in the space available?
What if an image doesn’t match the size of an image view?
What should the highlighted state look like?
It’s straightforward to test logic code, but less obvious how you should test views. You can do a lot of rectangle asserts, but these are hard to understand or visualize. Looking at an image diff shows you exactly what changed and how it will look to users.
FBSnapshotTestCaseto make snapshot tests easy.
FBSnapshotTestCase's Github page.
In other words ..
It records screenshots of your app at a given time, and then compares them with new screenshots of your app at the present moment; this can help you find out changes in your code that somehow have broken your app’s UI.
Fastlane is a set of tools that handle and automate several different needs of iOS developers. No iOS pipeline would be complete without the mention of Fastlane. For our purposes, we are interested in its *Slather and Scan action, which can help us to run our tests using the cmd line, something fundamental and a prerequisite for implementing continuous integration.
*Slather will be mentioned below.
*Fastlane is much more than just one tool as you can see below.
fastlane is the tool to release your iOS and Android app 🚀 It handles all tedious tasks, like generating screenshots…fastlane.tools
Slather is a tool that reads the Xcode test data and generates code coverage reports from it. This code coverage reports can be used to help the developers to spot areas of the code that are not currently tested. The report can help you to answer two questions:
- What should I test next?
- Is this class tested enough?
* Testing shows the presence, not the absence, of bugs. — Edsger W. Dijkstra
slather - Generate test coverage reports for Xcode projects & hook it into CI.github.com
KIF & KIF-Quick
KIF, which stands for Keep It Functional, is an iOS integration test framework. It allows for easy automation of iOS apps by leveraging the accessibility attributes that the OS makes available for those with visual disabilities.
KIF builds and performs the tests using a standard XCTest testing target. Testing is conducted synchronously in the main thread (running the run loop to force the passage of time) allowing for more complex logic and composition. This also allows KIF to take advantage of the Xcode Test Navigator, command line build tools, and Bot test reports.
KIF-Quick is a bridge between the Keep It Functional and Quick frameworks. It allows you to perform KIF actions using Quick syntax.
KIF Takeway ..
KIF allows us to write more functional tests, simulating the behavior of the user inside our app. In other others, it enables us to click on buttons, scroll, swipe, etc. In that sense it is like Apple UI Test case, the big difference is that we can still mock or modify the code from the test like we would in a unit test. In addition to that, we can load any screen without the need to navigate all the way to it, which means you can load the Confirmation screen in a checkout flow(Providing all the inputs for it as mock objects) and check the behavior without the need of running your whole checkout flow.
The goal of this post was to present several different tools that can enhance your iOS testing pipeline. Testing is not something inherently tricky by itself. The problem is that it requires discipline, a shift in your mindset and lots of practice. The tools presented in the post will help you to become a better developer, once you incorporate this set of practices and develop the test mindset, your code will become much more reliable, decoupled, flexible, among other things. It’s, of course, mandatory that you play with the tools to harvest the benefits.
"An ounce of practice is generally worth more than a ton of theory.”
― Ernst F. Schumacher