在CentOS系统上进行Swagger自动化测试,可以按照以下步骤进行:
首先,需要在CentOS系统上安装Swagger。可以通过以下几种方法进行安装:
确保Python和pip已安装:
sudo yum install python3 python3-pip
安装Swagger:
pip3 install swagger-ui-express
创建一个简单的Swagger应用(可选):
from flask import Flask
from flask_swagger_ui import get_swaggerui_blueprint
app = Flask(__name__)
SWAGGER_URL = '/api-docs'
API_URL = 'http://petstore.swagger.io/v2/swagger.json' # 这里可以使用你自己的API文档URL
swaggerui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL, config={'app_name': "Swagger UI"})
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
if __name__ == '__main__':
app.run(debug=True)
运行应用:
python3 app.py
然后在浏览器中访问 http://localhost:5000/api-docs
即可看到Swagger UI界面。
安装Docker:
sudo yum install docker
sudo systemctl start docker
sudo systemctl enable docker
拉取Swagger UI镜像:
docker pull swaggerapi/swagger-ui-express
运行容器:
docker run -p 80:80 -d swaggerapi/swagger-ui-express
这样,Swagger UI将会在 http://localhost
上可用。
在CentOS上配置Swagger的步骤如下:
sudo yum install gcc-c make -y
curl -sL https://rpm.nodesource.com/setup_12.x | bash -
sudo yum install nodejs -y
验证Node.js和npm是否安装成功:
node -v
npm -v
mkdir /opt/swagger
cd /opt/swagger
# 安装Swagger Editor
wget https://github.com/swagger-api/swagger-editor/archive/v3.14.0.tar.gz
tar -xf v3.14.0.tar.gz
cd swagger-editor-3.14.0
npm install -g http-server
http-server -p 8080
# 安装Swagger UI
wget https://github.com/swagger-api/swagger-ui/archive/v3.34.0.tar.gz
tar -xf v3.34.0.tar.gz
cd swagger-ui-3.34.0
npm init -y
npm install express --save
cat > index.js <<EOF
var express = require('express');
var app = express();
var http = require('http');
app.use('/static', express.static('public'));
app.get('/', function (req, res) { res.send('Hello World!'); });
app.listen(3000, function () { console.log('Example app listening on port 3000!'); });
EOF
node index.js
访问 http://your-server-ip:3000/static/index.html
确认可以访问。
结合Swagger进行自动化测试,可以使用以下工具和方法:
安装Swagger Codegen:
wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.19/swagger-codegen-cli-2.4.19.jar -O swagger-codegen-cli.jar
生成Python测试客户端:
java -jar swagger-codegen-cli.jar generate -i https://petstore.swagger.io/v2/swagger.json -l python -o /tmp/python-api-client
安装Newman:
npm install -g newman
从Swagger导入到Postman集合:
# (使用Postman GUI导入后导出为JSON)
运行测试:
newman run my_api_collection.json --environment=test_environment.json --reporters cli,html
安装Schemathesis:
pip install schemathesis
运行测试:
schemathesis run https://petstore.swagger.io/v2/swagger.json --checks all --base-url http://localhost:8080 --hypothesis-max-examples=100
安装Dredd:
npm install -g dredd
创建配置文件:
echo '{
"language": "python",
"server": "python -m SimpleHTTPServer 8080",
"blueprint": "apiary.apib",
"endpoint": "http://localhost:8080"
}' > dredd.yml
运行测试:
dredd
示例测试代码:
given()
.contentType("application/json")
.body("{ \"id\": 1, \"name\": \"Fido\" }")
.when()
.post("/v2/pet")
.then()
.statusCode(200)
.body("name", equalTo("Fido"));
通过以上步骤,你可以在CentOS系统上进行Swagger自动化测试。根据具体需求,可以选择合适的工具和方法进行API文档生成、可视化、测试和自动化测试。