国内写Tests的很少,写UITests的更少了...或许只是我眼见不够开阔.网上那么几篇都是Swift的.这里给个简单的OC.
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; _txt = [[UITextField alloc] initWithFrame:CGRectMake(,, , )]; _txt.layer.borderWidth = ;
_txt.layer.borderColor = [UIColor blackColor].CGColor; [self.view addSubview:_txt]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(, , , )]; 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:];//我测试了下,这个INDEX是按上下排序的,最上面的控件为0 XCUIElementQuery *TextQuery = [[XCUIApplication alloc] init].textFields;
XCUIElement *textField = [TextQuery elementBoundByIndex:]; [btn tap]; XCTAssert([textField.value isEqualToString:@"ABC123"]); }
当然UItest还有个更屌的功能就是直接录制动作来产生测试代码.如下图.
红框录制按钮->绿框直接在模拟器里进行你需要测试的操作->紫框生成后的代码->完成后回到红框结束.
当然断言什么的,还是得自己写..