我正在为我的 iOS swift 项目实施单元测试。我想为项目中的 POST API 实现性能测试,但我不知道如何实现该 POST API。我已经测试了 GET API,这给了我想要的结果。
这是我要实现的代码
func testPerfromancePOSTAPI() {
let session = URLSession.shared
var request = URLRequest(url: MY_URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
do {
request.httpBody = try JSONSerialization.data(withJSONObject: MY_SERVICE_PARA, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
self.measure {
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
do {
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print("response JSON = \(json)")
}
} catch let error {
XCTFail("Expectation Failed with error: %@", file: error as! StaticString);
}
})
task.resume()
}
}
当我运行此测试时,它成功通过但不打印响应 JSON,也没有给出错误并显示“时间 0.0000 秒(161% STDEV)”,而在邮递员中运行相同的 API 需要几秒钟。请检查并帮助我解决这个问题。