在 Xcode 单元测试中测量预期时间

Measure expectation time in Xcode Unit Testing

我想计算在我的应用程序中调用通知需要多长时间。我有这样的测试设置:

func testLoadingPerformance() {
    self.measureBlock {
        self.expectationForNotification("Loaded", object: nil, handler: nil)
        self.waitForExpectationsWithTimeout(30, handler: nil)
        print("loaded")
    }
}

我已经设置了断点并且在测试期间肯定会调用通知,并且 waitForExpectationsWithTimeout 在 "loaded" 被打印时继续执行。

然而,测试从未在 Xcode 内完成,它一直运行到超时。虽然我知道该块肯定已执行,但为什么它不通过?

self.measureBlock里面的方法被调用了10次。 "measuring" 部分采用这 10 次运行的平均值来计算平均值。如果未收到 10 次已加载通知,那么您将停止。我希望那是你的问题。