Xcode的一个简单的UITests

国内写Tests的很少,写UITests的更少了...或许只是我眼见不够开阔.网上那么几篇都是Swift的.这里给个简单的OC.

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _txt = [[UITextField alloc] initWithFrame:CGRectMake(20,20, 200, 30)];
    
    _txt.layer.borderWidth = 1;
    _txt.layer.borderColor = [UIColor blackColor].CGColor;
    
    [self.view addSubview:_txt];
    
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(230, 20, 100, 30)];
    
    btn.backgroundColor = [UIColor redColor];

    [self.view addSubview:btn];
    
    
}

-(void)btnClick
{
    _txt.text = @"ABC123";
}

上面一段代码其实有个地方是错的,刚好可以试试下面这个测试,bug找出来.

- (void)testExample {

    
    
    XCUIApplication *apps = [[XCUIApplication alloc] init];
    
    [apps launch];
    
    XCUIElementQuery *BtnQuery = [[XCUIApplication alloc] init].buttons;
    XCUIElement *btn = [BtnQuery elementBoundByIndex:0];//我测试了下,这个INDEX是按上下排序的,最上面的控件为0
    
    XCUIElementQuery *TextQuery = [[XCUIApplication alloc] init].textFields;
    XCUIElement *textField = [TextQuery elementBoundByIndex:0];

    [btn tap];
    
    XCTAssert([textField.value isEqualToString:@"ABC123"]);
    

}

 

当然UItest还有个更屌的功能就是直接录制动作来产生测试代码.如下图.

 

红框录制按钮->绿框直接在模拟器里进行你需要测试的操作->紫框生成后的代码->完成后回到红框结束.

当然断言什么的,还是得自己写..

posted @ 2016-03-05 16:02  NGI.  阅读(379)  评论(0编辑  收藏  举报
GitHub | M1989