使用Mocha在Nightwatch中进行测试

时间:2016-02-18 15:36:20

标签: javascript automated-tests mocha nightwatch.js

我使用Nightwatch和Mocha一起编写一些自动(动态)测试。动态,我想动态加载一些数据,用于循环测试用例。见下面的代码。我正在努力解决三个问题:

  1. 让我说我的testArray中有3个项目。当它在测试中循环时(它),它只打开一个浏览器,转到google.com并且不启动其他2个浏览器。我确定这与我遇到的一些异步问题有关,但我不知道如何解决它。
  2. 继续#1,第一个(也是唯一一个打开的浏览器)在结束时甚至没有关闭。使用done()。
  3. 的某些方面再次出现异步问题
  4. 理想情况下,我想在测试运行时动态命名测试。也就是说,而不是它(" foo" ...我想要围绕它循环并传递标题(我无法正常工作)或以某种方式改变它动态测试(it)代码。
  5. 请注意,我不需要将Mocha与Nightwatch一起使用,但我开始沿着使用Mocha动态测试功能(新TestCase)的路径开始,但我也无法实现这一功能。

    以下是我的代码的缩小版。

    var testArray = [];
    
    describe('createArray', function() {
      before(function(client, done) {
        // do some async operations within a loop and create testArray entries
        // loop {
            testArray.push(foo); // let's say I end up with 3 items.
        // }
        done();
      });
    
      it('foo', function(client) {
        console.log(testCaseArray);
        testCaseArray.forEach(function(testCase) {
          client.url("http://www.google.com"); // let's say here I would eventually want to have something like client.url("http://....." + testCase.value)
        });
      });
     });
    

    提前致谢。

1 个答案:

答案 0 :(得分:2)

你是对的,你需要异步处理测试用例。可以这样做:

it('foo', function(client, done) {
  var testsLeft = testCaseArray.length;
  function onTestComplete() {
    testsLeft--;
    if (testsLeft === 0)
      done();
  }
  testCaseArray.forEach(function(testCase) {
    client.url("http://" + testCase.value, onTestComplete);
  });
});

我不熟悉Nightwatch,所以你可能需要像这样使用onTestComplete

client.url("http://" + testCase.value).end(onTestComplete);

另外,我意识到这是处理异步回调的一种非常冗长的方式。通常对于这种情况,使用类似CallbackManager的内容会很有帮助,因此您无需手动跟踪剩余的测试数量。

更新

您还可以根据Mocha documentation动态生成测试。

相关问题