【问题标题】:Mocha - Setting message (reason) for test skipMocha - 为测试跳过设置消息(原因)
【发布时间】:2016-11-03 08:52:41
【问题描述】:

是否可以设置跳过特定测试的消息(提及原因),以便在报告者中使用它。

describe('xxx', function() {
 checkToSkip(1)("test1", function() {\*test goes here*\});
 checkToSkip(4)("test2", function() {\*test goes here*\});
});

function checkToSkip(now) {
    return now > 3 ? it : xit; 
   //return now > 3 ? it : it.skip; 
}

这里 'test1' 将被跳过,因为 'checkToSkip' 返回 'xit'(或 it.skip)。是否可以向记者传达跳过测试的原因?如下所示(或任何其他可能的方式)。

checkToSkip(4)("test2", function() { \\ test goes here}, "My Skip message!!!!" );

注意:我在 webdriverIO 中使用 mocha。

谢谢。

【问题讨论】:

标签: mocha.js webdriver-io


【解决方案1】:

我只是稍微修改checkToSkip 函数并使用Pending Tests 而不是skip()

function checkToSkip(now, title, testCallable) {
    if (now > 3) {
        it(title, testCallable);
    } else {
        it(title+"#My Skip message!#");
    }
}

然后像这样使用它:

describe('xxx', function() {
    checkToSkip(1, "test1", function() {\*test goes here*\});
});

【讨论】:

  • 即使它(标题)也无法传递任何信息来解释为什么这个测试被跳过/挂起。
  • @VNP 为什么不能将它挂起的原因放入title
  • 很多测试用例都是用上面的方法实现的,影响很大
  • 这绝对是一种解决方法,但是有什么 API 方法可以解决这个问题吗?我知道 mocha 文档效率不高。
  • @@martin - 这似乎是实现这一目标的唯一方式,也是最好的方式
【解决方案2】:

可以在跳过之前在测试中修改测试标题本身:

it('My cool test', async function() {
  if (this.response.status !== 201) {
   this._runnable.title += ' - Skipped with reason: wrong response code'
   this.skip()
  }
  expect(this.response.data).to.have.property('mycoolproperty')
})

也可以形式化检查并将其移至外部函数:

function skipIf(that, condition, reason){
  if (condition) {
    that._runnable.title += ` - Skipped with reason: ${reason}`
    that.skip()
  }
}

所以测试看起来像这样:

it('My cool test', async function() {
  skipIf(this, this.response.status !==201, 'Wrong response status')
  expect(this.response.data).to.have.property('mycoolproperty')
})

【讨论】:

    猜你喜欢
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2022-06-17
    • 1970-01-01
    • 2017-08-03
    相关资源
    最近更新 更多