以下是在Ubuntu上进行Postman自动化测试的实践步骤:
sudo snap install postman --classic
wget -qO - https://dl.postman.co/postman.gpg | sudo apt-key add -
echo "deb https://dl.postman.co/debian $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/postman.list
sudo apt update && sudo apt install postman
// 验证状态码为200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// 验证响应体包含特定字段
pm.test("Response has required fields", function () {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property("id");
});
api_url
),在请求中使用{{api_url}}
引用。npm install -g newman
newman run /path/to/collection.json -e /path/to/environment.json --reporters cli,html --reporter-html-export ./report.html
-e
:指定环境文件-r
:指定报告格式(如cli
、html
).github/workflows/postman.yml
:name: Postman Automation Test
on: [push, pull_request]
jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Newman
run: npm install -g newman
- name: Run Postman Collection
run: newman run ./collections/api-tests.json -e ./environments/test-env.json --reporters cli,html
pm.iterationData.get("变量名")
引用。newman-reporter-htmlextra
生成可视化HTML报告,或集成Allure生成专业报告。