单元测试

177 阅读1分钟

逻辑测试

创建测试类文件,继承XCTestCase

  • setUp
    每个类中 测试方法调用前 先调用这个方法 以方便 开发者 做些 测试前的准备
    • tearDown
      当这个 类中的 所有的 测试 方法 测试完后 会调用这个方法

-(void)testExample(){} 点击测试方法左侧的菱形按钮开始单独测试这一个方法。

NSAssert()

性能测试

- (**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)testAsyncMethod {
    // 定义一个预期
    XCTestExpectation *exp = [self expectationWithDescription:@"Person异步方法的期望"];
    [Person asyncMethodWithCompletion:^{
        // 异步结束,标注期望达成
        [exp fulfill];
    }];
    // 等待期望异步执行完,若在2秒内完成,则通过,否则不通过
    [self waitForExpectationsWithTimeout:2 handler:^(NSError * _Nullable error) {
        // 期望回调,根据error是否为空可知是否通过
    }];
}

ui测试:

- (void)testViewControllerInputUI {
    
    
    XCUIApplication *app = [[XCUIApplication alloc] init];
    
    XCUIElement *tf1 = [[[[[app.windows childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeTextField] elementBoundByIndex:0];
    [tf1 tap];
    [tf1 typeText:@"abcdefg"];
    
    XCUIElement *tf2 = [[[[[app.windows childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeTextField] elementBoundByIndex:1];
    [tf2 tap];
    [tf2 typeText:@"12345"];
    
    XCUIElement *deleteKey = app.keys[@"delete"];
    [deleteKey tap];
    [deleteKey tap];
    
    XCUIElement *button = [[[[app.windows childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeButton].element;
    [button tap];
}

单侧指标

覆盖率:

Test - Options - Code Coverage

在 Report Navigator - Coverage

参考: mp.weixin.qq.com/s/dSzq90Xvn… mp.weixin.qq.com/s/-xhcU5jeP…