iOS开发ios实用技术

iOS单元测试

2020-08-07  本文已影响0人  沉江小鱼

1.介绍

在讲XCTest之前我们先来了解一下单元测试。单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证,通过开发者编写代码去验证被测代码是否正确的一种手段,例如编写一个测试函数去测试某一功能函数是否能正确执行达到预期效果。在实际项目开发中使用单元测试可以提高软件的质量,也可以尽量早的发现代码中存在的问题加以修正。

2. 简单使用

XCTest是Xcode自带的单元测试框架,我们可以使用该框架做功能性代码的白盒单元测试,以自测并增强代码健壮性。

2.1 项目中添加XCTest
2.1.1 创建项目时勾选该选项
2.1.2 项目创建后添加
2.2 方法简单介绍

现在只有一个.m文件,里面有4个方法:

// 在每一个测试方法调用前,都会被调用
// 用来初始化 test 用例的一些初始值
- (void)setUp {
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

// 在每一个测试方法调用后,都会被调用
// 用来重置 test 方法的数值
- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
}

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

在编写测试代码时,需要知道以下几点:

2.3 简单使用
// Student.h 文件
@interface Student : NSObject

- (NSInteger)studyAddA:(NSInteger)a b:(NSInteger)b;

- (NSInteger)studyDeleteA:(NSInteger)a b:(NSInteger)b;

@end

// Student.m 文件
#import "Student.h"

@implementation Student

- (NSInteger)studyAddA:(NSInteger)a b:(NSInteger)b{
    NSInteger result = a + b;
    return result;
}

- (NSInteger)studyDeleteA:(NSInteger)a b:(NSInteger)b{
    NSInteger result = a - b;
    return result;
}

@end

#import "Student.h"

@interface StudentTests : XCTestCase

@property (nonatomic, strong) Student *student;

@end

@implementation StudentTests

- (void)setUp {
    self.student = [Student new];
}

- (void)tearDown {
    self.student = nil;
}

- (void)testStudentAdd {
    NSInteger result = [self.student studyAddA:2 b:3];
    XCTAssert(result == 5, @"结果计算出错");
}

@end

代码编辑器边栏菱形按钮,测试单个用例
Test 导航栏,测试单个用例
快捷键⌘ + U测试全部用例
使用命令行工具 xcodebuild 可以测试单个用例,也可以测试全部用例。

image.png image.png

勾选Gather coverage for:

image.png

然后重新,运行测试用例,观察结果:


image.png

3. 如何进行性能测试

性能测试通过度量代码块执行所消耗的时间长短,来衡量是否通过测试。
性能测试会运行想要评估的代码块十次,收集平均执行时间和运行的标准偏差。然后平均值与baseLine进行比较以评估成功或失败。

baseLine是我们指定的用来评估测试通过或者失败的值。我们也可以自己指定一个特定的值。

截屏2020-08-07 下午4.45.20.png

我们可以通过点击measureBlock:方法左边菱形圆心 icon ,来设置Baseline,设置之后需要点击save保存。之后再执行测试用例时,如果成功,左边的icon会从圆心变成一个 ✅。

3.1 如何进行性能测试

相关 API :

- (void)testPerformanceOfMyFunction {

    [self measureBlock:^{
        // Do that thing you want to measure.
        MyFunction();
    }];
}
- (void)testMyFunction2_WallClockTime {
    [self measureMetrics:[self class].defaultPerformanceMetrics automaticallyStartMeasuring:NO forBlock:^{

        // Do setup work that needs to be done for every iteration but you don't want to measure before the call to -startMeasuring
        SetupSomething();
        [self startMeasuring];

        // Do that thing you want to measure.
        MyFunction();
        [self stopMeasuring];

        // Do teardown work that needs to be done for every iteration but you don't want to measure after the call to -stopMeasuring
        TeardownSomething();
    }];
}

4. 异步测试

什么时候需要使用异步测试:

  1. 打开文档
  2. 在后台线程中执行的服务和网络活动
  3. 执行动画
  4. UI 测试时
4.1 异步测试XCTestExpectation

异步测试分为3个部分: 新建期望等待期望被履行履行期望

// 测试类持有的初始化方法
XCTestExpectation *expect1 = [self expectationWithDescription:@"asyncTest1"];

// 自己持有的初始化方法
XCTestExpectation *expect2 = [[XCTestExpectation alloc] initWithDescription:@"asyncTest3"];
// 测试类持有时的等待方法
[self waitForExpectationsWithTimeout:10.0 handler:nil];

// 自己持有时的等待方法
[self waitForExpectations:@[expect3] timeout:10.0];
XCTestExpectation *expect3 = [[XCTestExpectation alloc] initWithDescription:@"asyncTest3"];

[TTFakeNetworkingInstance requestWithService:apiRecordList completionHandler:^(NSDictionary *response) {
    XCTAssertTrue([response[@"code"] isEqualToString:@"200"]);
    [expect3 fulfill];
}];

[self waitForExpectations:@[expect3] timeout:10.0];
4.2 异步测试XCTWaiter

XCTWaiter是 2017 年新增的异步测试方案,可以通过代理方式来处理异常情况。

XCTWaiter *waiter = [[XCTWaiter alloc] initWithDelegate:self];
    
XCTestExpectation *expect4 = [[XCTestExpectation alloc] initWithDescription:@"asyncTest3"];
    
[TTFakeNetworkingInstance requestWithService:@"product.list" completionHandler:^(NSDictionary *response) {
    XCTAssertTrue([response[@"code"] isEqualToString:@"200"]);
    expect4 fulfill];
}];

XCTWaiterResult result = [waiter waitForExpectations:@[expect4] timeout:10 enforceOrder:NO];

XCTAssert(result == XCTWaiterResultCompleted, @"failure: %ld", result);

XCTWaiterDelegate:如果委托是XCTestCase实例,下方代理被调用时会报告为测试失败。

// 如果有期望超时,则调用。 
- (void)waiter:(XCTWaiter *)waiter didTimeoutWithUnfulfilledExpectations:(NSArray<XCTestExpectation *> *)unfulfilledExpectations;

// 当履行的期望被强制要求按顺序履行,但期望以错误的顺序被履行,则调用。
- (void)waiter:(XCTWaiter *)waiter fulfillmentDidViolateOrderingConstraintsForExpectation:(XCTestExpectation *)expectation requiredExpectation:(XCTestExpectation *)requiredExpectation;

// 当某个期望被标记为被倒置,则调用。 
- (void)waiter:(XCTWaiter *)waiter didFulfillInvertedExpectation:(XCTestExpectation *)expectation;

// 当 waiter 在 fullfill 和超时之前被打断,则调用。 
- (void)nestedWaiter:(XCTWaiter *)waiter wasInterruptedByTimedOutWaiter:(XCTWaiter *)outerWaiter;

5. 断言记录

在写测试用例的时候,我们可以使用断言,下面是记录一下:

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语言标量、结构体或联合体时使用,实际测试发现NSString也可以); 
 
XCTAssertNotEqual(a1, a2, format...)判断不等(当a1和a2是 C语言标量、结构体或联合体时使用);
 
XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...)判断相等,(double或float类型)提供一个误差范围,当在误差范围(+/-accuracy)以内相等时通过测试; 
 
XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判断不等,(double或float类型)提供一个误差范围,当在误差范围以内不等时通过测试; 
 
XCTAssertThrows(expression, format...)异常测试,当expression发生异常时通过;反之不通过;(很变态) XCTAssertThrowsSpecific(expression, specificException, format...) 异常测试,当expression发生specificException异常时通过;反之发生其他异常或不发生异常均不通过; 
 
XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...)异常测试,当expression发生具体异常、具体异常名称的异常时通过测试,反之不通过; 
 
XCTAssertNoThrow(expression, format…)异常测试,当expression没有发生异常时通过测试;
 
XCTAssertNoThrowSpecific(expression, specificException, format...)异常测试,当expression没有发生具体异常、具体异常名称的异常时通过测试,反之不通过; 
 
XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...)异常测试,当expression没有发生具体异常、具体异常名称的异常时通过测试,反之不通过
 
 
 
特别注意下XCTAssertEqualObjects和XCTAssertEqual。
 
XCTAssertEqualObjects(a1, a2, format...)的判断条件是[a1 isEqual:a2]是否返回一个YES。
 
XCTAssertEqual(a1, a2, format...)的判断条件是a1 == a2是否返回一个YES。
 
对于后者,如果a1和a2都是基本数据类型变量,那么只有a1 == a2才会返回YES。例如

合理使用测试基类和测试工具类,可以避免大量重复测试代码。时间转换工具类是一个没有外部依赖的类,当一些对外部有依赖的类需要测试时,可以尝试 OCMock ,它能帮助你模拟数据。另外,当你觉得测试框架提供的断言方法无法满足你时,也可以试着使用 OCHamcrest

6. 未完待续

简单的记录一下,还有很多等待发现。。。

7. 参考

iOS开发之XCTest
官方文档翻译
在XCode中使用XCTest
iOS 单元测试和 UI 测试快速入门
官方文档
XCTest 测试实战
OCMock翻译

上一篇 下一篇

猜你喜欢

热点阅读