在Linux上实现Swagger的自动化测试,可以遵循以下步骤:
首先,确保你的Linux系统上已经安装了以下工具:
sudo apt update
sudo apt install openjdk-11-jdk
sudo apt install maven
你可以从Postman官网下载并安装Postman,或者使用以下命令通过Snap安装:
sudo snap install postman
创建一个新的Maven项目,并在pom.xml文件中添加Swagger和Rest-Assured的依赖。
<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>com.example</groupId>
<artifactId>swagger-automation</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>swagger-automation</name>
<description>Demo project for Swagger Automation</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- Swagger dependencies -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.1.12</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>2.1.12</version>
</dependency>
<!-- Rest-Assured dependencies -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
在你的项目中创建一个Swagger文档文件(例如swagger.yaml),描述你的API。
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
paths:
/users:
get:
summary: List all users
responses:
'200':
description: A list of users
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
email:
type: string
format: email
创建一个测试类,使用Rest-Assured来发送请求并验证响应。
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.And;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static org.hamcrest.Matchers.*;
public class UserApiSteps {
@Given("I have the base URI for the API")
public void i_have_the_base_uri_for_the_api() {
RestAssured.baseURI = "http://localhost:8080";
}
@When("I send a GET request to /users")
public void i_send_a_get_request_to_users() {
RestAssured.given()
.when()
.get("/users")
.then()
.statusCode(200)
.body("size()", greaterThan(0));
}
@And("the response should contain a user with the name {string}")
public void the_response_should_contain_a_user_with_the_name(String userName) {
RestAssured.given()
.when()
.get("/users")
.then()
.body("find { it.name == $userName }", notNullValue());
}
}
使用Cucumber运行你的测试。
mvn test
如果你想在浏览器中查看和测试你的API,可以使用Swagger UI。
你可以从Swagger UI官网下载并安装Swagger UI,或者使用以下命令通过Snap安装:
sudo snap install swagger-ui
启动Swagger UI并访问你的API文档。
swagger-ui -p 8080 -s ./path/to/swagger.yaml
现在,你可以在浏览器中访问http://localhost:8080来查看和测试你的API。
通过以上步骤,你可以在Linux上实现Swagger的自动化测试。