在Linux上可通过以下方式使用Swagger实现API测试:
Swagger UI交互测试
docker pull swaggerapi/swagger-ui:v4.15.5
docker run -d -p 8080:8080 swaggerapi/swagger-ui:v4.15.5
访问http://localhost:8080,导入swagger.yaml/swagger.json文件,点击“TRY IT OUT”直接测试接口。命令行工具测试
curl -X GET "http://localhost:8080/api/users?id=1"
java -jar swagger-codegen-cli.jar generate -i swagger.yaml -l python -o ./client
pip install pytest requests
# 编写测试脚本调用生成的客户端
自动化测试工具集成
npm install -g newmannewman run collection.json -r cli,json。dredd swagger.yaml http://localhost:8080,自动校验接口响应与文档定义是否一致。说明:优先通过Swagger UI快速验证接口逻辑,复杂场景可结合代码生成或工具链实现自动化测试。