2017-07-10 34 views
0

我正在爲我的iOS swift項目實施單元測試。我想爲我的項目中的POST API實現性能測試,但我不知道如何實現該POST API。我測試了GET API,並且給了我期望的結果。POST API的XCTest性能測試

這裏就是我想實現

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則需要幾秒鐘的時間。 請檢查並幫我解決這個問題。

+0

是否檢查完成塊是否被執行? @vivek – jegadeesh

+0

@jegadeesh我把調試點,但它永遠不會在完成塊。 –

+0

那就是問題所在。你確定你的請求正在服務器上嗎? @Vivek – jegadeesh

回答

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

    } 
} 

更改超時值較高的一個,如果你仍然沒有得到任何迴應。

+0

我試過這個解決方案並且完成塊的叫了兩聲,但我越來越喜歡 1錯誤)失敗:抓住「NSInternalInconsistencyException」,「API違規 - 調用必須等待,沒有任何預期已經設置錯誤 2)斷言失敗 - [XCTestExpectation履行] 3 )終止由於未捕獲異常'NSInternalInconsistencyException'的應用程序,原因:'API違反 - 對POST進行多次調用 - [XCTestExpectation履行] 我也嘗試過更高的超時但錯誤保持不變 –

+0

性能測試將多次執行測量模塊。嘗試定義測量塊內的期望,並按照此鏈接瞭解更多信息:https://stackoverflow.com/questions/25145468/multiple-testperformance-with-xctestcase-objective-c – jegadeesh

+0

我試圖在度量塊內定義期望值,作品完美,謝謝:-) –

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