在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
sudo apt install flatpak
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
flatpak install flathub com.postman.Postman
安裝后可通過終端輸入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("success");
});
支持變量引用(如pm.environment.get("token")
)、數據參數化(CSV/JSON文件)等。npm install -g newman
newman run collection.json -e environment.json --reporters cli,html
支持生成HTML報告或集成到CI/CD流程。pm.iterationData.get("變量名")
引用。pm.environment.set()
和pm.environment.get()
動態切換環境變量。參考來源: