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
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

Continuous integration

Continuous integration (CI) is having your code automatically built and tested (and even deployed!) by a server that is continuously watching your revision control system for changes.

CI becomes tremendously important when you have a lot of developers on a project sending in their changes multiple times per day. If any conflicts or errors arise, everyone is notified immediately. Eventually, someone breaks the build!

Meet Jenkins

Jenkins (http://jenkins-ci.org) is the open source CI server you’re going to use in this tutorial to automatically build, test, and deploy code that is pushed to the GitHub remote repo.

You’ll spend the rest of this section getting Jenkins up and running.

Installing Jenkins

There are a few different ways you can install Jenkins on a Mac:

  1. You can use the installer from the Jenkins website, which will set up Jenkins to run as a daemon. This is nice because it is configured to use the Max OS X startup system, but it will result in you, the user, having to go through a few more steps in order to get Xcode building to work properly.
  2. You can run the WAR file (a Java archive filetype) from the Terminal, which uses the built-in winstone servlet container to run Jenkins.
  3. You can deploy the WAR file to an application container you have already set up, such as Tomcat, JBoss, etc.

In this tutorial, you are going to run Jenkins from the Terminal, because it’s the quickest and easiest way to do it. Download the Jenkins WAR file from http://mirrors.jenkins-ci.org/war/latest/jenkins.war. Open a Terminal window, and then run this command:

nohup java -jar ~/Downloads/jenkins.war --httpPort=8081 --ajp13Port=8010 > /tmp/jenkins.log 2>&1 &

Note: The above assumes that you have the Jenkins WAR file in your Downloads folder. If you downloaded the WAR file to a different location, adjust the path in the command accordingly.

Note: You might find it useful to create an alias for starting up Jenkins. From a Terminal, or text editor showing hidden files, open up /Users/(username)/.bash_profile and enter this line:

Save .bash_profile, open a new Terminal, and now type in jenkins to start it up.

alias jenkins="nohup java -jar ~/jenkins.war --httpPort=8081 --ajp13Port=8010 > /tmp/jenkins.log 2>&1 &"
alias jenkins="nohup java -jar ~/jenkins.war --httpPort=8081 --ajp13Port=8010 > /tmp/jenkins.log 2>&1 &"

This starts the Jenkins server on port 8081. The nohup command is a UNIX command that allows a process to keep running after you have logged out or the shell has exited – it stands for “no hangup”.

The above command also sets up a log file to /tmp/jenkins.log. I like to configure the ports to avoid any conflicts with servers I have running. To access Jenkins, open the following URL in a browser:

http://localhost:8081/

Configure Jenkins plugins

You need to add a few things to Jenkins before you can start setting up jobs. Open the Jenkins dashboard (via http://localhost:8081) and click on the Manage Jenkins\Manage Plugins\Available tab. There are a lot of plugins available, so filter the choices down by typing Git into the filter search box in the top right.

Here’s what you want:

Check the box next to the Git Plugin and then click the Install without restart button. You should see the following screen when it’s done installing:

Now enter Chuck Norris in the filter search box, and look for the Chuck Norris quote plugin. While this one is optional for the tutorial, I highly recommend you check it out! If you don’t install it, he will know! Beware the wrath of Chuck. :]

Note: My friends call me Charlie, but my real name is Charles. My Dad’s middle name was Norris. So I had legitimate claims to naming one of my sons Chuck Norris Fulton… but unfortunately she wouldn’t let me. :]

Setting up Jenkins email notification

It would be nice to have an SMTP server so that Jenkins can send you build notification errors. But if you’re not running an SMTP server, you can use your Gmail account with Jenkins (or any other SMTP server you prefer).

Go to Manage Jenkins\Configure System and in the Email Notification section, enter the following settings (use the Advanced Settings button to access some of the settings):

  • SMTP Server : smtp.gmail.com
  • Sender Email address :
  • Use SMTP Authentication
  • User Name:
  • Password:
  • Use SSL
  • SMTP Port:465

Now test out email notifications by checking the Test configuration by sending test-email, entering an email address, and clicking the Test configuration button. Once you’ve confirmed that the test is successful, remember to tap the Save button to save your configuration changes.

Now you can receive emails from Jenkins about build failures.

Creating a Jenkins job

You are now ready to create a Jenkins job! Open the Jenkins dashboard, click on New Job, enter GuildBrowser for the job name, choose the Build a free-style software project radio button, and finally click the OK button. You should now see the job configuration screen.

In the Source Code Management section, click the Git radio button, and enter your GitHub remote repo (origin/master) URL into the Repository URL text field. Your screen should look something like this:

Go to the Post-build Actions section, select Email Notification from the Add post-build action button, enter the email address(es) for the recipient(s), and finally check Send email for every unstable build. You should see the following:

If you installed the Chuck Norris plugin, you can also add a post-build action to Activate Chuck Norris. That sounds kinda scary though! Warning, don’t break the build!!

Note: The programs that Chuck Norris writes don’t have version numbers because he only writes them once. If a user reports a bug or has a feature request, they don’t live to see the sun set. :]

Don’t forget to click the Save button.

Let’s make sure everything is set up correctly with GitHub by clicking the Build Now button on the left sidebar. When the job starts, you will see an update in the Build History box:

If everything works correctly, your build #1 should be blue, indicating success:

To see what exactly happened during the build, hover over the build and look at the Console Output:

Your console output should look similar to this, showing that the job successfully pulled down the code from the GitHub repo:

So far, you’ve connected to GitHub with your job and set up email notification. Let’s press on and add the interesting bits: testing, building, and uploading your project to TestFlight.

Note: A Jenkins pro tip! To quickly get to the job configure screen, hover over the job name on the breadcrumb trail at the top of the screen and select Configure from the menu. This is especially helpful when you’re looking at console output

Charlie Fulton

Contributors

Charlie Fulton

Author

Over 300 content creators. Join our team.