Mocking data for UI testing in Xcode 7

Over the passage of time working on different projects i’ve seen a lot of different automation frameworks to handle UI testing. Mostly quite similar, they rely on the concept of having deterministic mock data that will power your views, they then drive your views through a user journey to assert an expected outcome. Most of these frameworks follow the Behaviour Driven Development (BDD) approach to writing tests through their adoption of Cucumber, I won’t go into the details of these here as its not really relevant.

Xcode UI testing however takes a different stance and on my current project, we’ve used it more as a “no, don’t unit test that, its a view controller, write a UI test” style tool, rather than a fully fledged automation framework. What this really means is, we’re asserting things like “the menu only contains 5 items that match our mocked data” or “when you hit login an activity indicator view is displayed”; whereas in UI Automation land we’d be testing things like “given i’m a logged out user, then I enter text into username and text into password, then I hit login, I should be presented the welcome screen”… Or similar. So effectively we’re testing small discreet units of UI within our application.

The two things we soon discovered we’d need for this are:

 The ability to inject dependencies where possible so we can load views without always necessarily progressing through a user journey to them.

The ability to test our views deterministically. This means we require MOCK data.

Why don’t we just go to the network, we can guarantee our API? Well, basically because you can’t. If you can run your test suite in a nuclear bunker, surrounded by a faraday cage with no outward or inward connectivity, then you’re onto a winner. The ability to be deterministic, to be certain that if your test fails its not because of some outward problem, but indeed due to a problem with your implementation, is vital for this specific type of testing. Other testing strategies such as end-to-end etc can accomodate this.

I’ve seen a few approaches to mocking data over the past decade. The common theme seems to be either one of:

Running a proxy that will returned canned responses to requests, configuring your network to use it as appropriate..

Littering all of your network code with #ifdef UI_AUTOMATION… and returning canned responses from an NSBundle.

In each individual test case, creating stubs and mocked responses for HTTP calls on the fly

All work, but it can be a little easier depending on your use case. SuperMock is inspired by both… a framework that you can drop into an existing code base, provide it with a simple .plist that maps network calls to canned responses in your NSBundle, functions as an NSURLConnection/NSURLSession proxy, but runs as part of your application test target, runs no server, requires no network and only leverages built in components of Foundation.

Example Scenario:

You have a menu in your application, which is dynamically driven from a web service, the contents of this menu can change at any time, but you’d like to assert that given some data, it populates and draws correctly on the screen.

Given the aforementioned problems… bring on the MOCK!

Example of the required Mocks.plist

Example of the required Mocks.json

By defining your original/real URL in

Mocks.plist

of SuperMock and running 1 line (ok maybe 2) of code in your AppDelegate, you are able to guarantee the value of your menu items in your application. Making your UI Test case very simple to write and very reliable to run.

How to implement SuperMock in your App.

How to implement SuperMock in your App.

Now when your HTTP client fires off a request for

http://mike.kz/api/menu.json

it will instead get returned

file://....menu_MOCK.json

from the NSBundle you provided earlier. In your UI Tests (whatever framework you use), you can now assert based upon these MOCK values. Whats advantageous is that there is no more work to be done and no extra code in your test cases to stub out responses.
This is a short whirlwind tour of my latest little pet project, but expect to see feature like dynamic override of mocks (for use when you want the same URL to return different responses upon each request) and some more real world scenarios.

I’ll be using this framework a lot myself in my personal projects over the coming months, so I may tweak / change / improve / try not to break it, in the process. Right now its functional but not complete and needs a new README.md ?

 

https://github.com/michaelarmstrong/SuperMock / @ArmstrongAtWork

 

Brisk a simple lightweight Networking Framework written in Swift


This is gonna be a short and sweet blog post… I’ve been working on a little pet project to practise and learn some Swift, so I thought, what do I use the most in projects these days… its networking. I’ve published on github.com my first attempt Brisk, its no where near complete and only has basic functionality right now, but if it becomes interesting i’ll pick up the effort and add more, until then, its a great educational exercise, you can check it out here and be sure to contribute back 🙂 it should work on both OS X and iOS and is MIT Licensed.

 

Pasti.ng An Engineers Pastebin re-invented.

As part of my ongoing pledge to be less selfish, i’ve decided to spend a few hours making a nice little Paste Bin website.

It uses Couchbase (a NoSQL storage database server) and Ruby on Rails along with a touch of bootstrap. I’m gonna be adding thing such as “re paste” in-line editing and self-destructing pastes as I get time. But until then, its nice little pet project for me to ponder on.

Once you paste it maintains the formatting of your code and even supports Unicode.

It should be lightening fast (even though its ruby) and stable + it has a cool domain name… what else do you need. Paste your heart out.

http://pasti.ng

Enjoy!

 

Objective-C iOS Code Style Guidelines (Part #1)

I move around on different projects a lot and work with different developers every few months. One thing I’ve noticed is people picking up bad habits from blogs and Stack Overflow posts that get very popular despite their quality (this is not a blanket statement of course)… On the other hand however, I’m able to share experiences with other developers and pickup some good tips in the process. One bone picking point is always what convention to use, when, where and most importantly… Why!

Something i’ve been asked for a few times recently and something I continually tweak as my own preferences and style changes is my Objective-C iOS Code Style Guideline doc. I thought i’d share it here for everyone to use, love and consume. All I ask is that you don’t steal it as your own and that you perform any discussion on twitter using #mike.kz or message me @italoarmstrong 🙂

These guidelines of course are my own personal preferences, which have developed over many years and will continue to change, even as writing this point, I changed my mind on a few points in the below guide. So take inspiration from it, rather than thinking “this is correct way of doing it” as its not. Thats usually the point in a “guideline”.

… 

 

Reading Linux extfs (ext2/3/4) in Mac OS X

I have a collection of both Mac OS X and Linux servers dotted around the place… Sometimes I need to read ext4 formatted drives on my Mac’s… this always proves an annoying problem where I have to mount them inside a Virtual Machine and copy things across…. Pain and Slow!

I searched the interweb for a solution to this… and most  guides are about 3 years out of date, so here’s one from 2013 that works on Lion / Mountain Lion and even Mavericks DP1/2.

Download and Install : OSXFUSE-2.5.4.dmg (this took off where MacFuse left off and even has compatibility for old MacFuse plugins).

Download and Install : fuse-ext2-0.0.7.dmg

Plug in your ext fs formatted drive… Voila! I wouldn’t rely on this in a production environment mind you… but its decent if you just need to grab some files from a USB drive quickly for example.

 

How to turn off Auto Layout Automagically on Xcode 4 post iOS 6

I’m sure i’m not the only one who is annoyed at the fact that, even though my project Deployment Target is set as iOS 5.0… Xcode finds it neccesary to enable Auto Layout on all newly created XIB’s regardless… causing Runtime exceptions that can be pesky and hard to find if not testing looking. So I started thinking how to disable this once and for all…

After monitoring the state of my disk, before and after ticking the infallable box in IB, I realised that Xcode is actually using some xml files to enable/disable this feature, however, there are a few locations and its a little pesky… i’d document it all here, but I don’t have it to hand and can’t remember… so maybe later… but until then, here is a little tool I made that performs a simple regexp on some of your Xcode installation files to make sure Auto Layout is disabled 🙂 NOTE: You’ll have to re-run this tool if you update/re-install Xcode.

Download: AutoLayoutAnnoysMe.zip

 

 

iPad Ripper 0.1 Convert Anything to H264 for FREE

Well, i know i’ve been slacking lately on this blog, but heres a lil treat, I recently got an iPad and iPhone and bla bla, and soon got very tired of converting all of my mkv and avi content over to the iPad, so I made a little tool to simplify the process, it supports as input formats pretty much everything that VLC does, including: xvid / flv / mkv / mpg etc, and outputs all files into .mp4 H264 🙂

Its free to use and released under a beer-ware license… i.e. if you like it, donate me some £ for some beer 🙂 there is a giant button in the app, you can’t miss it =). Here it is anyway: iPad Ripper

Requires: Intel Based Apple Mac running Mac OS X 10.6 or later.

Convert anything to iPad for Free

Convert anything to iPad for Free