2016-03-28 76 views
0

我想通过一些选项循环,并根据这些选项在摩卡生成测试。我已经建立了一个简单的动态测试概念证明,基于这个要点:https://gist.github.com/cybertk/fff8992e12a7655157ed在摩卡动态构建测试 - TypeError:test.retries不是函数

当我运行dynamicSuite.addTest()时,我总是收到错误:“TypeError:test.retries不是函数” 。我无法弄清楚是什么导致了错误。似乎没有太多关于摩卡建设测试方法的文档。

下面的代码:

var dynamicSuite = describe('dynamic suite', function() { 
this.timeout(10000); 

before(function (done) { 
    var a = ['a', 'b', 'c']; 
    for(let item of a){ 
     dynamicSuite.addTest(new common.Mocha.Test('test' + item, function(done){ 
     done(); 
     })); 
    } 
    done(); 
}); 

it('this is needed to make sure tests run', function (done) { 
    done(); 
}); 

after(function(done) { 
    done(); 
}); 
});//end describe test block 

回答

1

测试可读性是很重要的。考虑编程式生成测试是否会影响测试的可读性和/或增加测试包含错误的几率。

那说,这应该工作:

describe('dynamic suite', function(){ 
    ['a','b','c'].forEach(function(letter, index){ 
     it('should make a test for letter' + letter, function(done){ 
     // do something with letter 
     done(); 
     }); 
    }); 
    }); 

您目前在beforeEach块,将文件中的每个测试执行一次加入测试。所以如果你在文件中添加了另一个测试,所有的测试都会得到重复。

上面的代码工作是因为声明一个测试只是执行一个函数(it(name, test)),所以我们可以迭代每个输入并执行it函数。

+0

谢谢。我猜人们不会使用TDD风格? – mags