GHUnit for iOS测试指南

GHUnit简介

GHUnit是一个基于Object C的测试框架,支持Mac OSX 10.5和iOS 3.0以上版本,GHUnit的特点在于,它提供了一个供Mac和iOS程序使用的前端界面,提供了根据键盘按键来过滤测试结果的能力,也提供了比XCode更为丰富的,用于控制测试结果显示方式的功能。GHUnit框架提供图形界面来进行测试,而不是将测试注入应用程序中。需要新建一个编译目标,其中包含测试代码,以及用于检测和运行测试的GHUnit框架。

下面的内容来自GHUnit官网,自己对着试了一遍,copy过来以备不时之需:

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.

Add Target

  • Select iOS, Application, Window-based Application. Select Next.

Select Application

  • Name it Tests or something similar. Select Finish.

Name it

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.)

Add Framework

  • Select GHUnitIOS.framework and make sure the only the Tests target is selected.

Add Framework Dialog

  • 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.

Other Linker Flags

  • Select and delete the files from the existing Tests folder. Leave the Supporting Files folder. GHUnit will provide the application delegate below.

Remove Test Files

  • In Tests folder, in Supporting Files, main.m, replace the last argument of UIApplicationMain with @"GHUnitIOSAppDelegate". Remove the #import “AppDelegate.h” if present.

Main Method

  • Select the Tests target, iPhone Simulator configuration:

Select Target

  • Hit Run, and you’ll hopefully see the test application running (but without any tests).

Run It

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 description
        GHAssertNotNULL(string1, nil);
    
        // Assert equal objects, add custom error description
        NSString *string2 = @"a string";
        GHAssertEqualObjects(string1, string2, @"A custom error message. string1 should be equal to: %@.", string2);
      }
    
      @end
    

Add Test

  • Now run the “Tests” target. Hit the Run button in the top right.

Running Test

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 description
  GHAssertNotNULL(a, nil);

  // Assert equal objects, add custom error description
  NSString *b = @"bar";
  GHAssertEqualObjects(a, b, @"A custom error message. a should be equal to: %@.", b);
}

- (void)testBar {
  // Another test
}

@end

ExampleAsyncTest.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]);
} 

@end

Example 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 { }
 @end

Then 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 description
 STAssertNotNULL(a, nil);

 // Assert equal objects, add custom error description
 STAssertEqualObjects(a, b, @"Foo should be equal to: %@. Something bad happened", bar);
}

- (void)testBar {
 // Another test
}

@end

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值