1. 程式人生 > >使用Karate輕鬆實現自動API測試

使用Karate輕鬆實現自動API測試

如果您想做自動API測試,但沒有程式設計背景,那麼你必須要給Karate一個機會!

Karate由Intuit作為開源工具釋出。該工具旨在用於自動API測試,並具有使API測試變得輕而易舉且實際上令人愉快的所有必需功能。

與需要大量編碼的其他自動化API測試工具不同,即使只是做基本的東西,Karate開箱即用。您可以在不瞭解任何程式語言的情況下構建最複雜的請求 - 響應操作。您所要做的就是使用純文字Gherkin樣式編寫要素檔案。

因為Karate是一個完整的DSL並且位於Cucumber-JVM之上 ,所以你可以像任何標準的Java專案一樣執行測試並生成報告,但是你不是編寫Java程式碼,而是用一種用來處理HTTP,JSON的語言來編寫測試。或XML簡單易用。

雖然沒有使用Karate的先決條件,但如果您對HTTP,JSON,XML,JsonPath以及XPath和JavaScript有基本的瞭解,它會有所幫助。

下面,我們將介紹一些您通常在自動API測試中執行的典型操作,但首先是關於為Karate設定環境的快速指南。

Maven的

如果您使用的是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

或者,如果您使用的是Gradle,則需要

testCompile 'com.intuit.karate:karate-junit4:0.6.0'
testCompile 'com.intuit.karate:karate-apache:0.6.0'

資料夾結構

Karate測試指令碼具有副檔名 .feature ,後面是Cucumber的標準。您可以使用常規Java包約定來自由組織檔案。

Maven的傳統是將非Java原始檔放在一個單獨的 src/test/resources 資料夾結構中 - 但是Karate工具的建立者建議您將它們與*.java

 檔案並排放置 。

 

Karate Api測試工具 - 資料夾結構

Karate Api測試工具 - 資料夾結構

 

像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 {

}

使用Karate進行簡單的自動API測試

假設您正在測試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時如何做任何你需要的東西!