GHUnit is a test framework for Objective-C that runs on Mac OS X 10.5 and above and iPhone 3.x and above. It can be used standalone or with other testing frameworks like SenTestingKit or GTM.
These notes are for XCode 3.2.5. You can assume these steps are fragile and you need to follow them to the letter.
Setting Up the Environment
- Start XCode and create a new project by selecting New Project under the File menu,
- In the New Project popup, in the left column, select Application under iPhone OS. In the upper right panel, select Window-Based Application,
- Select your device type. Chose iPhone for this example.
- Provide a name and a location. For this example, I will use the name Calculator and the directory will be the /Users/Nearsoft/Desktop.
- Now run your application. Use
-r or from the pull-down menu Build > Build and Run.
- As a result you should see the simulator, like this,
- Now, quit the simulator and switch back to XCode.
Adding GHUnit Framework to Our Project.
- Download the last version of GHUnit Framework (as of this writing, it is 0.4.27) from here.
- Unzip the ZIP file in the same directory where you saved your project. This will create the directory GHUnitIOS.framework.
- Add a new target to your project. Control-click (right-click) on Targets and select Add - New Target.
- In the left column select iOS - Cocoa Touch and Application. Give it a descriptive name. For this example, I will use UnitTests.
- We need to add a few Frameworks to our new Target. Right-click on our target, select then Add and then choose Existing Frameworks. Lets add these three,
- CoreGraphics.framework
- Foundation.framework
- UIKit.framework
- To add the GHUnitIOS.framework, you need click on the “Add Other…” button and choose the GHUnitIOS.framework directory which just unzipped in step 9, above.
- If you've done this right so far, you will see something like this,
Add a few linker flags to our project
- Choose the UnitTests target, control-click on it and choose Get info.
- Tab and look for Other Linker Flags and double-click on that option.
- Add the following,
- -ObjC
- Add: -all_load
- Close that dialog.
- Once you add the flags you will see something like the image bellow,
- Look for the UnitTests-Info.plist in the Groups & Files column, and edit the option Main nib file base name. Then, delete this value (i.e., by clearing the field) and save it.
Add the GHUnitIOSTestMain.m
- Download GHUnitIOSTestMain. You can download it form here.
- Create a new Group in our project. I will call it UnitTests.
- Inside UnitTests Group add the GHUnitIOSTestMain.m,
- Right click on UnitTests Group, select Add – Existing Files…
- Look for the file just download and double-click on it.
- Checkmark Copy items into destination group's folder (if needed), and then click the Add button.
- Once you do that, Build and Run the UnitTests target.
NOTE: if you have a compilation error in the line #import <GHUnitIOS/GHUnit.h>
change it for #import <GHUnit/GHUnit.h>
- If you see this image, you've done well.
Create a Test
- Under the UnitTest Group, right click select Add - New File, an Objective-C class (under iOS – Cocoa Touch Class).
- Name it FirstTest.m. For Unit Test files, we do not need create a header file. So uncheck the option Also create "…".h. Verify that UnitTeststarget is selected.
- Then click Finish
- Enter this code in the FirstTest.m file,
//
// FirstTest.m
// Calculator
//
// Created by Jesus Guerra on 3/9/11.
// Copyright 2011 Nearsoft. All rights reserved.
//
#import
@interface FirstTest : GHTestCase {
NSString *stringValue;
}
@end
@implementation FirstTest
-(void)setUp {
stringValue= @"Hello World";
}
-(void)tearDown {
stringValue= nil;
}
-(void)testCompareTwoStrings {
GHAssertEquals(@"", stringValue, @"The value is not the same than '%@'", stringValue);
}
@end
- You will then have something like this,
- After that, Build and Run UnitTest target.
- When the simulator shows up, tap on Run. The test will fail, so just go back to your code and change the assertion in the method testCompareTwoStrings,
GHAssertEquals(@"Hello World", someVariableToInitialize, @"The value is not the same as '%@'", someVariableToInitialize);
And Finally …
Congratulations, we did it!
转载自:http://www.nearsoft.com/blog/printblog.html?index_php?view=article&id=305&tmpl=component&print=1