是否可以忽略单个测试的beforeEach?

问题描述 投票:0回答:1

我想让测试用例2不调用beforeEach,但测试用例1和3仍然调用beforeEach,这可能吗?

我使用的是NightWatch.js。

module.exports = {

  before(browser) {
    // > this will get run only ONCE, before all the tests <
  },
  beforeEach(browser) {
    // > this will get run before every test case <
  }

  tags: ['your', 'tags', 'go', 'here'],
  'Test Case No.1': (browser) => {
     // > this test does something here <
  },
  'Test Case No.2': (browser) => {
     // > this test does something else here <
  },
  'Test Case No.3': (browser) => {
     // > this test does something else here <
  },

  afterEach(browser) {
    // > this will get run after every test case <
  },
  after(browser) {
    // > this will get run ONCE, after all tests have run <
  }
};
automation nightwatch.js
1个回答
0
投票

欢迎来到StackOverflow!

你无法避免调用特定测试的钩子,但你可以通过一些条件选择跳过钩子里面的代码。当测试用例2运行时,下面的代码将不会在每个钩子之前执行代码。

module.exports = {

  before(browser) {
    // > this will get run only ONCE, before all the tests <
  },
  beforeEach(browser) {
    if (browser.currentTest.name !== 'Test Case No.2') {
      // your code
    }
  }

  tags: ['your', 'tags', 'go', 'here'],
  'Test Case No.1': (browser) => {
    // > this test does something here <
  },
  'Test Case No.2': (browser) => {
    // > this test does something else here <
  },
  'Test Case No.3': (browser) => {
    // > this test does something else here <
  },

  afterEach(browser) {
    // > this will get run after every test case <
  },
  after(browser) {
    // > this will get run ONCE, after all tests have run <
  }
};

0
投票

如果你还没有很多测试用例,你可以改变你的测试用例的结构。 我写测试的方式是,整个 "文件 "是1个测试用例,我以Jira中的TC号命名(即IW-xxxx.js),然后该TC中的每个块是一个步骤。 这将允许你禁用单个测试用例(尽管你可以直接添加 !function(browser)来跳过一个步骤),也允许你为每个TC设置不同的钩子。 这就是我所说的。

module.exports = {
   '@tags': ['nameOfTag'],

    before: function(browser){},

    beforeEach: function(browser){},

    after: function(browser){},

    afterEach: function(browser){},

    'Step 1': function(browser) {
         //code here for step 1
    },
    'Step 2': function(browser) {
        //code for step 2
    },
    'Make your verification/assertion': function(browser) {
        // assertion
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.