Beginning Automated Testing With Xcode Part 1/2
Note from Ray: This is the tenth and final iOS 6 tutorial in the iOS 6 Feast! This tutorial comes from our new book iOS 6 By Tutorials. Charlie Fulton wrote this chapter – a friend of mine and one of the newest members of the Tutorial Team. Enjoy! This is a blog post by […] By Charlie Fulton.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Beginning Automated Testing With Xcode Part 1/2
30 mins
- Introducing GuildBrowser
- Gitting started
- Continuous integration
- Meet Jenkins
- Installing Jenkins
- Configure Jenkins plugins
- Setting up Jenkins email notification
- Creating a Jenkins job
- Automating xcodebuild from Jenkins
- Unit testing
- Application vs. logic test targets
- Running tests from command line notes – no touching!
- Adding a logic unit test target
- Where To Go From Here?
Running tests from command line notes – no touching!
Unfortunately, as of Xcode 4.5 we are still not “officially” allowed to run application unit tests via the xcodebuild command. This is very frustrating, because you obviously can run application unit tests from within Xcode. For this reason, you will create a special logic test target in the next section.
Some creative developers have made patches to the testing scripts inside of the Xcode.app contents. For this tutorial, you are going to stick to running your logic tests from Jenkins, and application tests from Xcode. If you’re curious about the patches, take a peek at these files:
/Applications/Xcode.app/Contents/Developer/Tools/RunUnitTests
/Applications/Xcode.app/Contents/Developer/Tools/RunPlatformUnitTests.include
This is the script that runs by default when you run unit tests via ⌘-U:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Tools/RunPlatformUnitTests
Here is the error message you’ll get, and the reason we can’t run application unit tests in the Simulator from the command line (remember that this works fine from within Xcode):
Warning ${LINENO} “Skipping tests; the iPhoneSimulator platform does not currently support application-hosted tests (TEST_HOST set).”
}
Some clever hacks have gotten application tests to work, but the scripts have changed from Xcode 4.4 to 4.5, so do so at your own risk.
Adding a logic unit test target
You are now going to create a new test target that can be called from your build script.
In Xcode, Go to File\New\Target…\iOS\Other\Cocoa Touch Unit Testing Bundle and then click Next.
Enter GuildBrowserLogicTests for the target name, make sure that Use Automatic Reference Counting is checked, and then click Finish.
You will now have a new scheme with the same name. To test that everything is working, switch to the new scheme (using the scheme selector on the Xcode toolbar next to the Run and Stop buttons) and go to Product\Test (⌘-U).
You should see that the build succeeded, but the test failed.
As the error message indicates, this is because the unit tests have not been implemented yet. Let’s fix that!
First, delete the default test class by highlighting GuildBrowserLogicTest.h and GuildBrowserLogicTest.m, tapping Delete, and then selecting Move to Trash.
So that you don’t have to remember to switch schemes, let’s modify the main app scheme, GuildBrowser, to include your new target when running Product\Test.
Switch to the GuildBrowser scheme and then edit the scheme via Product\Edit Scheme (or you can simply Alt-click on the Run button). In the scheme editor, highlight the Test configuration, click on the + button to add a new target, select GuildBrowserLogicTests in the window and then click Add.
Your screen should look similar to this:
You are now ready to add unit test classes to this test target. You will then be able to call this target from your jenkins job.
You will do exaclty that in the next installment of this feast! Stay Thirsty.. err Hungry!
Where To Go From Here?
In a couple days, we’ll post part 2 of this tutorial. In the meantime, if you have any questions or comments, please join the forum discussion below!
This is a blog post by iOS Tutorial Team member Charlie Fulton, a full time iOS developer who enjoys hunting, fishing, and hanging out with his family.