iOSiOS入的那些坑将来跳槽用

iOS单元测试怎么写 ?

2016-06-10  本文已影响2367人  怪小喵

什么是单元测试 ?

针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作。程序单元是应用的最小可测试部件。对于面向对象编程,最小单元就是方法
iOS 集成了自己的测试框架 OCUnitUITests

为什么单元测试 ?

执行单元测试,就是为了证明这段代码的行为和我们期望的一致,比如测试一些功能是否正常,接口是否能正常,特别在一些大的项目,以防止程序被误改或引起新的问题 。
单元测试还具有一下几个好处:

单元测试 怎么写 ?

我在刚写单元测试,也看了网上有很多文章,但是依然无从下手。本文总结了几点,并摘录了一些例子。

在编写单元测试代码的时候先了解到被测试方法可能会使用的外部数据,然后将这些外部数据一次设置为上面规定的这几种情况,然后再执行方法。这样就基本可以达到外部数据所有情况都能够正确测试到了。


摘录的一些例子

** 例1**
在逻辑测试的某个操作步骤前后,应该有对应的数据发生了改变,这样才能够方便我们进行测试:

@interface LXDTestsModel : NSObject
 
@property (nonatomic, readonly, copy) NSString * name;
@property (nonatomic, readonly, strong) NSNumber * age;
@property (nonatomic, readonly, assign) NSUInteger flags;
 
+ (instancetype)modelWithName: (NSString *)name age: (NSNumber *)age flags: (NSUInteger)flags;
 
- (instancetype)initWithDictionary: (NSDictionary *)dict;
- (NSDictionary *)modelToDictionary;
 
@end

在测试用例中,我定义了一个testModelConvert
方法用来测试模型跟json之间的转换是否正确:

- (void)testModelConvert
{
    NSString * json = @"{\"name\":\"SindriLin\",\"age\":22,\"flags\":987654321}";
    NSMutableDictionary * dict = [[NSJSONSerialization JSONObjectWithData: [json dataUsingEncoding: NSUTF8StringEncoding] options: kNilOptions error: nil] mutableCopy];
 
    LXDTestsModel * model = [[LXDTestsModel alloc] initWithDictionary: dict];
    XCTAssertNotNil(model);
    XCTAssertTrue([model.name isEqualToString: @"SindriLin"]);
    XCTAssertTrue([model.age isEqual: @(22)]);
    XCTAssertEqual(model.flags, 987654321);
    XCTAssertTrue([model isKindOfClass: [LXDTestsModel class]]);
 
    model = [LXDTestsModel modelWithName: @"Tessie" age: dict[@"age"] flags: 562525];
    XCTAssertNotNil(model);
    XCTAssertTrue([model.name isEqualToString: @"Tessie"]);
    XCTAssertTrue([model.age isEqual: dict[@"age"]]);
    XCTAssertEqual(model.flags, 562525);
 
    NSDictionary * modelJSON = [model modelToDictionary];
    XCTAssertTrue([modelJSON isEqual: dict] == NO);
 
    dict[@"name"] = @"Tessie";
    dict[@"flags"] = @(562525);
    XCTAssertTrue([modelJSON isEqual: dict]);
}

** 例二 **
由于单元测试是在主线程中进行的,因此异步操作的测试在执行完毕之前,往往已经结束了。有两种方法解决

//waitForExpectationsWithTimeout是等待时间,超过了就不再等待往下执行。
#define WAIT do {
 [self expectationForNotification:@"RSBaseTest" object:nil handler:nil];
 [self waitForExpectationsWithTimeout:30 handler:nil];
} while (0)
#define NOTIFY [[NSNotificationCenter defaultCenter]postNotificationName:@"RSBaseTest" object:nil]
-(void)testRequest{
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];

    // 2.发送GET请求
    [mgr GET:@"http://www.weather.com.cn/adat/sk/101110101.html" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"responseObject:%@",responseObject);
        XCTAssertNotNil(responseObject, @"返回出错");
        NOTIFY //继续执行
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
        XCTAssertNil(error, @"请求出错");
        NOTIFY //继续执行
    }];
    WAIT  //暂停
}
- (void)downloadImageURLWithString:(NSString *)URLString
{ dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    NSURL *url = [NSURL URLWithString:URLString];
    __unused Photo *photo = [[Photo alloc] initwithURL:url
                             withCompletionBlock:^(UIImage *image, NSError *error) {
                                 if (error) {
                                     XCTFail(@"%@ failed. %@", URLString, error);
                                 }

                                 dispatch_semaphore_signal(semaphore);
                             }];

    dispatch_time_t timeoutTime = dispatch_time(DISPATCH_TIME_NOW, kDefaultTimeoutLengthInNanoSeconds);
    if (dispatch_semaphore_wait(semaphore, timeoutTime)) {
        XCTFail(@"%@ timed out", URLString);
    }
}

** 借鉴 开源项目**
常用的第三方框架例如YYModel、AFNetworking、Alamofire等等优秀框架中也有对框架自身编写的单元测试,学习仿写这些单元测试也是快速提升自己的一种手段。


上一篇 下一篇

猜你喜欢

热点阅读