【问题标题】:How to add custom success message in Mocha tests?如何在 Mocha 测试中添加自定义成功消息?
【发布时间】:2019-05-01 02:54:20
【问题描述】:

我在测试用例中有一个循环,我对每个数据样本重复部分测试。

如何添加自定义成功消息,指示代表测试用例的特定数据是否成功?

例如,我的代码是这样的:

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"

【问题讨论】:

    标签: mocha.js chai should.js


    【解决方案1】:

    我解决了创建动态测试的问题:

    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');
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-12
      • 2011-06-03
      • 2018-01-22
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多