iOS基础

iOS - 单元测试

2020-10-20  本文已影响0人  Simple_Code

建议在实际开发中使用单元测试,尤其是大型项目,单元测试可以给你带来意想不到的结果。

单元测试对于我目前来说,主要是
1.为了方便测试一些功能是否正常运行、
2.调试接口是否能正常使用、
有时候你可能是为了测试某一个网络接口,然后每次都重新启动并且经过很多操作之后才测试到了那个网络接口。如果使用了单元测试,就可以直接测试那个方法,相对方便很多。
比如由于修改较多,我们想测试一下分享功能是否正常,这时候就有用了。(而不是重新启动程序,进入到分享界面,点击分享,填写分享内容。)其实单元测试并没有降低我们打代码的效率,我们可以在单元测试通过了,直接用到相应的地方。

在xcode新建项目中,默认都会给你建一个单元测试的target,如果你的项目没有,可以在导航中选择 File->New->Target->选择ios-test->选择iOS Unit Testing Bundle 新建一个测试target。
建好后默认会建一个测试类,代码如下:

//
//  BasicTests.m
//  BabyBluetoothAppDemo
//
//  Created by ZTELiuyw on 16/3/9.
//  Copyright © 2016年 刘彦玮. All rights reserved.
//

#import <XCTest/XCTest.h>

@interface BasicTests : XCTestCase

@end

@implementation BasicTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testExample {
    // This is an example of a functional test case.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
}

- (void)testPerformanceExample {
    // This is an example of a performance test case.
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
    }];
}

@end

其中有几点需要大家注意:

1:该类中以test开头的方法且void返回类型的方法都会变成单元测试用例
2:单元测试类继承自XCTestCase,他有一些重要的方法,其中最重要的有3个, setUp ,tearDown,measureBlock

//每次测试前调用,可以在测试之前创建在test case方法中需要用到的一些对象等
- (void)setUp ;
//每次测试结束时调用tearDown方法
- (void)tearDown ;

//性能测试方法,通过测试block中方法执行的时间,比对设定的标准值和偏差觉得是否可以通过测试
measureBlock

断言代码

// 生成一个失败的测试
    XCTFail(format…);

// 为空判断

    // 为空判断,a1 为空时通过,反之不通过
    XCTAssertNil(a1, format...);

    // 不为空判断,a1 不为空时通过,反之不通过
    XCTAssertNotNil(a1, format…);

// 为真判断

    // 为真判断,当 expression 求值为 True 时通过
    XCTAssert(expression, format...);

    // 为真判断,当 expression 求值为 True 时通过
    XCTAssertTrue(expression, format...);

    // 为假判断,当 expression 求值为 False 时通过
    XCTAssertFalse(expression, format...);

// 相等判断

    // 相等判断,[a1 isEqual:a2] 值为 True 时通过,其中一个不为空时,不通过
    XCTAssertEqualObjects(a1, a2, format...);

    // 不等判断,[a1 isEqual:a2] 值为 False 时通过
    XCTAssertNotEqualObjects(a1, a2, format...);

    // 相等判断,当 a1 和 a2 是 C 语言标量、结构体或联合体时使用,a1 == a2 值为 True 时通过
    XCTAssertEqual(a1, a2, format...);

    // 不等判断,当 a1 和 a2 是 C 语言标量、结构体或联合体时使用
    XCTAssertNotEqual(a1, a2, format...);

    // 相等判断,double 或 float 类型,提供一个误差范围,当在误差范围(+/-accuracy)以内相等时通过测试
    XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...);

    // 不等判断,double 或 float类型,提供一个误差范围,当在误差范围以内不等时通过测试
    XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...);

// 异常判断

    // 异常判断,当 expression 发生异常时通过,反之不通过
    XCTAssertThrows(expression, format...);

    // 异常判断,当 expression 发生 specificException 异常时通过,反之发生其他异常或不发生异常均不通过
    XCTAssertThrowsSpecific(expression, specificException, format...);

    // 异常判断,当 expression 发生具体异常、具体异常名称的异常时通过测试,反之不通过
    XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...);

    // 异常判断,当 expression 没有发生异常时通过测试
    XCTAssertNoThrow(expression, format…);

    // 异常判断,当 expression 没有发生具体异常、具体异常名称的异常时通过测试,反之不通过
    XCTAssertNoThrowSpecific(expression, specificException, format...);

    // 异常判断,当 expression 没有发生具体异常、具体异常名称的异常时通过测试,反之不通过
    XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...);

断言

举个例子

- (void)testExample {
    //设置变量和设置预期值
    NSUInteger a = 10;
    NSUInteger b = 15;
    // 预期值
    NSUInteger expected = 24;
    // 执行方法得到实际值
    NSUInteger actual = [self add:a b:b];
    // 断言判定实际值和预期是否符合
    XCTAssertEqual(expected, actual,@"add方法错误!");
}
- (NSUInteger)add:(NSUInteger)a b:(NSUInteger)b{
    return a + b;
}

性能测试

举个例子

- (void)testAsynExample {
    XCTestExpectation *exp = [self expectationWithDescription:@"这里可以是操作出错的原因描述。。。"];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperationWithBlock:^{
        //模拟这个异步操作需要2秒后才能获取结果,比如一个异步网络请求
        sleep(2);
        //模拟获取的异步操作后,获取结果,判断异步方法的结果是否正确
        XCTAssertEqual(@"a", @"a");
        //如果断言没问题,就调用fulfill宣布测试满足
        [exp fulfill];
    }];

    //设置延迟多少秒后,如果没有满足测试条件就报错
    [self waitForExpectationsWithTimeout:3 handler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"Timeout Error: %@", error);
        }
    }];
}

这个测试肯定是通过的,因为设置延迟为3秒,而异步操作2秒就除了一个正确的结果,并宣布了条件满足 [exp fulfill],但是当我们把延迟改成1秒,这个测试用例就不会成功,错误原因是 expectationWithDescription:@"这里可以是操作出错的原因描述。。。

异步测试除了使用 expectationWithDescription以外,还可以使用 expectationForPredicateexpectationForNotification

下面这个例子使用expectationForPredicate测试方法,代码来自于AFNetworking,用于测试backgroundImageForState方法

- (void)testThatBackgroundImageChanges {
    XCTAssertNil([self.button backgroundImageForState:UIControlStateNormal]);
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(UIButton  * _Nonnull button, NSDictionary<NSString *,id> * _Nullable bindings) {
            return [button backgroundImageForState:UIControlStateNormal] != nil;
    }];

    [self expectationForPredicate:predicate
              evaluatedWithObject:self.button
                          handler:nil];
    [self waitForExpectationsWithTimeout:20 handler:nil];
}

利用谓词计算,button是否正确的获得了backgroundImage,如果正确20秒内正确获得则通过测试,否则失败。

expectationForNotification 方法 ,该方法监听一个通知,如果在规定时间内正确收到通知则测试通过。

- (void)testAsynExample1 {
    [self expectationForNotification:(@"监听通知的名称xxx") object:nil handler:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"监听通知的名称xxx" object:nil];

    //设置延迟多少秒后,如果没有满足测试条件就报错
    [self waitForExpectationsWithTimeout:3 handler:nil];
}

这个例子也可以用expectationWithDescription实现,只是多些很多代码而已,但是这个可以帮助你更好的理解expectationForNotification 方法和 expectationWithDescription 的区别。同理,expectationForPredicate方法也可以使用expectationWithDescription实现。

 func testAsynExample1() {
    let expectation = expectationWithDescription("监听通知的名称xxx")
    let sub = NSNotificationCenter.defaultCenter().addObserverForName("监听通知的名称xxx", object: nil, queue: nil) { (not) -> Void in
        expectation.fulfill()
    }
    NSNotificationCenter.defaultCenter().postNotificationName("监听通知的名称xxx", object: nil)
    waitForExpectationsWithTimeout(1, handler: nil)
    NSNotificationCenter.defaultCenter().removeObserver(sub)
}

参考资料1: iOS单元测试
参考资料2: iOS单元测试从入门到应用(长文)

上一篇 下一篇

猜你喜欢

热点阅读