【问题标题】:Where to put `beforeEach` for all test cases?将所有测试用例的“beforeEach”放在哪里?
【发布时间】:2020-06-08 04:10:50
【问题描述】:

这两个测试有什么区别:

beforeEach(doSomething)

describe("i am the only root describe in this test file", () => {
    // many test cases...
})
describe("i am the only root describe in this test file", () => {
    beforeEach(doSomething)
    // many test cases...
})

【问题讨论】:

  • Describe 为测试创建一个闭包。 BeforeEach 将在所有这些内部测试的每个测试之前运行。在第一个例子中,写 beforeEach 会执行一次(虽然不确定)

标签: javascript unit-testing mocha.js


【解决方案1】:

如果将beforeEach 放在描述中,它会在此块中的每个测试之前运行。如果把它放在根级别,它会在所有测试文件中的所有测试之前运行。

查看https://github.com/demo-drive-learn/mocha-root-pre-hook,或查看此demo

【讨论】:

    猜你喜欢
    • 2011-03-04
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2014-02-04
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多