iOS之性能优化

iOS单元测试使用

2018-01-18  本文已影响17人  考槃在涧

关于iOS单元测试

单元测试是针对程序模块来进行正确性检验的测试工作。程序单元是应用的最小可测试部件。进行单元测试,目的就是为了证明这段代码的行为和我们期望的一致,比如测试一些功能是否正常,接口是否正常。

iOS集成了自己的测试框架Unit TestsUI Tests。在新建一个工程的时候,可以直接选择包含单元测试框架:

单元测试
也可以在Xcode上方菜单栏选择 File->New->Target,打开之后选择iOS->iOS Unit Testing Bundle。
工程建好之后,我们可以看到一个工程名+Tests的文件夹,打开里面的.m文件,我们可以看到有几个方法:
- (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.
    // 测试用例的例子,注意测试用例一定要test开头
}

- (void)testPerformanceExample {
    // This is an example of a performance test case.
    // 测试性能例子
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
        // 测试代码执行时间
    }];
}

上面代码中的- (void)testExample方法只是一个测试用例的例子,我们还可以自己定义测试用例方法,但是必须以test来开头,一般test后面跟上要测试的方法名。

如何进行测试

我们可以先在ViewController.h中随便定义一个方法,返回一个int值:

- (int)number;

然后在ViewController.m中实现:

- (int)number{
    return 10;
}

接下来我们开始对这个方法进行简单的测试,我们在测试文件中导入ViewController.h,然后创建一个属性:

@property (nonatomic, strong) ViewController *vc;

在测试类的- (void)setUp中对vc进行初始化:_vc = [[ViewController alloc] init];。在tearDown方法中将测试用例的资源释放:self.vc = nil;注意释放要在[super tearDown]之前执行。

然后写测试用例:

- (void)testExample {
    int result = [self.vc number];
    // 测试返回的结果是否等于10
    XCTAssertEqual(result, 10,@"hahaha"); 
}

最后Command+u执行测试。也可以把测试的结果改一下运行看测试不通过会有什么效果。

XCTest一些常用的断言(暂时写这么多,其他自己查):

XCTFail(format…) 生成一个失败的测试; 
XCTAssertNil(a1, format...)为空判断,a1为空时通过,反之不通过;
XCTAssertNotNil(a1, format…)不为空判断,a1不为空时通过,反之不通过;
XCTAssert(expression, format...)当expression求值为TRUE时通过;
XCTAssertTrue(expression, format...)当expression求值为TRUE时通过;
XCTAssertFalse(expression, format...)当expression求值为False时通过;
XCTAssertEqualObjects(a1, a2, format...)判断相等,[a1 isEqual:a2]值为TRUE时通过,其中一个不为空时,不通过;
XCTAssertNotEqualObjects(a1, a2, format...)判断不等,[a1 isEqual:a2]值为False时通过;
XCTAssertEqual(a1, a2, format...)判断相等(当a1和a2是 C语言标量、结构体或联合体时使用, 判断的是变量的地址,如果地址相同则返回TRUE,否则返回NO);
XCTAssertNotEqual(a1, a2, format...)判断不等(当a1和a2是 C语言标量、结构体或联合体时使用);
XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...)判断相等,(double或float类型)提供一个误差范围,当在误差范围(+/-accuracy)以内相等时通过测试;
XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判断不等,(double或float类型)提供一个误差范围,当在误差范围以内不等时通过测试;

完整测试代码:

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

@interface XCTestZcTests : XCTestCase
@property (nonatomic, strong) ViewController *vc;
@end

@implementation XCTestZcTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
    // 初始化代码 在测试方法之前调用
    _vc = [[ViewController alloc] init];
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    // 释放测试用例的资源,每个测试用例执行后调用
    self.vc = nil;
    [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.
    // 测试用例  (一定要test开头)
    int result = [self.vc number];
    XCTAssertEqual(result, 10,@"hahaha");
    XCTAssertTrue(result);
    
}

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

//- (void)testTear{
//    int result = [self.vc number];
//    XCTAssertFalse(result);
//}

- (void)testPerformaneHaha{
    [self measureBlock:^{
        NSLog(@"test");
    }];
}

@end

真正的大师,永远怀着一颗学徒的心——易

上一篇 下一篇

猜你喜欢

热点阅读