如何在javascript / node中动态生成测试用例?
鼻子测试框架(用于python)支持dynamically generating test cases at run-time(以下,从文档中,导致五个不同的测试用例):
def test_evens():
for i in range(0, 5):
yield check_even, i, i*3
def check_even(n, nn):
assert n % 2 == 0 or nn % 2 == 0
如何使用jocha框架(如mocha或qunit)实现此结果? (此时我不依赖于任何特定的框架。)
我的用例是编写一个测试运行器来监视外部服务器上的几个项目。我会提供一个资源URL列表。每个测试都会尝试轮询该资源,并根据它找到的内容返回成功或失败。我有一个用python构建的原型(使用nose)但是如果可以的话我想在node.js中实现。最终,这将包含在CI设置中。
回答如下:是的,您可以使用Mocha动态创建包含案例的测试套件。我已经安装了mocha全球npm install -g mocha
,我使用should。
var should = require('should');
var foo = 'bar';
['nl', 'fr', 'de'].forEach(function(arrElement) {
describe(arrElement + ' suite', function() {
it('This thing should behave like this', function(done) {
foo.should.be.a.String();
done();
});
it('That thing should behave like that', function(done) {
foo.should.have.length(3);
done();
});
});
});