1. 程式人生 > >1分鐘入門介面自動化框架Karate

1分鐘入門介面自動化框架Karate

介紹

在這篇文章中,我們將介紹一下開源的Web-API自動化測試框架——Karate

Karate是基於另一個BDD測試框架Cucumber來建立的,並且共用了一些相同的思想。其中之一就是使用Gherkin檔案,該檔案描述了被測試的功能

與Cucumber不同的是測試用例不需要用Java編寫,並且被完整的描述在Gherkin檔案中

通過Karate,您可以編寫任何型別的Web服務端的測試指令碼,並檢查響應是否符合預期

Karate的驗證引擎可以靈活的比較兩個JSON或XML檔案內容,不受空格和資料順序的影響

有關Karate的更詳細的內容,請參考Karate官方介紹

特點

  1. 建立在Cucumber-JVM基礎上
  2. 可以像標準的Java工程一樣執行測試並且產生報告
  3. 測試程式碼的開發不需要掌握任何的Java知識
  4. 即使對非程式設計人員,測試程式碼也很容易編寫

環境需求

  1. JDK1.8及以上
  2. Maven
  3. IDEA

使用

建立工程

1.開啟IDEA,File|New|Project

2.選擇Maven工程,點選Next

3.輸入Maven基本資訊,點選Next

4.輸入工程名稱和存放路徑,點選Finish

新增依賴

要在Maven專案中使用Karate,需要將karate-apache依賴項新增到pom.xml,如果實現JUnit測試還需要新增karate-junit4依賴

<dependencies>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>0.8.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>0.8.0</version>
<scope>test</scope>
</dependency>

</dependencies>

設定測試資原始檔目錄,建議測試用例檔案和java檔案放在同一個目錄下,遇到龐大的工程的時候方便管理,不必在資料夾src/test/java和src/test/resources資料夾之間切換,可以在pom.xml的

<testResources>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>

服務端模擬

為了演示REST API,我們使用WireMock伺服器

在pom.xml中新增mock服務依賴配置

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.18.0</version>
<scope>test</scope>
</dependency>

編寫一個啟動服務的類

package server;

import com.github.tomakehurst.wiremock.WireMockServer;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

public class StartServer {

private static WireMockServer wireMockServer = new WireMockServer(8080);

public static void startServer(){
wireMockServer.start();

stubFor(
get(urlEqualTo("/user/get"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{ \"id\": \"1234\", name: \"John Smith\" }")));

stubFor(
post(urlEqualTo("/user/create"))
.withHeader("content-type", equalTo("application/json"))
.withRequestBody(containing("id"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{ \"id\": \"1234\", name: \"John Smith\" }")));

}

public static void main(String... args){
startServer();
}
}

用例檔案編寫

一個用例檔案以“ .feature”副檔名儲存。

檔案以Feature關鍵字開頭,在同一行跟著所測試的功能名稱

一個用例檔案包含不同的測試場景,每個場景都以關鍵字Scenario開頭,並且包含多個步驟。這些步驟包含關鍵字Given,When,Then,And和But

有關Cucumber和Gherkin結構的更多資訊,請點選此處

Feature: Learn How to use Karate for testing.

Scenario: Testing valid GET endpoint

Given url 'http://localhost:8080/user/get'
When method GET
Then status 200

Scenario: Testing the exact response of a GET endpoint

Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ == {id:"1234", name:"John Smith"}

Scenario: Testing that GET response contains specific field

Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ contains {id:"1234"}

我向大家推薦一個學習資料領取的qq群。這套視訊資料詳細講解了(自動化程式設計,mysql調優,自動化框架使用)。

對以上測試資料,測試技術 感興趣的朋友,歡迎加QQ群:175317069,一起學習,相互討論。

Runner類編寫

建議放在用例檔案同級目錄下

我們可以通過將Karate與JUnit整合來執行我們的測試

我們將使用@CucumberOptions註解指定Feature檔案的具體位置

package demo;

import com.intuit.karate.junit4.Karate;
import cucumber.api.CucumberOptions;
import org.junit.runner.RunWith;


@RunWith(Karate.class)
@CucumberOptions(features = "classpath:demo/demo.feature")

public class DemoRunner {

}

執行用例

1.先啟動服務

右擊StartServer類選擇Run StartServer.main()啟動服務

2.執行用例

右擊DemoRunner類選擇Run DemoRunner執行測試

檢視報告

在專案的target/surfire-reports目錄下有TEST-demo.demo.html檔案,瀏覽器中開啟即可看到結果

持續整合

可以藉助於jenkins完成自動化測試並且jenkins提供外掛cucumber-reports可以展示可讀性強的自動化測試報告

需要修改Runner繼承KarateRunner,先引入Karate-testng依賴

<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-testng</artifactId>
<version>0.8.0</version>
</dependency>

修改DemoRunner,注意配置CucumberOptions,要產生json格式的報告,cucumber-reports外掛會去解析該檔案並生成報告

package demo;

import com.intuit.karate.junit4.Karate;

import com.intuit.karate.testng.KarateRunner;
import cucumber.api.CucumberOptions;
import org.junit.runner.RunWith;

 

@CucumberOptions(features = "classpath:demo/demo.

feature",format={"pretty",

"html:reports","json:report.json"})

public class DemoRunner extends KarateRunner {

}

jenkins中cucumber-reports配置請參考網路資源

jenkins配置命令列執行指令

rm -rf ${WORKSPACE}/report.json
cd /home/pateo/IdeaProjects/demo4karate
mvn test -Dtest=DemoRunner
cp report.json ${WORKSPACE}/report.json

jenkins報告展示