当前位置:网站首页>GHUnit: Unit Testing Objective-C for the iPhone
GHUnit: Unit Testing Objective-C for the iPhone
2022-06-26 08:10: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!
转载自:http://www.nearsoft.com/blog/printblog.html?index_php?view=article&id=305&tmpl=component&print=1
边栏推荐
- Minor problems in importing D
- Informatics Orsay all in one 1354: bracket matching test
- buuresevewp
- ReW_ p
- Rewrite string() method in go language
- The first multi platform webcast of 2021ccf award ceremony pays tribute to the winners! CCF outstanding engineer
- Solve psycopg2 NotSupportedError: PQconninfo not available in libpq < 9.3
- Click the button to call the system browser to open Baidu home page
- Google Earth engine (GEE) 01- the prompt shortcut ctrl+space cannot be used
- Pic 10B parsing
猜你喜欢
Record the dependent installation problems encountered in building the web assets when developing pgadmin
Baoyan postgraduate entrance examination interview - operating system
Google Earth Engine(GEE) 02-基本了解和学习资源
Double linked list -- tail interpolation construction (C language)
Interview for postgraduate entrance examination of Baoyan University - machine learning
Opencv mouse event + interface interaction drawing rectangle polygon selection ROI
Oracle database self study notes
PyTorch-12 GAN、WGAN
Uploading pictures with FileReader object
You can command Siri without making a sound! The Chinese team of Cornell University developed the silent language recognition necklace. Chinese and English are OK
随机推荐
[UVM practice] Chapter 3: UVM Fundamentals (3) field automation mechanism
Livevideostackcon | evolution of streaming media distribution for online education business
2: String insert
PyTorch-12 GAN、WGAN
Automatic backup of MySQL database in the early morning with Linux
Comparison version number [leetcode]
Data governance: from top project to data culture!
Jz-063- median in data stream
How to debug plug-ins using vs Code
Crawler case 1: JS reversely obtains HD Wallpapers of minimalist Wallpapers
You can command Siri without making a sound! The Chinese team of Cornell University developed the silent language recognition necklace. Chinese and English are OK
What are the key points of turnover box management in warehouse management
Okhttp3 source code explanation (IV) cache strategy, disadvantages of Android mixed development
MySQL practice: 4 Operation of data
45. jumping game II dynamic planning DP
1. error using XPath to locate tag
Informatics Orsay all in one 1354: bracket matching test
[UVM practice] Chapter 2: a simple UVM verification platform (3) add various components to the verification platform
Interview JS and browser
Arrangement and insertion structure