我使用摩卡进行一些集成测试,并且有很多测试集。 他们都有每种设置的初始化测试。当这些测试失败时,其余的设置不应该运行,因为所有这些测试都会失败。 问题是,我无法避免这样的初始化测试,因为部分代码/环境是由一些不保证任何正确结果的工具生成的。有条件的摩卡测试
是否可以使用摩卡来实现这一点?
我使用摩卡进行一些集成测试,并且有很多测试集。 他们都有每种设置的初始化测试。当这些测试失败时,其余的设置不应该运行,因为所有这些测试都会失败。 问题是,我无法避免这样的初始化测试,因为部分代码/环境是由一些不保证任何正确结果的工具生成的。有条件的摩卡测试
是否可以使用摩卡来实现这一点?
使用BDD接口,以正常的方式与摩卡做,这是把任何东西,设置了测试环境为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
一个测试依赖于失败,那么测试不会运行。其他不依赖于它的测试仍然会运行。所以在上面,如果回调传递给before
在describe
命名first
失败,在名为second
的describe
测试不会造成任何影响,并运行该示例,只要自己before
和beforeEach
回调不会失败。
除此之外,Mocha设计用于运行彼此独立的独立的测试。所以如果一个it
失败,那么其他人仍然运行。
我发现mocha-steps
基本上让你写的it()
S(称为step()
)和摩卡“产业链”将中止,如果他们中的一个突破,从而避免了必然的失败的级联套件,我发现pull request 8马克后续步骤和subsuites作为待定。所以我可以这样写:
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
测试将失败;随后的(和subsuite将被标记为未决)。