Postmanでよく使われる断言


テスト天気:データを集約するための無料インタフェースhttps://www.juhe.cn/ツール:Postman
jsonインタフェース:
http://apis.juhe.cn/simpleWeather/query?city=武漢&key=******
key値は自分で集約データサイトで取得し、ここに記入すれば使用できます.
このケースで設定されたアサーション:
pm.test('ステータスコードは200',function(){
  pm.response.to.have.status(200)
});//ステータスコードは200と言い切る
 
pm.test('応答時間500 ms未満',function(){
    pm.expect(pm.response.responseTime).to.be.below(500);
});//応答時間500 ms未満
 
var jsonData =JSON.parse(responseBody);
tests['bodyのreason結果']=jsonData.reason=='クエリー成功!//Bodyの中身を検証する
tests['bodyのcityが正しいか]=jsonData.result.city=='武漢'//bodyの都市が正しいかどうかを検証
 
//当日からの検証
pm.test('bodyの初日の天気が当日であるかどうかを検証',function(){
    var jsonData = pm.response.json();
    pm.expect(jsonData.result.future[0].date)=='2020-06-15'//後で検証する必要がある場合は、ここを変更する時間
});
 
テスト結果:
Postman常用断言_第1张图片
 
postmanの一般的な断言方法の紹介:
 
環境変数の設定
1 pm.environment.set( "variable_key" "variable_value" );
  
Setting a nested object as an environment variable(ネストされたオブジェクトを環境変数に設定)
1 2 3 4 5 var   array = [1, 2, 3, 4]; pm.environment.set( "array" , JSON.stringify(array,  null , 2));   var   obj = { a: [1, 2, 3, 4], b: { c:  'val'   } }; pm.environment.set( "obj" , JSON.stringify(obj));
  
Getting an environment variable(環境変数を取得)
1 pm.environment.get( "variable_key" );
  
Getting an environment variable(whose value is a stringified object)は、環境変数(文字列化されたオブジェクトの値)を取得します.
1 2 3 4 // These statements should be wrapped in a try-catch block if the data is coming from an unknown source.   var   array = JSON.parse(pm.environment.get( "array" )); var   obj = JSON.parse(pm.environment.get( "obj" ));
  
Clear an environment variable(環境変数をクリア)
1 pm.environment.unset( "variable_key" );
  
Set a global variable(グローバル変数を設定)
1 pm.globals.set( "variable_key" "variable_value" );
  
Get a global variable(グローバル変数を取得)
1 pm.globals.get( "variable_key" );
  
Clear a global variable(グローバル変数をクリア)
1 pm.globals.unset( "variable_key" );
  
Get a variable(変数を取得)
この関数は、グローバル変数とアクティブ環境で変数を検索します.
1 pm.variables.get( "variable_key" );
  
Check if response body contains a string(応答主体に文字列が含まれているかどうかを確認)
1 2 3 pm.test( "Body matches string" function   () {      pm.expect(pm.response.text()).to.include( "string_you_want_to_search" ); });
  
Check if response body is equal to a string(応答主体が文字列に等しいかどうかを確認)
1 2 3 pm.test( "Body is correct" function   () {      pm.response.to.have.body( "response_body_string" ); });
  
Check for a JSON value(JSON値チェック)
1 2 3 4 pm.test( "Your test name" function   () {      var   jsonData = pm.response.json();      pm.expect(jsonData.value).to.eql(100); });
  
Content-Type is present(コンテンツタイプが存在する)
1 2 3 pm.test( "Content-Type is present" function   () {      pm.response.to.have.header( "Content-Type" ); });
  
Response time is less than 200 ms(応答時間が200 ms未満)
1 2 3 pm.test( "Response time is less than 200ms" function   () {      pm.expect(pm.response.responseTime).to.be.below(200); });
  
Status code is 200(ステータスコードは200)
1 2 3 pm.test( "Status code is 200" function   () {      pm.response.to.have.status(200); });
  
Code name contains a string(コード名は文字列を含む)
1 2 3 pm.test( "Status code name has string" function   () {      pm.response.to.have.status( "Created" ); });
  
Successful POST request status code(成功したPOST要求ステータスコード)
1 2 3 pm.test( "Successful POST request" function   () {      pm.expect(pm.response.code).to.be.oneOf([201,202]); });
  
UseTinyValidator for JSON data(JSONデータに対してTinyValidatorを使用)
1 2 3 4 5 6 7 8 9 10 11 12 var   schema = {   "items" : {   "type" "boolean"   } }; var   data1 = [ true false ]; var   data2 = [ true , 123];   pm.test( 'Schema is valid' function () {    pm.expect(tv4.validate(data1, schema)).to.be. true ;    pm.expect(tv4.validate(data2, schema)).to.be. true ; });
  
Decode base 64 encoded data(base 64符号化データを復号)
1 2 3 4 5 6 7 8 var   intermediate,      base64Content,  // assume this has a base64 encoded value      rawContent = base64Content.slice( 'data:application/octet-stream;base64,' .length);   intermediate = CryptoJS.enc.Base64.parse(base64content);  // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js pm.test( 'Contents are valid' function () {    pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be. true // a check for non-emptiness });
  
Send an asynchronous request(非同期リクエスト送信)
この機能は、事前要求としても、テストスクリプトとしても使用できます.
1 2 3 pm.sendRequest( "https://postman-echo.com/get" function   (err, response) {      console.log(response.json()); });
  
Convert XML body to a JSON object(XML本文をJSONオブジェクトに変換)
1 var   jsonObject = xml2Json(responseBody);