GHUnit Setup

The unit test framework that Saul M recommends is GHUnit. It is a unit-testing framework developed by Gabriel Handford. To use it, there is a bit of a setup process to go through. Here are the steps that I took.

  1. Create New Project in Xcode. Mine is called CubPath.

  2. Add a New Target… by Right-click or Control-click on the Target item in the Groups & Files pane in Xcode. Since we are creating are own test suite, select Application instead of Unit Test Bundle. Name it using convention ”Tests”, CubPathTests.

  3. Download GHUnitIOS from Github.

  4. Add GHUnitIOS.framework to your project by dragging the folder from the Finder to the Frameworks item in the Groups & Files pane in Xcode. Select “Copy items into destination group’s folder (if needed) and select your Tests target and deselect the Project target in “Add to Target”.

  5. In Finder, go into your projects folder. Duplicate your project’s precompiled header, <Project Name>_Prefix.pch and rename it to <Project Name>Tests_Prefix.pch. For me, it is CubPath_Prefix.pch, which I renamed to CubPathTests_Prefix.pch.

  6. Right-click or Control-click Other Sources in Groups & Files pane, and Add Existing Files… <Project Name>Tests_Prefix.pch. Make sure it is only added to Tests target.

  7. Edit <Project Name>Tests_Prefix.pch. and add #import <GHUnit/GHUnit.h> to the import statements. Doing this allows you to avoid including that import in every test file.

  8. Now we need to configure the Tests Target. Right-click or Control-click in the Groups & Files pane and select Get Info.

    1. On the General tab, add your Project Target (CubPath for me) to the Direct Dependencies section and add CoreGraphics.framework, Foundation.framework and UIKit.framework to the Linked Libraries section.
    2. On the Build tab, add -all_load and -ObjC to Other Linker Flags. (Typing ”other linker” in the Search bar can help find it.)
    3. Also on the Build tab, search for GCC_Prefix. GCC_PRECOMPILE_PREFIX_HEADER should be set to YES and GCC_PREFIX_HEADER should be set to <Project Name>Tests_Prefix.pch. Correct them if they are not.
  9. Select <Project Name>Tests-Info.plist and clear the Main nib file base name field. Its default setting is MainWindow.

  10. Download and add GHUnitIOSTestMain.m into your project in the Other Sources folder in the Groups & Files pane.

  11. I like to keep my unit-test source files separate from my project source files. To do this, in Finder I’ll create a Tests folder inside my project folder. Then, back in Xcode, I’ll Right-click/Control-click in the Groups & Files pane, and Add Existing Files… I’ll select the recently created Tests folder and Add. Uncheck Copy items into destination group’s folder (if needed) and <Project Name> target, and check <Project Name>Tests target.

  12. Now we can finally add a unit-test file. Right-click/Control-click the Tests folder in the Groups & Files pane, and Add New File… Select Objective-C class with Subclass of NSObject and click Next. Give it an appropriate file name. Since we want to make sure our setup is good, I named it SetupTests.m. I unchecked Also create “SetupTests.h”, and selected <Project Name>Tests target. Notice how location is set to your project directory/Tests created in step 11 above.

a. Since I didn’t create SetupTests.h, I had to add the interface to SetupTests.m. And I added two unit-test methods: testFirstUT and testSecondUT.

1
2
3
4
5
6
7
8
9
10
11
@interface SetupTests : GHTestCase {}
@end

@implementation SetupTests
- (void) testFirstUT {
       GHAssertEquals(1, 2, @"Should fail");
}
- (void) testSecondUT {
       GHAssertEquals(1, 1, @"Should pass");
}
@end

b. Click Build and Run. This screen should appear in the Simulator unless if you are configured for Device.

c. Tap Run and you should see testFirstUT fail and testSecondUT pass.

Seems like a lot of work for a little output. Hopefully, with time, you will see the benefit unit-testing will have not only with the quality of your code, but the the quality of your software skills. Unit-testing is a part of agile development which is the rage these days and having this in your set of tools will only benefit you as a developer in today’s market.