我在测试用例中有一个循环,我为每个数据样本重复一部分测试。
如何添加自定义成功消息,指示代表测试用例的特定数据是否成功?
例如,我的代码如下:
it('Should not allow saving bad data: ', async function () {
let badData = TestDataProvider.allData.invalid;
for (let i = 0; i < badData.length; i++) {
let d = badData[i];
let res = await request.post('/data').send(d);
let object = null;
try {
object = JSON.parse(res.text);
} catch (e) {
object = {};
}
expect(res.statusCode).to.equal(206);
expect(object).not.to.contain.a.property('_id');
console.log('Verified case: ' + d.testDesc);
}
});
我希望"Verified case..."
消息在报告中显示为成功的测试运行而不是控制台消息。
testDesc
属性包含测试描述,如:"Missing field a", "Invalid property b", "Missing field c"
。
我解决了创建动态测试的问题:
const allData = JSON.parse(fs.readFileSync('data.json'), 'utf8'));
allData.invalid.forEach((badData) => {
it('Should not allow saving with: ' + badData.testDesc, async () => {
let res = await request.post('/data').send(badData);
let d = null;
try {
d = JSON.parse(res.text);
} catch (e) {
d = {};
}
expect(res.statusCode).to.equal(206);
expect(d).not.to.contain.a.property('_id');
});
});