使用XCTest进行异步性能测试

rob*_*mac 11 unit-testing asynchronous ios 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];
   }];
}];
Run Code Online (Sandbox Code Playgroud)

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

谢谢

rob*_*mac 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];
   }];
}];
Run Code Online (Sandbox Code Playgroud)