日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

1分钟入门接口自动化框架Karate

發布時間:2023/12/13 综合教程 23 生活家
生活随笔 收集整理的這篇文章主要介紹了 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報告展示

總結

以上是生活随笔為你收集整理的1分钟入门接口自动化框架Karate的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。