XCTestAPI文档
软件测试学习交流群:925205234XCTest准备工作对于新项目,在新建项目界面勾选上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 allocinit;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<NSString *,id> * _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 defaultCenterpostNotificationName:"监听通知的名称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);获得符合期望的通知时将被调用,满足条件为Yestypedef BOOL (XCKeyValueObservingExpectationHandler)(id observedObject, NSDictionary *change);当KVO监视的值反正改变是调用,满足条件为Yestypedef void (XCWaitCompletionHandler)(NSError *error);当测试成功或超时时调用,需要指定error类型,否则error = nil;property BOOL continueAfterFailure;默认为Yes,当case中某条测试语句失败时会继续向下执行,实测只向下执行了一步,待验证。- (void)measureBlock:(void ()(void)block;测试块中代码的性能。- (void)measureMetrics:(NSArray<NSString *> *)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<NSString *> *)defaultPerformanceMetrics;这是调用measureBlock时默认使用的测量标准数组。- (id<NSObject>)addUIInterruptionMonitorWithDescription:(NSString *)handlerDescription handler:(BOOL ()(XCUIElement *interruptingElement)handler;在当前上下文中添加一个HandlehandlerDescription:用于阐述这个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