使用Nightwatchjs在运行时有条件地运行测试

时间:2016-05-27 14:29:13

标签: nightwatch.js

我正在使用nightwatch来运行我的端到端测试,但我想在运行时根据某些全局设置有条件地运行某些测试。

// globals.js
module.exports = {
    FLAG: true
};

// test.js
describe('Something', () => {
  it('should do something', client => {
      if (client.globals.FLAG) {
          expect(1).to.equal(1);
      }
  });
});

以上工作正常,但我想要对整个测试保持沉默并有条件地包括it例如:

// test.js
describe('Something', () => {
  // client does not exist out here so it does not work.
  if (client.globals.FLAG) {
      it('should do something', client => {
          expect(1).to.equal(1);
      });
  }
});

我知道我可以通过在nightwatch.js中定义它们并排除文件等来跳过测试,但这不是我可以在此实现中使用的方法。另一个解决方案可能是使用标签,但我不确定这是否可以使用Mocha。

1 个答案:

答案 0 :(得分:0)

您可以通过导入模块globals.js来访问第二个示例中的标记:

// test.js

const globals = require('../globals.js');

describe('Something', () => {

  if (globals.FLAG) {
      it('should do something', client => {
          expect(1).to.equal(1);
      });
  }
});

您还可以创建一个函数,以便在满足条件时忽略测试:

// test.js

const FLAG = require('../globals.js').FLAG;
const not = function(v){ return {it: v ? function(){}: it} };

describe('Something', () => {

  not(FLAG).it('should do something', client => {
      expect(1).to.equal(1);
  });

});
相关问题