1. 環境變量配置分離
步驟:
- 點擊Postman右上角,點擊“manage envionments”圖標,新建環境並配置環境變量參數:變量名、默認值、當前值
- {{}} 引用環境變量值
- 切換運行環境,發起Http請求
2. PreRequest Script請求參數預處理
作用:請求前動態構建請求參數,滿足各種定製化場景
場景:例如對外開發的服務API一般需要走網關中轉,通過appId/appSecret簽名,可通過PreRequest Script實現簽名邏輯,實現請求的靈活調用
案例:上傳人臉識別圖片
訪問路徑:http://{{server.url}}:9099/ticketMgmt/api/facialrecognition/uploadSnapshot?appid={{appid}}&sign={{sign}}
腳本:
//配置appId/appSecret
var appid='10001';
var appKey='a049eb2144';
//實現請求籤名邏輯
var requestBody = request.data;
var requestStr = JSON.stringify(requestBody).replace(/\\n/g,'').replace(/\\/g,"").substr(1);
requestStr = requestStr.substr(0, requestStr.length -1);
var signStr = 'appid='+appid+'&'+'body='+requestStr+'&key='+appKey;
var md5 = CryptoJS.MD5(signStr).toString().toUpperCase(); //計算簽名
console.log("md5 digest---->" + md5);
console.log('postman environment server.url: ' + pm.environment.get('server.url'));
//設置全局變量,方便URL通過{{}}引用,實現動態修改
postman.setGlobalVariable("sign", md5);
postman.setGlobalVariable("appid", appid);
3. 測試用例覆蓋測試
// 響應狀態測試
pm.test("response status test", function () {
pm.response.to.have.status(200);
pm.response.to.be.ok;
pm.response.to.not.be.error;
//響應頭內容校驗
pm.response.to.have.header("Content-Type");
pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/json;charset=UTF-8');
//響應時間校驗: <= 200ms
pm.expect(pm.response.responseTime).to.be.below(200);
});
//響應體內容校驗
pm.test("response content test", function () {
pm.response.to.be.withBody;
pm.response.to.be.json;
pm.response.to.have.jsonBody("code");
// pm.response.to.not.have.jsonBody("code");
let responseBody = pm.response.json();
console.log("response data: " + JSON.stringify(responseBody));
pm.expect(responseBody.code).to.eql('00');
pm.expect(responseBody.code).to.be.oneOf(['00']);
//響應字段類型校驗
pm.expect(responseBody.code).to.not.be.an("object");
pm.expect(responseBody.message).to.be.a("string");
pm.expect(responseBody.code).to.not.be.a("number");
pm.expect(responseBody.code).to.not.be.an("array");
pm.expect(responseBody.website).to.be.undefined;
pm.expect(responseBody.code).to.not.be.null;
});
//Cookie 校驗
pm.test('cookie test', function(){
pm.expect(pm.cookies.has('JSESSIONID')).to.not.be.true;
pm.expect(pm.cookies.get('JSESSIONID')).to.be.undefined;
});
//數組驗證
pm.test('array test', function(){
//測試用,實際調整成響應頭結構
let names = ['a', 'b', 'c'];
pm.expect(names).to.be.not.empty;
pm.expect(names).to.include('a');
pm.expect(names).to.have.members(['a','b','c']);
pm.expect('Subscriber').to.be.oneOf(["Subscriber", "Customer", "User"]);
});
//對象驗證
pm.test('object test', function(){
pm.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');
pm.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');
pm.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');
pm.expect({a: 1}).to.have.property('a');
pm.expect({a: 1, b: 2}).to.be.an('object').that.has.all.keys('a', 'b');
const current = {"created": true, "errors": []};
const response = {"code": '000', "created": true, "errors": []};
pm.expect(response).to.deep.include(current); //對象屬性包含
});
//結構校驗
pm.test('struct test', function(){
const schema = {"items": {"type": "boolean"}}; //定義數組字段類型:true
const data1 = [true, false];
const data2 = [true, 123];
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.not.be.true;
//定義json字段類型:響應體code、message必須string類型,data未定義可以任意類型
const jsonSchema = {"properties": {"code": {"type": "string"}, "message":{"type":"string"}}};
pm.response.to.have.jsonSchema(jsonSchema);
});
//發送異步請求
pm.test('send async request', function(){
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
});
單個請求訪問效果:
Postman Collector Runner效果: