使用XCTest进行异步性能测试

时间:2014-07-09 13:16:46

标签: ios unit-testing asynchronous xctest

我已经开始探索用于异步和性能测试的新XCTest API。在隔离的情况下,WWMC中的Apple示例运行良好,但我一直无法弄清楚如何将它们组合起来。我能够提出的最好的是以下内容,但运行时收到以下错误:

API违规 - 调用等待没有任何期望已经设置。

XCTestExpectation *clsQueryReturnedExpectation = [self expectationWithDescription:@"clsQuery returned"];

PFCLSClient *theClient = [[PFCLSClient alloc] init];

[self measureMetrics:@[XCTPerformanceMetric_WallClockTime] automaticallyStartMeasuring:YES forBlock: ^{
   [theClient getStructureOfType:clsImageTypeSVG ForID:idString success: ^(NSDictionary *structureInfo) {
       [clsQueryReturnedExpectation fulfill];
} failure: ^(NSError *error) {
       XCTFail();
       [clsQueryReturnedExpectation fulfill];
}];

   [self waitForExpectationsWithTimeout:5 handler: ^(NSError *error) {
        [self stopMeasuring];
   }];
}];

有没有人能够完成类似的事情?

Thx

1 个答案:

答案 0 :(得分:21)

在Apple的帮助下,我有一个解决方案。对我来说是愚蠢的疏忽,因为这很容易解决。要开始工作,您需要做的就是在measureMetrics块中创建期望对象(clsQueryReturnedExpectation),以便每次运行性能测试时重新创建它。

PFCLSClient *theClient = [[PFCLSClient alloc] init];

[self measureMetrics:@[XCTPerformanceMetric_WallClockTime] automaticallyStartMeasuring:YES forBlock: ^{
    XCTestExpectation *clsQueryReturnedExpectation = [self expectationWithDescription:@"clsQuery returned"];  
    [theClient getStructureOfType:clsImageTypeSVG ForID:idString success: ^(NSDictionary *structureInfo) {
       [clsQueryReturnedExpectation fulfill];
    } failure: ^(NSError *error) {
       XCTFail();
       [clsQueryReturnedExpectation fulfill];
    }];

   [self waitForExpectationsWithTimeout:5 handler: ^(NSError *error) {
        [self stopMeasuring];
   }];
}];