Succulent
Succulent 是一个 Swift 库,为 iOS 上自动测试提供了 API 记录和回放功能。
Succulent 会创建一个本地 Web 服务器,您可以将您的应用程序指向该服务器,而不是使用实时 API。在记录模式下,Succulent 从应用程序接收 API 请求,然后将其发送给实时 API(而不是实际的应用),记录请求和响应,以便将来回放。
Succulent 也可以处理像 POST、PUT 和 DELETE 这样的突变请求:在突变请求之后,Succulent 会存储后续任何响应的新版本,然后在回放时正确地模拟这种变化。
为什么?
Succulent 解决了获取可重复 API 结果的问题,从而支持稳定的自动化测试。
示例
在 XCTest 的 setUp
方法中设置 Succulent
override func setUp() {
super.setUp()
if let traceUrl = self.traceUrl {
// Replay using an existing trace file
succulent = Succulent(replayFrom: traceUrl)
} else {
// Record to a new trace file
succulent = Succulent(recordTo: self.recordUrl, baseUrl: liveApiBaseUrl)
}
succulent.start()
launchEnvironment["succulentBaseURL"] = "https://:\(succulent.actualPort)/"
}
/// The name of the trace file for the current test
private var traceName: String {
return self.description.trimmingCharacters(in: CharacterSet(charactersIn: "-[] ")).replacingOccurrences(of: " ", with: "_")
}
/// The URL to the trace file for the current test when running tests
private var traceUrl: URL? {
let bundle = Bundle(for: type(of: self))
return bundle.url(forResource: self.traceName, withExtension: "trace", subdirectory: "Succulent")
}
/// The URL to the trace file for the current test when recording
private var recordUrl: URL {
let bundle = Bundle(for: type(of: self))
let recordPath = bundle.infoDictionary!["TraceRecordPath"] as! String
return URL(fileURLWithPath: "\(recordPath)/\(self.traceName).trace")
}
请注意,recordUrl
使用的是需要在您的 Info.plist
文件中设置的字符串
<key>TraceRecordPath</key>
<string>$(PROJECT_DIR)/Succulent/</string>
使用此设置,Succulent 将将跟踪文件记录到您的项目源树中。因此,Succulent 跟踪文件与您的测试文件一起提交到源控制中,当您构建和运行测试时,这些跟踪文件将复制到测试应用程序中。
最后,在您的应用程序中,查找 "succulentBaseURL"
环境变量,并使用该 URL 替换实时 API URL
let apiBaseUrl = ProcessInfo.processInfo.environment["succulentBaseURL"]
示例项目中有一个示例项目,位于Example
目录下。要运行示例项目,请在示例目录下执行pod install
命令,然后打开Xcode工作空间并运行测试。该示例项目展示了Succulent在独立环境下的应用,而不是像预期的那样用于另一个应用的UI自动化测试。
需求
安装
Succulent可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中。
pod "Succulent"
作者
许可证
Succulent遵循MIT许可证。更多信息请参阅LICENSE文件。