【问题标题】:XCTest performance testing of POST APIXCTest POST API 性能测试
【发布时间】:2017-07-10 06:19:36
【问题描述】:

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

【问题讨论】:

  • 你检查完成块是否被执行了吗? @vivek
  • @jegadeesh 我放了调试点,但它从来没有出现在完成块中。
  • 这就是问题所在。您确定您的请求正在访问服务器吗? @Vivek
  • 删除 self.measure 代码并直接恢复任务。不知道self.measure里面的代码会不会被执行。
  • @jegadeesh 在这种情况下它将执行完成块,但它会被视为功能测试而不是性能测试

标签: ios swift performance-testing xctest xctestcase


【解决方案1】:

尝试如下更改你的代码

 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)")
            }
        }

    }
}

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

【讨论】:

  • 我已经尝试过这个解决方案,并且完成块被调用了两次,但我得到了类似 1) 的错误:捕获“NSInternalInconsistencyException”、“API 违规 - 调用等待而没有任何期望已设置错误。2 ) -[XCTestExpectation 实现] 中的断言失败 3) 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“API 违规 - 多次调用 -[XCTestExpectation 实现] 以进行 POST。我也尝试过更高的超时,但错误仍然是一样
  • 性能测试将多次执行测量块。尝试在 measure 块中定义期望值并点击此链接了解更多信息:stackoverflow.com/questions/25145468/…
  • 我试图在 measure 块内定义期望并且工作完美,谢谢 :-)
  • 漫长的一天不是吗..? :)
  • 是的,需要这么多小时
【解决方案2】:

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

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)")
                }
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2014-08-30
    • 2018-04-15
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多