如何从JSON文件中提取数据来编写测试用例

时间:2017-06-05 09:22:02

标签: json unit-testing testing postman testcase

如何从postman中的JSON输出中提取所需的父/子节点值。

我需要从下面的JSON文件中提取model.ConfirmPassword

{
  "Message": "The request is invalid.",
  "ModelState": {
  "model.ConfirmPassword": [
  "The password and confirmation password do not match."
  ]
}

要获得该属性,要传递的属性是什么。 jsonData.value无效,如下所述。

var jsonData = JSON.parse(responseBody);
tests["Your test name"] = jsonData.value;

Image

2 个答案:

答案 0 :(得分:0)

修改在我错过之前 model.ConfirmPassword 嵌套在 ModelState

这是如何读取属性:

var x = jsonData.ModelState['model.ConfirmPassword'];
console.log(x);

这是插件:http://plnkr.co/edit/rCtVijwWpPgX7SdnP2ja

答案 1 :(得分:0)

答案应该是

var jsonData = JSON.parse(responseBody);
tests["Your test name"] = jsonData.ModelState['model.ConfirmPassword'][0] ==="The password and confirmation password do not match.";

需要在[0]中将索引作为jsonData.ModelState['model.ConfirmPassword'][0]传递 否则,如果我们想访问儿童元素,它将无法工作。 它可以是[0]或特定索引。

相关问题