【问题标题】:Mocha:How to bail only failed describe() in nested describe()?Mocha:如何在嵌套的 describe() 中只保释失败的 describe()?
【发布时间】:2017-06-20 07:37:29
【问题描述】:

我的测试结构如下

describe('Test Suite'){
     describe('First Test Case'){
        it('1st step'){};
        it('2nd step'){};
        it('3rd step'){};
    }
     describe('Second Test Case'){
        it('1st step'){};
        it('2nd step'){};
        it('3rd step'){};
    }
}

我想使用 --bail 这样如果在第一个测试用例中任何 it() 失败,那么 describe() 应该被保释。但是第二个测试用例应该运行。

我得到了预期的结果:

describe('Test Suite'){
     this.bail(false)
     describe('First Test Case'){
        this.bail(true);
        it('1st step'){};
        it('2nd step'){};
        it('3rd step'){};
    }
     describe('Second Test Case'){
        this.bail(true);
        it('1st step'){};
        it('2nd step'){};
        it('3rd step'){};
    }
}

在每个 describe() 中不明确将 bail 分配为 true 的情况下,还有其他方法吗?

【问题讨论】:

  • 我很确定没有。

标签: javascript unit-testing mocha.js


【解决方案1】:

我找到了解决方法,也许会有所帮助。

你可以设置你的条件不是文件,而是我的函数名或测试名。

在设置文件中:

const FAILED_TESTS = {};
// Skip test if first test from folder failed
beforeEach(function() {
  if (FAILED_TESTS[this.currentTest.file]) {
    this.skip();
  }
});

afterEach(function() {
  if (this.currentTest.state === "failed") {
    FAILED_TESTS[this.currentTest.file] = true;
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 2013-10-18
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2018-01-03
    相关资源
    最近更新 更多