介紹
在這篇文章中,我們將介紹一下開源的Web-API自動化測試框架——Karate
Karate是基於另一個BDD測試框架Cucumber來建立的,並且共用了一些相同的思想。其中之一就是使用Gherkin文件,該文件描述了被測試的功能
與Cucumber不同的是測試用例不需要用Java編寫,並且被完整的描述在Gherkin文件中
通過Karate,您可以編寫任何類型的Web服務端的測試腳本,並檢查響應是否符合預期
Karate的驗證引擎可以靈活的比較兩個JSON或XML文件內容,不受空格和數據順序的影響
有關Karate的更詳細的內容,請參考Karate官方介紹
特點
- 建立在Cucumber-JVM基礎上
- 可以像標准的Java工程一樣運行測試並且產生報告
- 測試代碼的開發不需要掌握任何的Java知識
- 即使對非編程人員,測試代碼也很容易編寫
環境需求
- JDK1.8及以上
- Maven
- IDEA
使用
創建工程
- 打開IDEA,File|New|Project
- 選擇Maven工程,點擊Next
- 輸入Maven基本信息,點擊Next
- 輸入工程名稱和存放路徑,點擊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"}
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 {
}
運行用例
-
先啟動服務
右擊StartServer類選擇Run StartServer.main()啟動服務 -
運行用例
右擊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