使用Karate框架进行API测试

1.设置环境

打开IDEA,新建maven工程

删除自动生成的代码,然后在main和test目录新建resources目录

打开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>org.example</groupId>
    <artifactId>KarateTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>KarateTest</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>17</maven.compiler.release>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-junit5</artifactId>
            <version>1.4.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-apache</artifactId>
            <version>0.9.6</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.4.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.3.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.13.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.3.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.4.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>3.1.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>3.1.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.12.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.6.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

安装karate扩展

打开Settings—>Plugins搜索karate

2.编写代码

在src/test/resources目录新建karate-config.js文件

karate-config.js

function fn() {
    var env = karate.env; // get system property 'karate.env'
    karate.log('karate.env system property was:', env);
    if (!env) {
        env = 'dev';
    }
    var config = {
        env: env,
        baseUrl: 'https://ad4b627a-bbd1-4ea3-a678-af41ceb5bfb1.mock.pstmn.io'
    }
    return config;
}

在src/test/resources目录新建目录features,然后新建getGrocery.feature文件

getGrocery.feature

Feature: Grocery API

  Background: The Request Body Configuration
    # Set a configuration for the payload
    * url baseUrl

  Scenario: Get All Products from Grocery
    Given header Content-Type = 'application/json'
    And path '/allGrocery'
    When method get
    Then status 200
    And match response.data[*].id == '#present'
    And match response.data[*].price == '#present'
    And match response.data[*].name == '#present'
    And match response.data[*].stock == '#present'

  Scenario Outline: Get Grocery Details with a name
    Given header Content-Type = 'application/json'
    And path '/allGrocery/<name>'
    When method get
    Then status 200
    And match response.data[0].name == "<name>"
    And match response.data[0].id == '#present'
    And match response.data[0].price == '#present'
    And match response.data[0].stock == '#present'

    Examples:
      | name   |
      | apple  |
      | grapes |

  Scenario: Add a new product to the Grocery Basket
    * def jsonBody =
      """
      {
        "id": 4,
        "name": "string",
        "price": 12.3,
        "stock": 3
      }
      """
    Given header Content-Type = 'application/json'
    And path '/add'
    And request jsonBody
    When method post
    Then status 201
    And response.message == "success"

  Scenario: Send an invalid payload while adding a new product
    Given path '/add'
    When method post
    Then status 400
    And response.message == "Request body has invalid format."

  Scenario: Search the product that has not found
    Given header Content-Type = 'application/json'
    And path '/allGrocery/cherry'
    When method get
    Then status 404
    And match response.message == "The product has not found"

在src/test目录新建runner目录,然后新建TestRunner class文件

TestRunner.java

package runner;

import com.intuit.karate.Results;
import com.intuit.karate.junit5.Karate;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TestRunner {

    @Test
    public void testParallel() {
        Results results = Karate.run("src/test/resources/features").parallel(6);
        assertEquals(results.getErrorMessages(), 0, results.getFailCount());
    }
}

最终目录如下:

3.运行测试

在TestRunner.java文件点击run

测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值