Karate由Intuit做爲開源工具發佈。該工具旨在用於自動API測試,並具備使API測試變得垂手可得且實際上使人愉快的全部必需功能。java
與須要大量編碼的其餘自動化API測試工具不一樣,即便只是作基本的東西,Karate開箱即用。您能夠在不瞭解任何編程語言的狀況下構建最複雜的請求 - 響應操做。您所要作的就是使用純文本Gherkin樣式編寫要素文件。git
由於Karate是一個完整的DSL而且位於Cucumber-JVM之上 ,因此你能夠像任何標準的Java項目同樣運行測試並生成報告,可是你不是編寫Java代碼,而是用一種用來處理HTTP,JSON的語言來編寫測試。或XML簡單易用。github
雖然沒有使用Karate的先決條件,但若是您對HTTP,JSON,XML,JsonPath以及XPath和JavaScript有基本的瞭解,它會有所幫助。面試
下面,咱們將介紹一些您一般在自動API測試中執行的典型操做,但首先是關於爲Karate設置環境的快速指南。apache
若是您使用的是Maven,則須要如下兩個依賴項編程
<dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-apache</artifactId> <version>0.6.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit4</artifactId> <version>0.6.0</version> <scope>test</scope> </dependency>
或者,若是您使用的是Gradle,則須要api
testCompile 'com.intuit.karate:karate-junit4:0.6.0' testCompile 'com.intuit.karate:karate-apache:0.6.0'
Karate測試腳本具備文件擴展名 .feature
,後面是Cucumber的標準。您可使用常規Java包約定來自由組織文件。jvm
Maven的傳統是將非Java源文件放在一個單獨的 src/test/resources
文件夾結構中 - 可是Karate工具的建立者建議您將它們與*.java
文件並排放置 。maven
像Cucumber同樣,你須要一個運行特徵文件的「Runner」類。然而,與Cucumber不一樣,沒有步驟定義!這就是Karate的魔力。
要使用TestRunner.java類來執行要素文件,您須要在pom.xml文件中包含構建部分。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Tutorials</groupId> <artifactId>Karate</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.intuit.karate</groupId> 若是對軟件測試、接口測試、自動化測試、性能測試、LR腳本開發、 <artifactId>karate-apache</artifactId> 面試經驗交流。感興趣能夠175317069,羣內會有不按期的發放免費的 <version>0.6.0.4</version> 資料連接,這些資料都是從各個技術網站蒐集、整理出來的,若是你有 </dependency> 好的學習資料能夠私聊發我,我會註明出處以後分享給你們。 <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit4</artifactId> <version>0.6.0.4</version> </dependency> </dependencies> <build> <testResources> <testResource> <directory>src/test/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> </testResources> </build> </project>
你的TestRunner.java類看起來像
package com.tutorials.karate; import com.intuit.karate.junit4.Karate; import org.junit.runner.RunWith; @RunWith(Karate.class) public class TestRunner { }
假設您正在測試API(https://some-api.com/api/users),它返回JSON格式的用戶列表
[ { "id": 1, "name": "FirstUser", "password": "User1Pass" }, { "id": 2, "name": "SecondUser", "password": "User2Pass" } ]
您的Karate功能文件以下所示:
Feature: Test User API Scenario: Fetch all users Given url 'https://some-api.com/api/users' When method GET Then status 200 And assert response.length == 2 And match response[0].name == 'FirstUser'
就是這樣 - 很是簡潔,重要的是,沒有代碼!
Karate具備很是豐富的實用功能,使您可以很是輕鬆快速地執行自動API測試。在後面,咱們將深刻研究這個神奇的工具,並舉例說明在測試API時如何作任何你須要的東西!