CODEARTS API-后置脚本:断言

时间:2024-04-11 09:18:00

断言

可以利用后置脚本验证API响应返回结果的正确性。

// 示例1:pm.response.to.have
pm.test('Response status is 200', function() {
  pm.response.to.have.status(200);
});

// 示例2:pm.expect()
pm.test('this is production', function() {
  pm.expect(pm.environment.get('env')).to.equal('production');
});

// 示例3:response assertions
pm.test('no error', function() {
  pm.response.to.not.be.error;
  pm.response.to.have.jsonBody('');
  pm.response.to.not.have.jsonBody('error');
});

// 示例4:pm.response.to.be* 
pm.test('no error', function() {
  // 断言结果:info, success, redirection, clientError,  serverError, are other variants
  pm.response.to.be.ok;
  // 断言有body,并且是json格式
  pm.response.to.be.withBody;
  pm.response.to.be.json;
});

support.huaweicloud.com/usermanual-codeartsapi/apiarts_01_0029.html