【发布时间】:2014-04-06 05:44:49
【问题描述】:
我使用 mocha 进行一些集成测试,并且有很多测试集。 每组都有初始化测试。当此类测试失败时,该集合的其余部分根本不应该运行,因为如果一个失败,那么每个都将失败。 问题是我无法避免这样的初始化测试,因为部分代码/环境是由某些不保证任何正确结果的工具生成的。
是否可以使用 mocha 来实现?
【问题讨论】:
标签: javascript mocha.js
我使用 mocha 进行一些集成测试,并且有很多测试集。 每组都有初始化测试。当此类测试失败时,该集合的其余部分根本不应该运行,因为如果一个失败,那么每个都将失败。 问题是我无法避免这样的初始化测试,因为部分代码/环境是由某些不保证任何正确结果的工具生成的。
是否可以使用 mocha 来实现?
【问题讨论】:
标签: javascript mocha.js
使用 BDD 接口,使用 Mocha 执行此操作的常规方法是将设置测试环境的任何内容放入 before 或 beforeEach:
describe("foo", function () {
describe("first", function () {
before(function () {
// Stuff to be performed before all tests in the current `describe`.
});
beforeEach(function () {
// Stuff to perform once per test, before the test.
});
it("blah", ...
// etc...
});
describe("second", function () {
before(function () {
// Stuff to be performed before all tests in the current `describe`.
});
beforeEach(function () {
// Stuff to perform once per test, before the test.
});
it("blah", ...
// etc...
});
});
如果测试所依赖的before 或beforeEach 失败,则不会运行测试。其他不依赖它的测试仍然会运行。因此,在上面的示例中,如果在名为 describe 的 before 中传递给名为 first 的回调失败,则在名为 second 的 describe 中的测试根本不会受到影响并且会运行,前提是它们自己的before 和 beforeEach 回调不会失败。
除此之外,Mocha 旨在运行彼此独立的测试。因此,如果一个 it 失败,那么其他的仍然运行。
【讨论】:
我发现mocha-steps 基本上允许您编写it()s 的“链”(称为step()),如果其中一个发生故障,mocha 将中止该套件,从而避免一连串不可避免的故障,并且我发现pull request 8 将后续步骤和子套件标记为待处理。所以我可以写:
describe("businessCode()", function() {
step("should be not null", function() {
assert(businessCode() != null)
});
step("should be a number", function() {
assert(typeof businessCode() === 'number');
});
step("should be greater than 10", function() {
assert(businessCode() > 10);
});
describe("thingThatCallsBusinessCode()", function() {
step("should be greater than 10", function() {
assert(thingThatCallsBusinessCode() != null);
});
});
});
如果例如businessCode() 返回一个布尔值,只有 should be a number 测试会失败;后续的(并且子套件将被标记为待处理)。
【讨论】: