如何跳到下一个下一个描述摩卡中的错误?
How to skip to next next describe on error in Mocha?
我有一堆 describe
测试 API 的不同部分。在一个部分中,所有测试都取决于一个测试是否成功。我想让 Mocha 运行 成为第一个测试,如果失败,则跳过所有后续测试,运行 成为 API.
下一部分的下一个测试套件
mocha --bail
将在第一次失败后完全停止测试,并且不会继续进行下一部分。
mocha-steps
是一个可行的解决方案,但我不想使用任何外部库。此外,它不会在失败后执行 skip
步,也不会将它们全部打印出来。正如我所说,这是一个可行的解决方案,但并不理想。
在原版 Mocha 中实现此行为的最佳方式是什么?
将你所谓的 "first test" 放入包含所有其他测试的 describe
块内的 before
挂钩中:
describe("bunch of related tests", function () {
before(function () {
// "first test" here
});
it("test 1", function () { ... });
it("test 2", function () { ... });
// ...
});
这是 "vanilla Mocha" 中设置 before
挂钩中的代码与每个测试之间的依赖关系的正确方法。如果 before
挂钩失败,Mocha 将报告它,并跳过 describe
块中的所有测试。如果您在其他地方进行其他测试,它们仍将 运行.
虽然我对接受的答案投了赞成票,但我无法在 before
函数中对 运行 进行 Mocha it
测试。相反,我不得不将第一个测试分成它自己的 describe
并在测试通过时设置一个变量,然后检查包含所有其他测试的 describe
的 before
中的变量。
let passed = false
describe('first test', function() {
it('run the first test', function(done) {
if (theTestPassed)
passed = true
done()
})
})
describe('rest of the tests', function() {
before(function() {
if (!passed)
throw new Error('skip rest of the tests')
});
it('second test', ...)
it('third test', ...)
});
我有一堆 describe
测试 API 的不同部分。在一个部分中,所有测试都取决于一个测试是否成功。我想让 Mocha 运行 成为第一个测试,如果失败,则跳过所有后续测试,运行 成为 API.
mocha --bail
将在第一次失败后完全停止测试,并且不会继续进行下一部分。
mocha-steps
是一个可行的解决方案,但我不想使用任何外部库。此外,它不会在失败后执行 skip
步,也不会将它们全部打印出来。正如我所说,这是一个可行的解决方案,但并不理想。
在原版 Mocha 中实现此行为的最佳方式是什么?
将你所谓的 "first test" 放入包含所有其他测试的 describe
块内的 before
挂钩中:
describe("bunch of related tests", function () {
before(function () {
// "first test" here
});
it("test 1", function () { ... });
it("test 2", function () { ... });
// ...
});
这是 "vanilla Mocha" 中设置 before
挂钩中的代码与每个测试之间的依赖关系的正确方法。如果 before
挂钩失败,Mocha 将报告它,并跳过 describe
块中的所有测试。如果您在其他地方进行其他测试,它们仍将 运行.
虽然我对接受的答案投了赞成票,但我无法在 before
函数中对 运行 进行 Mocha it
测试。相反,我不得不将第一个测试分成它自己的 describe
并在测试通过时设置一个变量,然后检查包含所有其他测试的 describe
的 before
中的变量。
let passed = false
describe('first test', function() {
it('run the first test', function(done) {
if (theTestPassed)
passed = true
done()
})
})
describe('rest of the tests', function() {
before(function() {
if (!passed)
throw new Error('skip rest of the tests')
});
it('second test', ...)
it('third test', ...)
});