iOS的单元测试

原创文章
声明:作者声明此文章为原创,未经作者同意,请勿转载,若转载,务必注明本站出处,本平台保留追究侵权法律责任的权利。
全栈老韩
全栈工程师,擅长iOS App开发、前端(vue、react、nuxt、小程序&Taro)开发、Flutter、React Native、后端(midwayjs、golang、express、koa)开发、docker容器、seo优化等。

单元测试

  • 逻辑测试
  • 性能测试
  • 异步测试
  • UI测试
  • 自动化测试
    驱动:TDD测试驱动代码

Example 1 Normal Test

复制代码
- (void)setUp {
    // Put setup code here. This method is called before the invocation of each test method in the class.
    
    self.vc = [[ViewController alloc] init];
}

- (void)testPlus {
    int num1 = 10;
    int num2 = 20;
    int plus = [self.vc getPlus:num1 num2:num2];
    XCTAssertEqual(plus, 20, @"test failed");
}

Example 2 Async Tes

复制代码
- (void)loadData:(void (^)(id))dataBlock {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [NSThread sleepForTimeInterval:2];
        NSString *dataStr = @"异步";
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Refresh UI");
            dataBlock(dataStr);
        });
    });
    
}

- (void)testAsyExample {
    // 时间 + 数据
    XCTestExpectation *exp = [self expectationWithDescription:@"如果不在我预期"];
    
    [self.vc loadData:^(id data) {
        XCTAssertNotNil(data, @"为nil");
        [exp fulfill];
    }];
    [self waitForExpectationsWithTimeout:1.5 handler:^(NSError * _Nullable error) {
        NSLog(@"%@", error);
    }];
}

Example 3 Performance 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.
        
        /**
         性能测试
         参照物
         统计学
         baseline
         */
        [self.vc openCamera];
    }];
}

暂无评论,快来发表第一条评论吧