GHUnit for iOS测试指南
GHUnit簡(jiǎn)介
GHUnit是一個(gè)基于Object C的測(cè)試框架,支持Mac OSX 10.5和iOS 3.0以上版本,GHUnit的特點(diǎn)在于,它提供了一個(gè)供Mac和iOS程序使用的前端界面,提供了根據(jù)鍵盤按鍵來過濾測(cè)試結(jié)果的能力,也提供了比XCode更為豐富的,用于控制測(cè)試結(jié)果顯示方式的功能。GHUnit框架提供圖形界面來進(jìn)行測(cè)試,而不是將測(cè)試注入應(yīng)用程序中。需要新建一個(gè)編譯目標(biāo),其中包含測(cè)試代碼,以及用于檢測(cè)和運(yùn)行測(cè)試的GHUnit框架。下面的內(nèi)容來自GHUnit官網(wǎng),自己對(duì)著試了一遍,copy過來以備不時(shí)之需: http://gabriel.github.io/gh-unit/index.html
GHUnit框架源碼:http://download.csdn.net/detail/jjunjoe/5585963
Installing in iOS (Xcode 4)
To use GHUnit in your project, you’ll need to create and configure a test target.
Create Test Target
- You’ll want to create a separate Test target. Select the project file for your app in the Project Navigator. From there, select the Add Target + symbol at the bottom of the window.
- Select iOS, Application, Window-based Application. Select Next.
- Name it Tests or something similar. Select Finish.
Configure the Test Target
- Download and copy the GHUnitIOS.framework to your project. Command click on Frameworks in the Project Navigator and select: Add Files to “MyTestable”. (This should automatically add GHUnitIOS.framework to your Link Binary With Libraries Build Phase for the Tests target.)
- Select GHUnitIOS.framework and make sure the only the Tests target is selected.
- We want to enable use of Objective-C categories, which isn’t enabled for static libraries by default. In the Tests target, Build Settings, under Other Linker Flags, add -ObjC and -all_load.
- Select and delete the files from the existing Tests folder. Leave the Supporting Files folder. GHUnit will provide the application delegate below.
- In Tests folder, in Supporting Files, main.m, replace the last argument of UIApplicationMain with?@"GHUnitIOSAppDelegate". Remove the #import “AppDelegate.h” if present.
- Select the Tests target, iPhone Simulator configuration:
- Hit Run, and you’ll hopefully see the test application running (but without any tests).
Now you can?create and run tests!
Create a Test
- Command click on the Tests folder and select: New File…
- Select Objective-C class (iOS, Cocoa Touch or Mac OS X, Cocoa) and select Next. Leave the default subclass and select Next again.
- Name the file MyTest.m and make sure its enabled only for the “Tests” target.
-
Delete the MyTest.h file and update the MyTest.m file.
#import <GHUnitIOS/GHUnit.h> @interface MyTest : GHTestCase { }@end@implementation MyTest- (void)testStrings { NSString *string1 = @"a string";GHTestLog(@"I can log to the GHUnit test console: %@", string1);// Assert string1 is not NULL, with no custom error descriptionGHAssertNotNULL(string1, nil);// Assert equal objects, add custom error descriptionNSString *string2 = @"a string";GHAssertEqualObjects(string1, string2, @"A custom error message. string1 should be equal to: %@.", string2);}@end
- Now run the “Tests” target. Hit the Run button in the top right.
Examples
ExampleTest.m:
// For iOS #import <GHUnitIOS/GHUnit.h> // For Mac OS X //#import <GHUnit/GHUnit.h>@interface ExampleTest : GHTestCase { } @end@implementation ExampleTest- (BOOL)shouldRunOnMainThread {// By default NO, but if you have a UI test or test dependent on running on the main thread return YES.// Also an async test that calls back on the main thread, you'll probably want to return YES.return NO; }- (void)setUpClass {// Run at start of all tests in the class }- (void)tearDownClass {// Run at end of all tests in the class }- (void)setUp {// Run before each test method }- (void)tearDown {// Run after each test method } - (void)testFoo { NSString *a = @"foo";GHTestLog(@"I can log to the GHUnit test console: %@", a);// Assert a is not NULL, with no custom error descriptionGHAssertNotNULL(a, nil);// Assert equal objects, add custom error descriptionNSString *b = @"bar";GHAssertEqualObjects(a, b, @"A custom error message. a should be equal to: %@.", b); }- (void)testBar {// Another test }@endExampleAsyncTest.m:
// For iOS #import <GHUnitIOS/GHUnit.h> // For Mac OS X //#import <GHUnit/GHUnit.h> @interface ExampleAsyncTest : GHAsyncTestCase { } @end@implementation ExampleAsyncTest- (void)testURLConnection {// Call prepare to setup the asynchronous action.// This helps in cases where the action is synchronous and the// action occurs before the wait is actually called.[self prepare];NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]http://www.google.com"]];NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];// Wait until notify called for timeout (seconds); If notify is not called with kGHUnitWaitStatusSuccess then// we will throw an error.[self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0];[connection release]; }- (void)connectionDidFinishLoading:(NSURLConnection *)connection {// Notify of success, specifying the method where wait is called.// This prevents stray notifies from affecting other tests.[self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testURLConnection)]; }- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {// Notify of connection failure[self notify:kGHUnitWaitStatusFailure forSelector:@selector(testURLConnection)]; }- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {GHTestLog(@"%@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]); } @endExample projects can be found at:?http://github.com/gabriel/gh-unit/tree/master/Examples/
Assert Macros
The following test macros are included.
These macros are directly from:?GTMSenTestCase.h?prefixed with GH so as not to conflict with the GTM macros if you are using those in your project.
The description argument appends extra information for when the assert fails; though most of the time you might leave it as nil.
GHAssertNoErr(a1, description, ...) GHAssertErr(a1, a2, description, ...) GHAssertNotNULL(a1, description, ...) GHAssertNULL(a1, description, ...) GHAssertNotEquals(a1, a2, description, ...) GHAssertNotEqualObjects(a1, a2, desc, ...) GHAssertOperation(a1, a2, op, description, ...) GHAssertGreaterThan(a1, a2, description, ...) GHAssertGreaterThanOrEqual(a1, a2, description, ...) GHAssertLessThan(a1, a2, description, ...) GHAssertLessThanOrEqual(a1, a2, description, ...) GHAssertEqualStrings(a1, a2, description, ...) GHAssertNotEqualStrings(a1, a2, description, ...) GHAssertEqualCStrings(a1, a2, description, ...) GHAssertNotEqualCStrings(a1, a2, description, ...) GHAssertEqualObjects(a1, a2, description, ...) GHAssertEquals(a1, a2, description, ...) GHAbsoluteDifference(left,right) (MAX(left,right)-MIN(left,right)) GHAssertEqualsWithAccuracy(a1, a2, accuracy, description, ...) GHFail(description, ...) GHAssertNil(a1, description, ...) GHAssertNotNil(a1, description, ...) GHAssertTrue(expr, description, ...) GHAssertTrueNoThrow(expr, description, ...) GHAssertFalse(expr, description, ...) GHAssertFalseNoThrow(expr, description, ...) GHAssertThrows(expr, description, ...) GHAssertThrowsSpecific(expr, specificException, description, ...) GHAssertThrowsSpecificNamed(expr, specificException, aName, description, ...) GHAssertNoThrow(expr, description, ...) GHAssertNoThrowSpecific(expr, specificException, description, ...) GHAssertNoThrowSpecificNamed(expr, specificException, aName, description, ...)Custom Test Case Classes
You can register additional classes at runtime; if you have your own. For example:
[[GHTesting sharedInstance] registerClassName:@"MySpecialTestCase"];Using an Alternate (Test) iOS Application Delegate
If you want to use a custom application delegate in your test environment, you should subclass?GHUnitIOSAppDelegate:
@interface MyTestApplicationDelegate : GHUnitIOSAppDelegate { }@endThen in main.m (or GHUnitIOSTestMain.m):
int retVal = UIApplicationMain(argc, argv, nil, @"MyTestApplicationDelegate");Using SenTestingKit
You can also use GHUnit with SenTestCase, for example:
#import <SenTestingKit/SenTestingKit.h>@interface MyTest : SenTestCase { } @end@implementation MyTest- (void)setUp {// Run before each test method }- (void)tearDown {// Run after each test method }- (void)testFoo {// Assert a is not NULL, with no custom error descriptionSTAssertNotNULL(a, nil);// Assert equal objects, add custom error descriptionSTAssertEqualObjects(a, b, @"Foo should be equal to: %@. Something bad happened", bar); }- (void)testBar {// Another test }@end總結(jié)
以上是生活随笔為你收集整理的GHUnit for iOS测试指南的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 恋与制作人安卓和ios互通吗
- 下一篇: 使用DISM++挂载ISO镜像进行修改