我有一个基于JSON的测试数据,我该如何在此测试数据上迭代测试以使每个信用对象运行?
cred: {
nameValue: 'ant',
emailValue: 'ant@gmail.com',
passwordValue: 'ant',
},
cred: {
nameValue: 'bat',
emailValue: 'bat@gmail.com',
passwordValue: 'bat',
},
您的测试数据JSON文件应该是这样,
[
{
"nameValue": "ant",
"emailValue": "ant@gmail.com",
"passwordValue": "ant"
},
{
"nameValue": "bat",
"emailValue": "bat@gmail.com",
"passwordValue": "bat"
}
]
现在您可以通过索引(如数组中的索引(访问它们
const testDataObject = require("path to testData json");
// to loop on all elements
testDataObject.forEach(function(element) {
it(' test case def ', function() {
console.log("nameValue "+ element['nameValue']+ "emailValue
"+element['emailValue'] + "passwordValue "+element[passwordValue]);
});
});
// to select any particular index
it(' test case def ', function() {
console.log("nameValue "+ testDataObject[1]['nameValue']+ "emailValue
"+testDataObject[1]['emailValue'] + "passwordValue "+testDataObject[1][passwordValue]);
});
});
在或您可以这样做
{
"cred1":
{
"nameValue": "ant",
"emailValue": "ant@gmail.com",
"passwordValue": "ant",
},
"cred2":
{
"nameValue": "bat",
"emailValue": "bat@gmail.com",
"passwordValue": "bat",
}
}
并通过
中的nodejs代码访问测试数据console.log( `${testDataObject['cred1']["nameValue"]}` );
console.log( `${testDataObject['cred1']["emailValue"]}` );
console.log( `${testDataObject['cred1']["passwordValue"]}` );