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

 

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!

 

Manage your KVM Hypervisor Remotely on your iPhone / iPad

Recently I began experimenting with KVM virtualisation in the Linux Kernel. Its a great technology that if your CPU supports VT-x / AMDV offers almost (really, almost) bare metal level performance inside Virtual Machines. It works on most Linux flavours and has a couple of handy management tools such as virsh and virt-manager. However, one thing I thought was always lacking and annoying me was of course, the ability to manage my Hypervisor from my iPhone / iPad when on the move! Time for an experiment I thought; then out came “KVM Remote”

KVM Remote on the iPad and 3 Different Remote Hypervisors

Its universal so works on both the iPhone and iPad and is extremely bleeding edge right now, but works! and is incidentally the first App i’ve made that doesn’t have selfish fiscal intentions, so theres another great reason to download it from the AppStore now!

P.S. i’ll be updating it regularly adding more features as requests come in.

 

Amazon Support Store

Hi everyone! I keep getting lots of emails from people asking where they can buy xyz to complete the tutorials and try out some of the things listed on CaptainGeek, well after I kept emailing people the same links i had a thought, why not setup an amazon affiliate store. Basically, i’ve setup a small amazon site with a small selection of products (only those used for the tutorials on this site + related ones), purchases and payments are handled by amazon, however a small percentage of the sale goes to helping fund the server this website is hosted on AT NO EXTRA COST TO YOU πŸ™‚ so its a win win situation, please use the links whenever you can.

Our Amazon Store

 

VBareMetal The New CaptainGeek Bare-Metal Hypervisor Installer

Introducing VBareMetal!

I’ve been very busy lately, here is one of my latest incarnations, a Virtualisation Linux Distribution, which you can use as a live-cd or a bare-metal installer, it is debian based with virtualbox. Everything configured out of the box. You can run Linux/BSD/Solaris/Windows/Other virtual machines on this distribution, even if your system does not support virtualisation natively! =) Please note, this release is super early, it works, but very bare. In the coming weeks i’ll make it a pretty fully functional system. … 

 

Problems, Updates, Projects & More!

So you’ve probably wondered why i went from posting 3 times a day to not posting at all. I’ve been pretty busy recently with exams, jobs, personal-issues and so on. Few bits of news regarding the project. I’ve moved house, therefor am going to migrate everything thats using UKERNA over to HE.NET purely as they seem to be much more stable, and I can use 6over4 πŸ™‚ Also i am working on something you are all gonna like, just gimme time and i’ll have it sorted πŸ™‚ need to buy a more powerful server first heh πŸ™‚

For now peace! and stay tuned for whats coming next πŸ™‚

ETA: 2 weeks

 

Server Re-Location

Hey everyone, i moved everything from where it was about 5 miles down the road, slight change in ipv4 addressing, (1 digit) but ipv6 addressing stays the same, let me know if any services are not working correctly πŸ™‚ peace x

 

Fixed: Connectivity Issues

If any of you have been recently experiencing connectivity issues with the site or problems with zones generated using the zone builder, these bugs have been now rectified.

The zone builder was not specifying a $TTL correctly, Also as all of my zones are generated here, my bind9 stopped working properly. All fixed now, sorry for any inconvenience caused πŸ™‚

 

UPDATED: IPV6 Reverse Zone Builder

Hey guys, another update tonight! Thanks to a LOT of help from rewt in #ipv6 on efnet, i’ve managed to eleminate most bugs from the zone builder, if you had problems before, try it again and it should be fine. Im also thinking about creating a hosted dns solution, leave your thoughts in the comments πŸ™‚ until then, check out the new and improved IPV6 Reverse Zone Builder

 

IPV6 Reverse Zone Builder for BIND

Two services launched in 1 Day! Here I bring you something I did on impulse after many people seem to be struggling to setup IPV6 reverse zones, i’ve put together a simple zone building script which will generate all the information you need for BIND automatically! At the moment it only supports /64’s and is a bit error prone, but i think it will suffice. Soon I will perfect it and offer zone hosting services here for people. Until then you can check it out here and setup your reverse zone!