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

时间:2020-04-30 22:19:52

标签: automation nightwatch.js

我希望测试用例2不调用beaforeEach,但是测试用例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 <
  }
};

2 个答案:

答案 0 :(得分: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 <
  }
};

答案 1 :(得分:0)

如果您还没有很多测试用例,可以改变它们的结构。我编写测试的方式是,整个“文件”是1个测试用例,我用Jira中的TC编号(即IW-xxxx.js)命名,然后该TC中的每个块都是一个步骤。这将允许您禁用单个测试用例(即使您可以仅添加!function(browser)以跳过步骤),也可以使每个TC具有不同的钩子。这就是我在说的:

# Initialize list that will serve as a container for bind values
L = []

UpdateLog ('Load CSV file into List...')
reader = csv.reader(open(map_drive + ':\\'+ sp_file), delimiter=',')

# the code exludes the column headers on row
UpdateLog ('Skip Header Row...')
next(reader)

for row in reader:
    L.append(tuple(row))

# prepare insert statement
UpdateLog ('Creating INSERT Statement')
cursor.prepare(insert)


# execute insert with executemany
cursor.executemany(None, L)

# report number of inserted rows
UpdateLog ('Inserted: ' + str(cursor.rowcount) + ' rows into ' + table)
tmpRowCount = cursor.rowcount

cursor = connection.cursor()

#commit
connection.commit()
UpdateLog ('End import to '+ table)
相关问题