gpt4 book ai didi

javascript - 在 Mocha 中动态构建测试 - TypeError : test. 重试不是函数

转载 作者:行者123 更新时间:2023-12-03 07:19:49 24 4
gpt4 key购买 nike

我正在尝试遍历一些选项并根据 Mocha 中的这些选项构建测试。我已经建立了一个简单的概念证明,用于大致基于以下要点进行动态测试:https://gist.github.com/cybertk/fff8992e12a7655157ed

当我运行dynamicSuite.addTest()时,我不断收到错误:“TypeError: test.retries is not a function”。我无法弄清楚是什么导致了错误。似乎没有太多关于在 mocha 中构建测试的方法的文档。

代码如下:

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

最佳答案

测试的可读性很重要。考虑以编程方式生成测试是否会损害测试的可读性和/或增加测试包含错误的机会。

也就是说,这应该有效:

  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 block 中添加测试,该 block 将在文件中的每个测试中执行一次。因此,如果您在文件中添加另一个测试,所有测试都会重复。

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

关于javascript - 在 Mocha 中动态构建测试 - TypeError : test. 重试不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36255822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com