1. 配置Maven依赖,在pom.xml文件中添加以下依赖:
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-core</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit5</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
指定测试执行结果导出位置
<build>
<testResources>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
...
</plugins>
</build>
2. 创建测试文件
在src/test/java目录下创建一个测试类,例如KarateTestRunner.java,并在其中使用@Karate.Test注解指定需要运行的Karate测试脚本文件,例如:
package com.example;
import com.intuit.karate.junit5.Karate;
class KarateTestRunner {
@Karate.Test
Karate testAll() {
return Karate.run(“example”).relativeTo(getClass());
}
}
3. 创建Karate测试脚本
在src/test/java目录下创建Karate测试脚本文件,例如example.feature,并编写测试脚本。例如:
Feature: Example feature
Background:
* url 'https://jsonplaceholder.typicode.com'
Scenario: GET request to /posts
Given path '/posts'
When method get
Then status 200
And match response[0].id == 1
Scenario: POST request to /posts with request body
Given path '/posts'
And header Content-Type = 'application/json'
And request {title: 'test', body: 'test', userId: 1}
When method post
Then status 201
4. 运行测试
使用Maven命令mvn test来运行测试,或者在IDE中右键点击KarateTestRunner.java文件选择“Run KarateTestRunner”
关于karate-config.js 配置文件
在Karate中,可以使用karate-config.js文件来定义使用的变量和配置。该文件必须位于项目的src/test/java目录下。
Karate会在执行测试之前自动加载karate-config.js文件并将其解析为一个JavaScript对象。在测试中可以通过karate-config.js中定义的变量名称来读取该对象中的变量值。
下面是一个示例karate-config.js文件:
function fn() {
var config = {
baseUrl: 'https://jsonplaceholder.typicode.com',
authCredentials: {
username: 'admin',
password: 'password'
}
}
return config;
}
该文件定义了一个名为config的JavaScript对象,并包含了两个属性baseUrl和authCredentials。这些属性可以在测试脚本中使用,例如:
Feature: Example feature
Background:
* url baseUrl
Scenario: GET request to /posts
Given path '/posts'
When method get
Then status 200
And match response[0].id == 1
Scenario: GET request to /users with authentication
Given url baseUrl
Given path '/users'
Given header Authorization = 'Basic ' + karate.base64Encode(authCredentials.username + ':' + authCredentials.password)
When method get
Then status 200
在上面示例中,可以看到baseUrl和authCredentials变量在测试脚本中被使用,而其值则是通过读取karate-config.js文件中定义的config对象中的属性得到的。
关于karate 脚本语法
Karate使用的是Gherkin 语言,这是在Cucumber(自动化测试框架)框架下定义的标准,所以karate也是基于Cucumber创建的自动化测试框架。
语法中包括如下关键字,示例如下:
1. Given:指定测试数据或预置条件 例如:
Given url 'https://example.com'
Given path '/users'
Given header Authorization = 'Bearer ' + accessToken
Given request { name: 'John', age: 30 }
2. When: 触发被测系统的操作 例如:
When method GET
When path '/users'
When request { name: 'Jane', age: 25 }
3. Then:验证测试结果 例如:
Then status 200
Then match response.name == 'John'
Then assert response.users[0].age == 30
4. And:将多个关键字串在一起,连续执行多个操作 例如:
Given url 'https://example.com'
And path '/users'
And header Authorization = 'Bearer ' + accessToken
When method GET
And request { name: 'Jane', age: 25 }
Then status 200
And match response.name == 'Jane'
5. Table:用于传递数据表格 例如:
Scenario: Create user with multiple addresses
Given url 'https://example.com'
And path '/users'
And header Authorization = 'Bearer ' + accessToken
And request { name: 'John', age: 30 }
When method POST
And table addresses
| city | zip |
| New York | 10001 |
| Boston | 02111 |
Then status 201
其中table关键字可以使测试数据更加规范、简洁,方便复用。
一些参考网址:
Gherkin语言语法: Gherkin Reference - Cucumber Documentation
Karate 官方:GitHub - karatelabs/karate: Test Automation Made Simple
Karate get start:Karate Framework Tutorial: Automated API Testing With Karate