电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

类型XCTestAPI文档

收藏

编号:344143295    类型:共享资源    大小:63.24KB    格式:DOCX    上传时间:2023-02-11
  
5
金贝
分享到微信 分享到微博 分享到QQ空间
关 键 词:
XCTestAPI 文档
资源描述:
软件测试学习交流群:925205234 XCTest 准备工作 对于新项目,在新建项目界面勾选上UI Tests; 对于旧项目,在项目界面点击菜单栏中的FileàNewàTarget…àiOSàTestàiOS UITesting Bundle。 sleepForTimeInterval: 线程休眠 [NSTread sleepForTimeInterval:1.0f]; 也可以使用sleep(3),OC兼容C语言。 定义测试用例 XCTestCase + (void)setUp; 在类中的第一个测试方法调用之前调用,区别于-(void)setUp:在每个测试方法调用之前都调用。 + (void)tearDown; 在类中的最后一个测试方法完成后调用。区别于-(void) tearDown:在每个测试方法调用后都调用。 异步测试表达式 - (XCTestExpectation *)expectationWithDescription:(NSString *)description; 指定时间内满足测试条件则测试通过,超时则输出description。 - (void)testAsynExample { XCTestExpectation *exp = [self expectationWithDescription:@"这里可以是操作出错的原因描述。。。"]; NSOperationQueue *queue = [[NSOperationQueue alloc]init]; [queue addOperationWithBlock:^{ //模拟这个异步操作需要2秒后才能获取结果,比如一个异步网络请求 sleep(2); //模拟获取的异步操作后,获取结果,判断异步方法的结果是否正确 XCTAssertEqual(@"a", @"a"); //如果断言没问题,就调用fulfill宣布测试满足 [exp fulfill]; }]; //设置延迟多少秒后,如果没有满足测试条件就报错 [self waitForExpectationsWithTimeout:3 handler:^(NSError * _Nullable error) { if (error) { NSLog(@"Timeout Error: %@", error); } }]; } - (XCTestExpectation *)expectationForPredicate:(NSPredicate *)predicate evaluatedWithObject:(id)object handler:(XCPredicateExpectationHandler)handler; 利用谓词计算,如果限定时间内满足条件则通过测试 - (void)testThatBackgroundImageChanges { XCTAssertNil([self.button backgroundImageForState:UIControlStateNormal]); NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(UIButton * _Nonnull button, NSDictionary * _Nullable bindings) { return [button backgroundImageForState:UIControlStateNormal] != nil; }]; [self expectationForPredicate:predicate evaluatedWithObject:self.button handler:nil]; [self waitForExpectationsWithTimeout:20 handler:nil]; } - (XCTestExpectation *)expectationForNotification:(NSString *)notificationName object:(id)objectToObserve handler:(XCNotificationExpectationHandler)handler; 监听一个通知,如果在规定时间内正确收到通知则测试通过。 - (void)testAsynExample1 { [self expectationForNotification:(@"监听通知的名称xxx") object:nil handler:nil]; [[NSNotificationCenter defaultCenter]postNotificationName:@"监听通知的名称xxx" object:nil]; //设置延迟多少秒后,如果没有满足测试条件就报错 [self waitForExpectationsWithTimeout:3 handler:nil]; } - (XCTestExpectation *)keyValueObservingExpectationForObject:(id)objectToObserve keyPath:(NSString *)keyPath expectedValue:(id)expectedValue; 创建一个KVO观察模式 - (XCTestExpectation *)keyValueObservingExpectationForObject:(id)objectToObserve keyPath:(NSString *)keyPath handler:(XCKeyValueObservingExpectationHandler)handler; 创建一个KVO观察模式 - (void)waitForExpectationsWithTimeout:(NSTimeInterval)timeout handler:(XCWaitCompletionHandler)handler; 设定等待时间,等待时间内满足所有条件则测试通过,成功或超时都会执行handler block(optional) typedef BOOL (^XCPredicateExpectationHandler)(void); 如果未提供Handle,第一次测试通过即满足条件,如果提供了Handle,它能覆盖原有的行为和条件,那么将重新判定是否满足条件。 typedef BOOL (^XCNotificationExpectationHandler)(NSNotification *notification); 获得符合期望的通知时将被调用,满足条件为Yes typedef BOOL (^XCKeyValueObservingExpectationHandler)(id observedObject, NSDictionary *change); 当KVO监视的值反正改变是调用,满足条件为Yes typedef void (^XCWaitCompletionHandler)(NSError *error); 当测试成功或超时时调用,需要指定error类型,否则error = nil; @property BOOL continueAfterFailure; 默认为Yes,当case中某条测试语句失败时会继续向下执行,实测只向下执行了一步,待验证。 - (void)measureBlock:(void (^)(void))block; 测试块中代码的性能。 - (void)measureMetrics:(NSArray *)metrics automaticallyStartMeasuring:(BOOL)automaticallyStartMeasuring forBlock:(void (^)(void))block; measureBlock的拓展版,当需要自定义测量的开始点和结束点时,又或者要测量多个指标时调用此方法。 Metrics:是测量标准数组;automaticallyStartMeasuring为真时,自动开始测试,为假则需要startMeasuring作为启动点。 注意在一个代码块中开始点和结束点只能各有一个,出现一下情况时测试将会失败: automaticallyStartMeasuring = YES且代码块中调用了startMeasuring方法; automaticalltStattMeasuring = NO 且代码块中没调用或多次调用了startMeasuring方法; 在代码块中多次调用了stopMeasuring方法。 - (void)startMeasuring; 在measureBlock中调用此方法来标记一个测量起点。 - (void)stopMeasuring; 在measureBlock中调用此方法来标记一个结束点。 + (NSArray *)defaultPerformanceMetrics; 这是调用measureBlock时默认使用的测量标准数组。 - (id)addUIInterruptionMonitorWithDescription:(NSString *)handlerDescription handler:(BOOL (^)(XCUIElement *interruptingElement))handler; 在当前上下文中添加一个Handle handlerDescription:用于阐述这个Handle的作用和行为,主要被用来Debug和分析 异步测试 XCTestExpectation 使用以下XCTestCase方法来创建XCTestExpectation实例: expectationWithDescription: expectationForPredicate:evaluatedWithObject:handler: expectationForNotification:object:handler: keyValueObservingExpectationForObject:keyPath:expectedValue: keyValueObservingExpectationForObject:keyPath:handler: - (void)fulfill; 为满足条件的表达式做标记 布尔值检测 XCTAssert / XCTAssertTrue 断言表达式为真,XCTAssert(expression, format...)当expression求值为TRUE时通过;  XCTAssert([image exists]); XCTAssertTrue(expression, format...)当expression求值为TRUE时通过;  XCTAssertTure([image exists]); XCTAssertFalse 表达式为假,XCTAssertFalse(expression, format...)当expression求值为False时通过;  XCTAssertFalse(![image exists]); 空值检测 XCTAssertNil 表达式的值为空,XCTAssertNil(a1, fo
展开阅读全文
提示  金锄头文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:XCTestAPI文档
链接地址:https://www.jinchutou.com/shtml/view-344143295.html
关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.