Step 1: A Create a New iPhone Project
Create a new project via File > New Project. A Choose "View-Based Application" (you can choose any, but we'll use this type here.) A Name your project "example1'' to follow along with this tutorial.
After your project is created, it should look something like this:
The "example1" project after creation.
Now that you have created the project, you can add the supporting files to enable unit testing.
Step 2: Create a Folder to Organize Your Unit Testing "Framework"
One of the lessons learned from my iPhone consulting is that iPhone projects can get big and messy. A It helps if you isolate your "frameworks" into folders in Finder. A I say "frameworks" loosely because you can't really have Mac OS X-style frameworks in your iPhone projects. A There is no dynamic loading of libraries (which frameworks are) on the iPhone. A Instead, you end up copying all the files into your project.
In this example, we will create a folder named "UnitTestingFramework" in our project to separate it from the rest of our code. A If you're using a lot of external code in your project, you could put this under another folder called "OpenSourceFrameworks." A I've done this in most of my consulting projects, but we'll keep it simple since we're only working with one "framework" in this tutorial.
So: In Finder, create a new Folder inside your "example1'' project folder called "UnitTestingFramework."
Create a folder named UnitTestingFramework in the example1 project folder.
Drag and drop the "UnitTestingFramework" folder from Finder into the "example1'' project in Xcode. A When the sheet pops up, do not select "Copy items into destination group's folder (if needed)." A The reason is because it already is in the "example1'' folder.
You should see a Group called "UnitTestingFramework" underneath the "example1'' Project.
You should see a UnitTestingFramework group underneath the example1 project.
Step 3: Find and Install a Unit Testing Framework
In the first post, I alluded to a group of "diligent and ingenious engineers". A I was specifically referring to the Google Mac Team. A They've created a great project called Google Toolbox for Mac. A It actually does a lot more than unit testing. A For example, I used their Address Book interface to make working with Address Book on the iPhone much nicer.
In this tutorial, we'll focus on one subset of Google Toolbox for Mac, the unit testing portion. A We won't even use the entire unit testing portion, since there are helpers for Mac OS X unit testing as well.
First, you'll need to download the source. A The latest stable version is their 1.5.1 from June of 2008. A You can get it from here:A google-toolbox-for-mac-1-5-1.zip
You could also get the latest from Subversion, but we'll focus on the stable version for this tutorial.
Unzip the file by double-clicking on it in Finder if it wasn't unzipped already into a folder. A Then go into the folder and find the following 7 files:
The 7 files you need to select from Google Toolbox for Mac.
Drag and drop those 7 selected files to the "UnitTestingFramework" group in Xcode. A When the sheet appears, make sure to check "Copy items into destination group's folder (if needed.)" A This time, we need to copy them from the Google Toolbox for Mac download to our project and specifically to the UnitTestingFramework folder we created in Step 2.
You have all the raw ingredients now, but you will need to perform a few more steps to be able to find and run unit tests in your iPhone project.
Step 4: Create a "Tests" Target
Create a new target via Project > New Target.
Choose iPhone OS > Cocoa Touch > Application.
Call it "Tests".
In the first post of this series, we discussed that there are only two types of Targets available for iPhone projects. A Applications are the only ones that can run and we want to execute our unit tests. A That is why we are adding this new "Tests" target.
We are almost there. A Now we just need to tell the "Tests" target to find and execute our unit tests.
Step 5: Add a Run Script Build Phase to the "Tests" Target
Select the "Tests" target.
Control-click (or right-click or whatever finger gestures you use on your fancy new MacBook Pro with no buttons) and select Add > New Build Phase > New Run Script Build Phase.
Enter "./UnitTestingFramework/RunIPhoneUnitTest.sh" in the Script text area. A Note that if you had spaces in your folder, you would need to escape it with a backslash like "\ " here. A So another lesson is to not include spaces in your folders to keep things simple.
Enter "./UnitTestingFramework/RunIPhoneUnitTest.sh" in the Script text area.
What is this "RunIPhoneUnitTest.sh"?
It is a shell script that the Google Mac team has created. A It sets up some environment variables to help with executing the unit tests. A The real magic will be explained later in this series. A Hint: it is in those files you selected and dragged into your project.
Step 6: Ensure All the "UnitTestingFramework" Files Are Part of the "Tests"A Target
Select the "Tests" target in the Active Target portion of the Toolbar. A You can also select in via the menu item Project > Set Active Target > Tests. A Ensure tha the Active Build Configuration is Debug and the Active SDK is Simulator.
Select the UnitTestingFramework group. A Look at the list on the right side and make sure that all the *.m files are checked. A This ensures that they will be included in the Tests target. A Check any that are unchecked.
Note that you'll want to make sure these are all unchecked in your "example1'' target as well. A You don't need these testing "framework" files in your regular application target.
Check all the UnitTestingFramework *.m files for the Tests target.
Step 7: Run the Tests and Verify Success
At this point, the project is fully set up to run unit tests for an iPhone project. A To verify this, press the Build button on the toolbar or use the menu item Build > Build.
Open up the Build Results so you can see the progress. A You can use the menu item Build > Build Results.
To better see the progress, you will want to look at the detailed Build Transcript. A To see this, you'll need to click on the little transcript icon in the Build Results window.
Click on the Build Transcript button to see the details of the unit tests progress.
You should see: (give or take a few miliseconds)
"Executed 0 tests, with 0 failures (0 unexpected) in 0.004 (0.004) seconds".
This is success! A Your project is set up correctly. A We have no tests yet. A We will cover that tomorrow. A But this shows that the unit testing "framework" is running correctly. A If we did have any tests, they would execute and show in the Build Details.
If you don't see this, don't worry - try reading part 4 of this series: Pitfalls That You May Encounter when Running iPhone Unit Tests and How to Overcome Them. A You can also visit the Google Toolbox for Mac Google Group for support via an email discussion list.
Note that the results were reported twice - this is a known bug in 1.5.1 and is fixed in a later version.
You can see the completed version of this project atA http://github.com/luisdelarosa/iphone_unit_testing. A That's sort of like when you see a chef pull out a fully cooked pie at the end of A cooking show.