当前位置:网站首页>GHUnit: Unit Testing Objective-C for the iPhone
GHUnit: Unit Testing Objective-C for the iPhone
2022-06-26 08:15:00 【may_ he】
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!
Reprinted from :http://www.nearsoft.com/blog/printblog.html?index_php?view=article&id=305&tmpl=component&print=1
边栏推荐
猜你喜欢
How to debug plug-ins using vs Code
Oracle 19C download installation steps
Reflection example of ads2020 simulation signal
Opencv mouse event + interface interaction drawing rectangle polygon selection ROI
Baoyan postgraduate entrance examination interview - Network
Diode voltage doubling circuit
MySQL practice: 1 Common database commands
Vs2019-mfc setting edit control and static text font size
Oracle database self study notes
Household enterprises use WMS warehouse management system. What are the changes
随机推荐
Bluebridge cup 1 introduction training Fibonacci series
Crawler case 1: JS reversely obtains HD Wallpapers of minimalist Wallpapers
Opencv鼠标事件+界面交互之绘制矩形多边形选取感兴趣区域ROI
Vs2019-mfc setting edit control and static text font size
Informatics Orsay all in one 1354: bracket matching test
I want to create SQL data (storage structure)
Use intent to shuttle between activities -- use implicit intent
[postgraduate entrance examination planning group] conversion between signed and unsigned numbers
教你几招:30句哄女孩的“霸道”温馨话,不看后悔!
GHUnit: Unit Testing Objective-C for the iPhone
X-VLM多模态模型解读
. eslintrc. JS configuration
Chapter VI (pointer)
JMeter performance testing - Basic Concepts
What are the characteristics of digital factory in construction industry
Interview for postgraduate entrance examination of Baoyan University - machine learning
我想造SQL数据(存储结构)
Install the local Google plug-in and watch the free video through the oil monkey script
The solution of installing opencv with setting in pycharm
Wifi-802.11 2.4G band 5g band channel frequency allocation table