REST APIs自動化測試

集成測試是軟件測試的一個階段,在軟件測試中,單獨軟件模塊做爲一組進行組合和測試,而不是單獨測試每一個類。這能夠經過使用用於後端代碼的JUnit和用於UI的Selenium來輕鬆實現。這兩個測試均可以做爲構建/CI系統的一部分,來查看報告和構建/CI系統失敗或經過。git

因爲咱們如今都在寫或維護RESTful微服務,這些服務或api都暴露在web上,還分佈在不一樣的網絡上,所以它們極易有風險和安全威脅,這些威脅會影響到基於它們的進程。所以爲了確保它們正確地執行,測試變成必要。爲了測試這些API,不依靠人工測試來自動化REST API測試用例是很是重要的。本文關注的是基本原則、機制和測試REST API的幾種方法。爲了簡單起見,這裏將使用GitHub REST API。github

能獲取的技術和工具備不少,其中包括Apache HTTP client,rest-assured,soapUI,Postman等等。我將介紹Apache HTTP client,rest-assured和soapUI。web

這類測試一般會在持續集成過程當中做爲較晚的步驟運行,在它以後運行的REST API已經部署完畢。json

當測試REST APIs時,咱們要關注如下幾點:後端

  • HTTP響應代碼
  • 響應主體 - JSON, XML
  • 在響應中的HTTP頭部

1.用Apache HTTP Client寫測試用例api

Http Client提供高效的、最新的和多功能的包,它實現了最新的HTTP標準和推薦的客戶端。安全

HTTP響應代碼網絡

public void validStatusCode() throws  IOException {

     HttpUriRequest request = new HttpGet( "https://api.github.com/events" );

     HttpResponse httpResponse = HttpClientBuilder.create().build().execute( request );     

     Assert.assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.OK));

}

響應主體和頭部app

public void responseBody() IOException {

    String jsonMimeType =  "application/json";

HttpUriRequest request = new HttpGet( "https://api.github.com/events" );

HttpResponse response = HttpClientBuilder.create().build().execute( request );

String mimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();

    Event[] events = new ObjectMapper().readValue(response.getEntity().

                     getContent(), Event[].class);       

 Assert.assertEquals( jsonMimeType, mimeType );

   // more assert starments can be added here

 }

@JsonIgnoreProperties(ignoreUnknown = true)// this is added since new ObjectMapper().readValue(response.getEntity().getContent(), Event[].class);

//throw will exception if don't have all properties(part of the response) present in this class

class Event {

    private String type;

    private long id;

    private Repo repo;

    // setters and getters for all properties goes here

}

@JsonIgnoreProperties(ignoreUnknown = true)

class Repo {

    private long id;

    private String name;

// setters and getters for all properties goes here

}

2.經過rest-assured寫測試用例maven

REST-assured是Java DSL(領域專用語言)用於簡化構建在HTTP Builder頂部基於服務的REST測試。它支持 POST,GET,PUT,DELETE,OPTIONS,PATCH和 HEAD 請求,並能夠用於確認和驗證這些請求的響應。

HTTP響應代碼、響應主體和頭部

@Test

public void getStatusWithRestAssured() {

  Event[] events =  RestAssured.get("https://api.github.com/events").then()

               .statusCode(200).assertThat().contentType(ContentType.JSON)

               .body("", CoreMatchers.notNullValue())

               .extract().as(Event[].class);

  // more assert statement goes here.

}

經過rest-assured,能夠經過簡單的方法覆蓋各類測試場景。更多關於rest-assured的細節能夠點擊閱讀原文。

3.經過SoapUI寫測試用例

SoapUI是一款開源、跨平臺的測試工具。它能夠自動化SOAP和REST web服務的功能、迴歸、依從性和負載測試。它提供了一個易用的圖形界面,並支持業界領先的技術和標準來模擬好激發web service的行爲。

下面是設置它所需的步驟。

  1. 建立soapUI測試項目。
  2. 定義端點。
  3. 測試用例和測試套件的建立。
  4. 爲端點添加測試步驟。
  5. 項目描述符生成。

一旦咱們完成上述步驟,經過在pom中增長插件,建立一個maven項目。下面的代碼假設項目描述符文件的名稱是project.xml。

<plugin>

<groupId>com.smartbear.soapui</groupId>

    <artifactId>soapui-maven-plugin</artifactId>

    <version>5.2.1</version>

    <configuration>

    <projectFile>${basedir}/project.xml</projectFile>

</configuration>

    <executions>

    <execution>

<id>soapui-test</id>

         <phase>integration-test</phase>

         <goals>

            <goal>test</goal>

         </goals>

    </execution>

</executions>

</plugin>

若是在默認的Maven repo下不可用,則須要添加如下資源庫:

<pluginRepositories>

<pluginRepository>

    <id>smartbear-sweden-plugin-repository</id>

    <url>http://www.soapui.org/repository/maven2</url>

  </pluginRepository>

</pluginRepositories>

運行接下來的Maven命令來運行你全部的測試:

mvn clean integration-test

mvn clean integration-test
相關文章
相關標籤/搜索