简体   繁体   English

`before()` 和 `beforeEach()` 有什么区别?

[英]What is the difference between `before()` and `beforeEach()`?

What specifically is the difference between Mocha 's before() and beforeEach() ? Mochabefore()beforeEach()之间的具体区别是什么? (Same question for after() and afterEach() .) after()afterEach()的相同问题。)

I assume before() runs once per describe() block, and beforeEach() runs once per test ( it() block).我假设每个describe()块 before( before()运行一次,并且每个测试( it()块) beforeEach()运行一次。 Is that true?真的吗?

And when would I choose to use one over the other?我什么时候会选择使用一个而不是另一个?

before() is run once before all the tests in a describe before()describe 所有测试之前运行一次
after() is run once after all the tests in a describe after()describe 所有测试之后运行一次
beforeEach() is run before each test in a describe beforeEach()describe 每个测试之前运行
afterEach() is run after each test in a describe afterEach()describe 每个测试之后运行

Which one you want to use depends on your actual test. 您要使用哪一个取决于您的实际测试。

Now, for the long explanation. 现在,长期解释。 If you run mocha -R min on this: 如果你运行mocha -R min

describe("top", function () {
    before(function () {
        console.log("top before");
    });
    after(function () {
        console.log("top after");
    });
    beforeEach(function () {
        console.log("top beforeEach");
    });
    afterEach(function () {
        console.log("top afterEach");
    });
    it("test1", function () {
        console.log("top test1");
    });
    describe("sublevel", function() {
        before(function () {
            console.log("sublevel before");
        });
        after(function () {
            console.log("sublevel after");
        });
        beforeEach(function () {
            console.log("sublevel beforeEach");
        });
        afterEach(function () {
            console.log("sublevel afterEach");
        });
        it("test1", function () {
            console.log("sublevel test1");
        });
        it("test2", function () {
            console.log("sublevel test2");
        });
    });
    it("test2", function () {
        console.log("top test2");
    });
});

You'll see something like (I've omitted the output that is not relevant): 你会看到类似的东西(我省略了不相关的输出):

top before
top beforeEach
top test1
top afterEach
top beforeEach
top test2
top afterEach
sublevel before
top beforeEach
sublevel beforeEach
sublevel test1
sublevel afterEach
top afterEach
top beforeEach
sublevel beforeEach
sublevel test2
sublevel afterEach
top afterEach
sublevel after
top after

The thing that may be surprising if you look at what executes before and after each of the tests at the sublevel is that both the beforeEach callbacks at the top level and at the sublevel are called. 如果你看看之前和之后每个在分段测试的执行什么可奇怪的事情是, 无论beforeEach在顶层,并在次级回调被调用。 Same thing for the afterEach . afterEach也是afterEach

Some are also surprised by the sequence sublevel before , top beforeEach , sublevel beforeEach . 也有的由序列惊讶sublevel beforetop beforeEachsublevel beforeEach They think that all the hooks in an outer scope should execute before all the hooks in an inner scope, so they expect the sequence: top beforeEach , sublevel before , sublevel beforeEach . 他们认为外部作用域中的所有钩子都应该在内部作用域中的所有钩子之前执行,因此他们期望序列: top beforeEachtop beforeEach sublevel beforesublevel beforeEach However, the order in which Mocha executes the hooks makes complete sense: a before hook is meant to set the stage for a group of tests, whereas a beforeEach test is for each individual tests. 但是,Mocha执行钩子的顺序是完全beforeEachbefore钩子意味着为一组测试设置阶段,而beforeEach测试用于每个单独的测试。 When Mocha executes a test, all the before and the beforeEach hooks that were set in the describe that contains it, and all the ancestors of that describe apply to the test. 当摩卡执行测试,所有beforebeforeEach那是在设置挂钩describe包含它,和所有的祖先describe适用于测试。 Mocha will execute each before hook from the outermost scope to the innermost, and all beforeEach hook from the outermost scope to the innermost. before从最外层范围到最内层范围的钩子以及从最外层范围到最内层的所有beforeEach钩子before Mocha将执行每个操作。 However , all before hooks that apply are executed before any beforeEach hook. 但是 ,所有before挂钩之前都会在任何beforeEach挂钩之前执行。 This explains the order above: sublevel before executes before top beforeEach because it is a before hook. 这解释了上面的顺序:在top beforeEach sublevel before执行之前的top beforeEach因为它是before hook。 And with after and afterEach , the same logic applies but the the order is reversed: all afterEach hooks that apply are executed before any after hook. 并且使用afterafterEach ,相同的逻辑适用但顺序颠倒:所有适用的afterEach挂钩都after挂钩之前执行。

Also notice that Mocha does not care about how I ordered my it calls relative to the describe call in the top level describe . 还要注意,摩卡不关心我如何命令我it要求相对于describe呼叫在顶层describe It executes top test1 , top test2 and then the sublevel tests, even though the order I gave was top test1 , then the sublevel tests and then top test2 . 它执行top test1top test2 然后是sublevel测试,即使我给出的顺序是top test1 ,然后是sublevel测试然后是top test2

What you want to use among before , beforeEach , etc. really depends on the specifics of your tests. before想要使用的是什么, beforeEach等等,实际上取决于测试的具体情况。 If you need to setup a mock object or data structure and this object or structure can be reused by all the tests in a single describe , you can use before to set it up, and after to tear it down. 如果您需要设置一个模拟对象或数据结构和该物体或结构可以通过所有的测试在一个被重用describe ,您可以使用before对其进行设置,并且after将其摧毁。 This could be the case if you are doing read-only tests on the structure. 如果您对结构进行只读测试,则可能就是这种情况。 If all your tests only read it, then there is no need to create it over and over. 如果您的所有测试只读取它,那么就不需要反复创建它。 If each test in your describe needs a new copy of the structure because each test is modifying the structure then you should use beforeEach to create the structure anew for each test and then afterEach if you need to tear it down cleanly. 如果describe每个测试都需要一个的结构副本,因为每个测试都在修改结构,那么你应该使用beforeEach为每个测试重新创建结构,然后在每次测试之后如果你需要将它afterEach干净。 Doing this ensures test isolation: each test starts from a known state and does not depend on the presence or absence of a previous test to succeed. 这样做可确保测试隔离:每个测试都从已知状态开始,并且不依赖于先前测试的存在与否来成功。

before() call once when the test cases are executed Hardhat. before() 在执行测试用例时调用一次 Hardhat. 在此处输入图像描述

&& beforeEach() block executed every time when it block is executed in test cases Hardhat. && beforeEach() 块在测试用例 Hardhat 中每次执行块时执行。 #hardhat #ethereum #hardhat #ethereum 在此处输入图像描述

I used the same things for both functions you can see the difference between them...我对这两个函数使用了相同的东西,你可以看到它们之间的区别......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM