【问题标题】:Dynamically build tests in mocha - TypeError: test.retries is not a function在 mocha 中动态构建测试 - TypeError: test.retries is not a function
【发布时间】:2016-03-28 04:06:47
【问题描述】:

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

我在运行 dynamicSuite.addTest() 时不断收到错误:“TypeError:test.retries 不是函数”。我无法弄清楚是什么导致了错误。这种在 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

【问题讨论】:

    标签: javascript tdd mocha.js suite


    【解决方案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函数。

    【讨论】:

    • 谢谢。我猜人们只是不使用 TDD 风格?
    猜你喜欢
    • 2019-02-08
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2021-04-19
    • 2022-09-29
    • 2018-11-02
    相关资源
    最近更新 更多