【问题标题】:Is there a way to NOT execute beforeEach function only for certain tests ('it' blocks)有没有办法只为某些测试('it'块)执行 beforeEach 函数
【发布时间】:2020-03-12 19:11:28
【问题描述】:

有没有办法只为某些测试('it' 块)执行 beforeEach 函数。

我有一个量角器清理文件,其中包含应该在每个测试用例之后运行的 aftereach 函数。有没有办法在某些测试用例('it' 块)中不执行它们。

afterEach(() => {
    common.performance.captureMemory();
    replay.cleanReplay();
    dialog.cleanupDialog(); // Will also close search page source selector dialog if open
    pinboards.closeVizContextIfOpen();
    common.util.dismissNotificationIfPresent();
    formula.closeFormulaEditorIfOpen();
    common.util.ensureLoggedInAsDefaultUser();
    common.util.clearStickerSelection();
    common.util._uniqueNameSeed = -1;
});

I tried this:
global.defaultJasmineAfterEach = () => {
    common.performance.captureMemory();
    replay.cleanReplay();
    dialog.cleanupDialog(); // Will also close search page source selector dialog if open
    pinboards.closeVizContextIfOpen();
    common.util.dismissNotificationIfPresent();
    formula.closeFormulaEditorIfOpen();
    common.util.ensureLoggedInAsDefaultUser();
    common.util.clearStickerSelection();
    common.util._uniqueNameSeed = -1;
};

global.overrideAfterEachOnce = (fn) => {
    global.jasmineAfterEach = fn;
};

global.jasmineAfterEach = defaultJasmineAfterEach;

// This beforeEach block will run after every test, we use this to perform some cleanup that might
// be necessary before next test can run.
afterEach(() => {
    global.jasmineAfterEach();
    if (global.jasmineAfterEach !== global.defaultJasmineAfterEach) {
        global.jasmineAfterEach = global.defaultJasmineAfterEach();
    }
});```

Thanks in advance. :)

【问题讨论】:

  • 有一些可行的方法。每次您不想运行它时是否都进行相同的测试?大概需要多少测试?
  • 大约 8 到 10 个测试用例。所有这些情况都有从非角度页面到有角度页面的重定向。

标签: javascript jasmine protractor


【解决方案1】:

beforeEach 和 afterEach 的作用域在 "describe()" 内

describe('title', () => { 

    beforeEach(...)
    it(...)
    it(...)

})

所以也许你想在不同的“描述”上定义不同的“它”?

【讨论】:

  • “afterEach”位于量角器清理文件中,将为所有测试用例自动调用。我想要一种方法来忽略那些仅适用于 8 个测试用例的方法。
【解决方案2】:

我认为您应该查看this answer,但对我来说最简单的方法如下。 不要将它放在配置文件中,而是创建一个名为 afterEach.js 的单独文件

afterEach.js

module.exports = function () {
    afterEach(() => {
        console.log('afterEach complete')
    })
}

对于在描述块中每次使用 require('./afterAll')(); 后需要的任何规范

spec.js

describe('angularjs homepage todo list', function () {
  require('./afterEach')();

  it('should add a todo', function () {
    expect(true).toBe(false);

  });
  it('should add a todo2', function () {
    expect(true).toBe(true);

  });
});

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 2021-12-07
    • 2018-09-23
    • 2022-08-11
    • 2018-09-05
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多