iOS Test Setup
Yesterday I wrote an article with my Rails test setup, so it’s right about the time to do the same for iOS. I’ll be covering Specta, Expecta and KIF, plus OHHTTPStubs for… you know… stubs.
First thing first, as you can see from the previous post, I’m not really into Cucumber. I have more of a dev-first approach for specs. That’s the benefit of working on your own projects: you only have to explain things to yourself, who is usually a pretty cool guy and understands what you’re saying. Most of the times at least. That being said, I’m not a fan of the syntax that’s behind the nice english-like specs, i.e. lots of regex. Welp! That brings me to my choice for UI Testing: KIF. KIF (Keep It Functional) is a pretty neat iOS integration framework, it uses the accessibility attributes embedded in every UIKit object to streamline integration testing. This means that you write your tests in plain old Objective-C, which is a big selling point for me. Tests won’t read in fluid english-like sentences, like they do with Cucumber, but they still remain easy to write and easy to understand, even if your objc-fu is not strong.
I’ll be using Cocoapods, of course. First of all, you should already have a XCTest target ready. Make sure to add another one for the integration tests. I usually callit ‘UI Tests’. Here’s the Podfile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
pod install everything will be ready, so we can
As for Specta we should already be set, so we can just head to our Tests target and start adding specs. Note that I added also Expecta, to have a RSpec 2 like syntax.
1 2 3 4 5 6 7 8 9 10 11 12 13
⌘ + U and you should be green.
Now, Specta needs no further configurations, but KIF does.
KIF documentation suggest to make sure that the Bundle loader and Test Host are correctly configured, but I always found them already set by XCode to the proper values.
Since now XCode ships with XCTest, I had to switch back to
octest in the Wrapper Extension property, under Build Settings.
Everything should be ok, let’s write a test.
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10
⌘ + U and we are good.
brew install xctool
then run your test with it:
xctool -workspace Project.xcworkspace -scheme Project -sdk iphonesimulator test
That’s it. Until next time.