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.

Leave a rating/review
Save for later
Share

Learn how to add automated testing into your iOS 6 apps!

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 iOS Tutorial Team member Charlie Fulton, a full time iOS developer who enjoys hunting, fishing, and hanging out with his family.

All developers have to test their software, and many of the smart ones create test suites for this purpose. A test suite is a collection of test cases, also known as unit tests, targeting small “units” of code, usually a specific method or class.

Unit tests allow you to employ test-driven development practices and ensure that your code has fewer bugs. And by creating a unit test for each bug found, you can help make sure the bug never comes back! By unit testing and debugging proactively, you significantly narrow the chances that your app will behave unexpectedly out of the development environment.

But as you know, we’re often rushed to get finished – or we get lazy – so automating your unit tests to run automatically each time you change your code is even better. This is especially important when you’re working as part of a team.

So if you could put in place a system to automatically build, test, and then submit your app to beta testers, would you be interested? If so, then read on!

This bonus chapter and the next will walk you through setting up an automated building and testing system for your iOS apps. You will take an example project, add some unit tests to it, add a remote GitHub (github.com) repository, and then set up a continuous integration (CI) server with Jenkins. Jenkins will periodically check your GitHub repository for changes and automatically build, test, and submit your app to TestFlight!

If you’re new to automated testing, you might be wondering what some (or all) of the above terms are, including GitHub, Jenkins, and TestFlight. We’ll go into each of these in detail in this tutorial, but for now here are instant micro-introductions to these tools:

  • GitHub – Free public Git repositories, collaborator management, issue tracking, wikis, downloads, code review, graphs, and much more!
  • Jenkins – A continuous integration server and scheduler that can watch for changes to Git repositories, kick off builds and tests automatically, and notify you of results.
  • TestFlight – A service to automate the sending of iOS apps over the air to testers, and perform crash log analysis.

That ought to tide you over for a short while, giving me time to introduce the geektastic example project that will be your vehicle for this educational odyssey – but be ready to learn more about all of this in the coming pages!

Introducing GuildBrowser

In this tutorial, you will be creating an automated build and test system for an app called GuildBrowser. GuildBrowser is a simple app that lets you browse the members of a guild from the popular game World of Warcraft. You can enter a guild and a realm name to see all of the members listed in a UICollectionViewController, as you can see below:

The app is driven by the freely-available WOW API from Blizzard. You will be using the AFNetworking framework to make RESTful calls to the WOW API, retrieving JSON data that contains guild and character information.

If you never have time to play games, the next best thing is making apps that help with games!

Note: For more details about the WOW API, check out Blizzard’s GitHub project (https://github.com/Blizzard/api-wow-docs), the official WOW API docs (http://blizzard.github.com/api-wow-docs/), and the WOW forum devoted to the community platform API (http://us.battle.net/wow/en/forum/2626217/).

Gitting started

If you’re not using some form of version control for your code these days, then you’re definitely not doing it right! Arguably the most popular system right now is Git. Linus Torvalds (yes, he who created Linux) designed and developed Git as a better system than its predecessor, SVN, to manage the code for the Linux kernel. Not only is git a great source control system, but it’s integrated directly into Xcode, which makes it very convenient to use in iOS development.

Git is a “distributed” version control system, meaning that every Git working directory is its own repository, and not dependent upon a central system.

However, when you want to share your local repository with other developers, you can define a connection in your “remotes” setting. Remote repositories are versions of your project that are usually set up on a central remote server. These can even be a shared via a Dropbox folder (although this isn’t particularly recommended).

A number of services have emerged in the last few years to make it easier to set up remote repositories for Git, and other distributed version control systems. Github has become one of the best and most popular of these services, and for good reason. It offers free public Git repositories, collaborator management, issue tracking, wikis, downloads, code review, graphs and much more… sorry to sound like an advertisement, but Github is pretty awesome!

Note: For a more detailed description of Git and how you can work with it, check out the tutorial on How To Use Git Source Control with Xcode in iOS 6, coming soon to raywenderlich.com.

Your first action in this tutorial will be setting up a remote repository (repo for short) on GitHub. It will be your remote “origin” repo. It will allow you to work locally as normal, and then when you are ready to sync with teammates or send a new build to testers, you’ll push all of your local commits to this repo.

Note: This tutorial assumes you already have a GitHub account with SSH key access setup. If you do not, please consult this guide (https://help.github.com/articles/generating-ssh-keys), which shows you how to get set up by generating and using an SSH key pair. You can also use an https URL with your GitHub username/password, but the guide assumes SSH key access setup.

First, make sure you have the command line tools installed, choose the Xcode/Preferences/Downloads tab, and make sure your screen looks similar to this (the important thing is to have Installed next to Command Line Tools):

This tutorial comes with a starter project, which you can download here. Copy the project to a location of your choice on your hard drive, and open the GuildBrowser project in Xcode.

The GuildBrowser app was created with the Single View Application template using ARC, Unit testing, Storyboards, iPad-only, and a local Git repository. Compile and run, and you should see the app showing some characters from the default guild.

Open a browser, go to github.com and login to your account, or create an account if don’t have one already. Then, click on the Create a New Repo button on the top-right corner:

Enter whatever you like for the Repository name and Description fields – the name does not actually have to correspond with the name of your Xcode project. Then click the Create repository button.

Note: Do NOT check the Initialize this repository with a README box. You want to create an empty repo and then push your Xcode project into it.

Get a copy of the repository SSH URL by clicking the copy to clipboard button or typing ⌘-C (remember to tap the SSH button first to switch to the SSH URL).

Now open Organizer (Cmd-shift-2) and then select the Repositories tab.

You should see the GuildBrowser project listed at the bottom. Click on the Remotes folder and then click the Add Remote button.

Enter origin for the Remote Name, and paste in your personal GitHub URL that you copied earlier into Location:

Click Create, and Organizer should look like the following:

Don’t be alarmed that there is no password entered for the origin location. You’ll be using Git’s SSH access, so no password is needed.

You now have a remote repo set up! You can use these steps for all of your Xcode projects. This is great, but you still have not pushed anything from your local Git repo to the remote Git repo. Let’s do that now with Xcode.

Go back to your project and first make sure you have committed all of your local changes. Xcode will not allow you to push to a remote repo until you have done that. Here are the steps:

  1. Go to File\Source Control\Commit (⌥⌘-C) and on the following screen, enter a commit message like “initial commit” and click Commit.
  2. Now you need to push the local master branch to the remote origin/master branch. Go to File\Source Control\Push and click Push.
  3. Since this is the first time you’re pushing to this repo, you should see (Create) beside the Remote name, origin/master. Tap the Push button, and Xcode will handle the rest.

Once the push is complete, go back to your browser, refresh the page, and the changes you committed should now show up in your GitHub repo:

You should also now see your remote repo and its master branch listed in Organizer\Repositories\Guild Browser project\Remotes:

Congratulations, you know how to connect your local Git repo to a remote repo on GitHub and push changes to it!

Now that you have a GitHub repository set up, you could invite others to clone your repository, accept pull requests, or take a look at your code. When someone else pushes their commits to the repo on GitHub origin/master repo you go to File\Source Control\Pull (⌥⌘-X), in the popup Remote should be set to origin/master, select Choose and those changes will be added to your project.

To see what has changed, go to Organizer\Repositories\GuildBrowser, expand the arrow and click on the View Changes button.

Charlie Fulton

Contributors

Charlie Fulton

Author

Over 300 content creators. Join our team.