iOS单元测试

Xcode集成了对测试的支持,其中单元测试使用的是XCTest框架 <XCTest/XCTest.h> ,良好的单元测试可以提高产品的稳定性,快速定位bug,节省开发时间。

本文主要介绍一下单元测试的基本用法。

1、创建、增加单元测试

在xcode新建项目中,可以勾选下面的第二个选项——同时创建单元测试,


如果你的项目没有,可以在导航中选择 File->New->Target->选择ios-test->选择iOS Unit Testing Bundle 新建一个测试target。


我创建一个专门单元测试的demo,叫Uint_test。


建好后默认会建一个测试类,代码如下:

#import <XCTest/XCTest.h>
#import "AddManager.h"

@interface Uint_testTests : XCTestCase

@end

@implementation Uint_testTests

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

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

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

//此方法每次测试前调用
- (void)setUp ;
//此方法每次测试结束时调用
- (void)tearDown ;

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

断言

大部分的测试方法使用断言决定的测试结果。所有断言都有一个类似的形式:比较,表达式为真假,强行失败等。


//通用断言
XCTAssert(expression, format...)
//常用断言:
XCTAssertTrue(expression, format...)
XCTAssertFalse(expression, format...)
XCTAssertEqual(expression1, expression2, format...)
XCTAssertNotEqual(expression1, expression2, format...)
XCTAssertEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNotEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNil(expression, format...)
XCTAssertNotNil(expression, format...)

XCTFail(format...) //直接Fail的断言
2、例子
- (void)testExample {
    // This is an example of a functional test case.
    // Use XCTAssert and related functions to verify your tests produce the correct results.

    AddManager *manager = [[AddManager alloc] init];
    NSInteger actual = [manager resultFromA:2 B:3];

    NSInteger expect = 5;
//    XCTAssertTrue(6 == [a resultFromA:2 B:3],@"");

    XCTAssertEqual(actual, expect);
}
结果设置为5时,测试是通过的。如果设置成6,则会报错误。
从这也能看出一个测试用例比较规范的写法,1:定义变量和预期,2:执行方法得到实际值,

性能测试

性能测试主要使用 measureBlock 方法 ,用于测试一组方法的执行时间,通过设置baseline(基准)和stddev(标准偏差)来判断方法是否能通过性能测试。

举个栗子:

- (void)testPerformanceExample {
    // This is an example of a performance test case.
    [self measureBlock:^{
		  //Put the code you want to measure the time of here.
		  //你的性能测试的代码放在这里
    }];
}

直接执行方法,因为block中没有内容,所以方法的执行时间为0.0s,如果我们把baseline设成0.05,偏差10%,是可以通过的测试的。但是如果设置如果我们把baseline为1,偏差10%,那测试会失败,因为不满足条件。

期望

期望实际上是异步测试,当测试异步方法时,因为结果并不是立刻获得,所以我们可以设置一个期望,期望是有时间限定的的,fulfill表示满足期望

举个栗子

    - (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以外,还可以使用 expectationForPredicate和expectationForNotification 

下面这个例子使用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];
}

命令行测试

测试不仅可以在xcode中执行,也可以在命令行中执行,这个便于代码持续集成和构建,在git提交中也编译检查代码

如果你有development-enabled设备插入,你可以按照名称或 id 调用他们。例如,如果你有一个名为”Development iPod touch”的 iPod 设备连接了测试的代码,可以使用下面的命令来测试代码 > xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp -destination 'platform=iOS,name=Development iPod touch 

测试也可以在 iOS模拟器上运行。使用模拟器可以应对不同的外形因素和操作系统版本。例如> xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp -destination 'platform=iOS Simulator,name=iPhone,0S=7.0' 

-destination 参数可以被连接在一起,这样你只需使用一个命令,就可以跨目标进行指定集成共享方案。例如,下面的命令把之前的三个例子合并到一个命令中

> xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp
-destination 'platform=OS X,arch=x86_64'
-destination 'platform=iOS,name=Development iPod touch'
-destination 'platform=iOS Simulator,name=iPhone,0S=7.0'

关于更多xcodebuild的使用可以查看man手册 > man xcodebuild 

执行测试快捷键

  • cmd + 5 切换到测试选项卡后会看到很多小箭头,点击可以单独或整体测试
  • cmd + U 运行整个单元测试

注意点

都是血与泪的教训

  • 使用pod的项目中,在XC测试框架中测试内容包括第三方包时,需要手动去设置Header Search Paths才能找到头文件 ,还需要设置test target的PODS_ROOT。
  • xcode7要使用真机做跑测试时,证书必须配对,否则会报错exc_breakpoint错误
  • XCTestExpectation的fulfill方法只能调用一次,系统不会帮你检查,如果你调用两次就会出错,而且你经常都找不到错在哪里。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值