如何跳至下一个接下来描述Mocha中的错误? [英] How to skip to next next describe on error in Mocha?
问题描述
我有一堆describe
可以测试API的不同部分.在某一节中,所有测试都依赖于后续的一个测试.我想让Mocha运行第一个测试,如果失败,请跳过所有后续测试,并为API的下一部分运行下一个测试套件.
I have a bunch of describe
s that test different parts of an API. In one section, all the tests are dependent on one test succeeding. I want to make Mocha run the first test, and if it fails, skip all following tests and run the next test suite for the next section of the API.
mocha --bail
在第一次失败后将完全停止测试,并且不会继续进行下一部分.
mocha --bail
would stop testing altogether after the first fail, and won't continue to the next section.
mocha-steps
是一个可行的解决方案,但我不希望使用任何外部库.此外,它不会在失败后执行skip
步骤,也不会完全打印它们.就像我说的那样,这是一个可行的解决方案,但并不理想.
mocha-steps
is a viable solution, but I prefer not to use any external libraries. In addition, it doesn't skip
steps after the failure, it doesn't print them altogether. Like I said, it's a viable solution, but not ideal.
在香草摩卡咖啡中实现此行为的最佳方法是什么?
What would be the best way to implement this behavior in vanilla Mocha?
推荐答案
在包含所有其他测试的describe
块内的before
挂钩中放入您所谓的第一个测试":
Put what you call your "first test" in a before
hook inside a describe
block that contains all the other tests:
describe("bunch of related tests", function () {
before(function () {
// "first test" here
});
it("test 1", function () { ... });
it("test 2", function () { ... });
// ...
});
这是在香草摩卡"中设置before
挂钩中的代码与每个测试之间的依赖关系的正确方法.如果before
挂钩失败,Mocha将报告它,并且将跳过describe
块中的所有测试.如果您在其他地方有其他测试,它们仍将运行.
This is the proper way in "vanilla Mocha" to set a dependency between the code in the before
hook and each of the tests. If the before
hook fails, Mocha will report it, and it will skip all the tests in the describe
block. If you have other tests elsewhere, they will still run.
这篇关于如何跳至下一个接下来描述Mocha中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!