0

我正在为我的 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 需要几秒钟。请检查并帮助我解决这个问题。

4

2 回答 2

0

在测量块内定义期望将有助于测量性能。

func testPostAPIFetchParseTime() {
        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 expectations = self.expectation(description: "POST API performance check")
            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)")
                        expectations.fulfill()
                    }
                } catch let error {
                    XCTFail("Expectation Failed with error: %@", file: error as! StaticString);
                }
            })
            task.resume()
            self.waitForExpectations(timeout: 10.0) { (error) in
                if let error = error {
                    XCTFail("Error: \(error.localizedDescription)")
                }
            }
        }
    }
于 2017-07-10T13:17:18.663 回答
0

尝试更改您的代码如下

 func testPerfromancePOSTAPI() {
    let session = URLSession.shared
    var request = URLRequest(url: MY_URL)
    let expectations = expectation(description: "POST")
    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)")         expectations.fulfill()
                }
            } catch let error {
                XCTFail("Expectation Failed with error: %@", file: error as! StaticString);
            }
        })
        task.resume()
        self.waitForExpectations(timeout: 10) { (error) in
            if let error = error {
                XCTFail("Error: \(error.localizedDescription)")
            }
        }

    }
}

如果您仍然没有得到任何响应,请将超时值更改为更高的值。

于 2017-07-10T09:32:14.007 回答