在Ubuntu中集成Postman与Jenkins的完整步骤
在Ubuntu服务器上安装Jenkins(持续集成工具),用于自动化执行Postman测试:
# 添加Jenkins存储库密钥和源
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
# 更新包列表并安装Jenkins
sudo apt update
sudo apt install jenkins
# 启动Jenkins并设置开机自启
sudo systemctl start jenkins
sudo systemctl enable jenkins
访问http://<服务器IP>:8080
完成Jenkins初始化配置(按提示输入管理员密码)。
Newman是Postman的命令行接口,需通过Node.js安装:
# 安装Node.js和npm(Node包管理器)
sudo apt install nodejs npm -y
# 全局安装Newman
sudo npm install -g newman
# 验证安装
newman -v
确保输出Newman版本号(如5.3.0
),表示安装成功。
登录Jenkins管理界面,进入Manage Jenkins
→ Manage Plugins
,安装以下插件:
在Postman客户端中:
New
→ Collection
,输入集合名称(如API_Tests
);+ Add Request
,配置API请求(URL、Method、Headers、Body等);Tests
标签页,编写测试脚本(如验证状态码、响应数据):// 示例:验证状态码为200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// 示例:验证响应体包含"success"
pm.test("Response contains 'success'", function () {
const responseJson = pm.response.json();
pm.expect(responseJson.message).to.eql("success");
});
Save
保存集合。Export
,选择Collection Format: v2.1
,导出为collection.json
;Manage Environments
,创建环境(如Test_Env
),添加变量(如base_url: https://api.example.com
),导出为environment.json
;/var/lib/jenkins/workspace/api_tests/
),避免中文路径。New Item
,输入任务名称(如Postman_API_Tests
),选择Pipeline
,点击OK
;Pipeline
配置页,选择Pipeline script from SCM
(从代码仓库拉取脚本),SCM选择Git
,输入仓库URL(如https://github.com/your-username/api-tests.git
),分支选择main
。在代码仓库根目录创建Jenkinsfile
,内容如下(整合Newman执行与报告生成):
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/your-username/api-tests.git'
}
}
stage('Run Postman Tests') {
steps {
sh '''
# 运行Postman测试,生成JUnit和HTML报告
newman run "collection.json" \
-e "environment.json" \
-d "testdata.csv" \ # 可选:数据驱动测试
-r cli,junit,htmlextra \
--reporter-junit-export "reports/junit.xml" \
--reporter-htmlextra-export "reports/report.html"
'''
}
}
}
post {
always {
// 发布JUnit测试报告
junit 'reports/junit.xml'
// 发布HTML报告(需安装HTML Publisher插件)
publishHTML(target: [
allowEmptyArchive: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'reports',
reportFiles: 'report.html',
reportName: 'Postman Test Report'
])
// 邮件通知(需安装Email Extension插件)
emailext (
subject: "Postman API Tests Result: ${currentBuild.result}",
body: """<p>Check the test report here: ${BUILD_URL}HTML_Report/</p>""",
to: 'dev-team@example.com'
)
}
success {
echo 'All Postman tests passed!'
}
failure {
echo 'Some Postman tests failed!'
}
}
}
说明:
newman run
命令:执行collection.json
,使用environment.json
中的环境变量,生成JUnit(用于Jenkins解析)和HTML(可视化)报告;junit
步骤:将JUnit报告集成到Jenkins;publishHTML
步骤:发布HTML报告,方便查看详细结果;emailext
步骤:发送邮件通知(可选)。保存Jenkins任务配置,点击Build Now
,Jenkins将自动:
若需代码提交后自动触发,可在Pipeline
配置页的Build Triggers
中选择GitHub hook trigger for GITScm polling
,并在GitHub仓库的Webhooks
中添加Jenkins钩子(URL格式:http://<服务器IP>:8080/github-webhook/
)。
collection.json
、environment.json
等文件有读取权限,避免中文路径;Credentials
管理(如Secret text),而非直接写入环境变量文件;Jenkinsfile
中添加npm install -g newman
(若未全局安装),或使用Docker容器运行Newman(避免污染主机环境)。