我想“访问”
XCTApplicationLaunchMetric
中报告的测量结果。为此,我创建了自己的类,它采用 XCTMetric
并在幕后使用、调用 XCTApplicationLaunchMetric
,如下所示:
class MyXCTApplicationLaunchMetric: NSObject, XCTMetric {
let myLaunchMetric: XCTApplicationLaunchMetric
init(waitUntilResponsive: Bool) {
print(#function)
myLaunchMetric = XCTApplicationLaunchMetric(waitUntilResponsive: waitUntilResponsive)
}
// XCTMetric conformance
func willBeginMeasuring() {
print(#function)
myLaunchMetric.willBeginMeasuring()
}
// XCTMetric conformance
func didStopMeasuring() {
print(#function)
myLaunchMetric.didStopMeasuring()
}
// XCTMetric conformance
func reportMeasurements(from startTime: XCTPerformanceMeasurementTimestamp, to endTime: XCTPerformanceMeasurementTimestamp) throws -> [XCTPerformanceMeasurement] {
let measurement = Measurement(value: 40, unit: .init(symbol: "M"))
let performanceMeasurement = XCTPerformanceMeasurement(identifier: "SomeIdentifier", displayName: "SomeDisplayName", value: measurement)
return [performanceMeasurement]
}
// NSObject conformance
func copy(with zone: NSZone? = nil) -> Any {
myLaunchMetric.copy(with: zone)
}
}
但是,当我在这里使用它时,没有调用任何
XCTMetric
函数。称为 init
。我不知道为什么。
final class PerformanceUITests: XCTestCase {
func testLaunchPerformance() throws {
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) {
let options = XCTMeasureOptions()
options.iterationCount = 1
let launchMetric = MyXCTApplicationLaunchMetric(waitUntilResponsive: true)
measure(metrics: [launchMetric],
options: options) {
XCUIApplication().launch()
}
}
}
}
原因是因为
measure
调用 copy(with:)
并且您不是返回自定义类型的实例,而是 myLaunchMetric
这是 XCTApplicationLaunchMetric
的实例。