【问题标题】:Asynchronous Performance Tests with XCTest使用 XCTest 进行异步性能测试
【发布时间】:2014-08-30 13:04:07
【问题描述】:

我已经开始探索用于异步和性能测试的新 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];
   }];
}];

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

谢谢

【问题讨论】:

    标签: ios unit-testing asynchronous xctest


    【解决方案1】:

    在 Apple 的帮助下,我找到了解决方案。我的愚蠢疏忽,因为这很容易解决。要开始工作,您需要做的就是将期望对象 (clsQueryReturnedExpectation) 的创建放在 measureMetrics 块中,这样每次运行性能测试时都会重新创建它。

    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];
       }];
    }];
    

    【讨论】:

    • 除了时间还有什么可以衡量的吗? API 参考似乎说它需要一个 XCTPerformanceMetrics 数组,但我无法在任何地方找到其他任何东西来衡量。
    • 据我所知,不,你不能。我读过同样的东西,但认为苹果目前还没有实现任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2021-11-16
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 2021-04-12
    相关资源
    最近更新 更多