动态运行Mocha测试 [英] Dynamically Running Mocha Tests

查看:70
本文介绍了动态运行Mocha测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态运行一系列测试.我有以下设置,但它似乎无法运行,并且没有出现任何错误:

I'm trying to run a series of tests dynamically. I have the following setup but it doesn't seem to run and I'm not getting any errors:

import Mocha from 'mocha';
const Test = Mocha.Test;
const Suite = Mocha.Suite;
const mocha = new Mocha();
for (let s in tests) {
  let suite = Suite.create(mocha.suite, s);
  tests[s].forEach((test) => {
    console.log('add test', test.name)
    suite.addTest(new Test(test.name), () => {
      expect(1+1).to.equal(2);
    });
  });
}
mocha.run();

我正在运行的tests看起来像这样:

The tests I'm running look like this:

{ todo: 
  [ { name: 'POST /todos',
      should: 'create a new todo',
      method: 'POST',
      endpoint: '/todos',
      body: [Object] } ] }

(尽管此时我的测试只是试图检查基本期望)

(though at this point my test is just trying to check a basic expect)

基于console.logs,迭代看起来很好,并且似乎正在添加测试,因此我对操作流程充满信心,我只是无法获得任何执行或错误.

Based on the console.logs the iteration seems fine and it appears to be adding the tests, so I'm confident in the flow of operations, I just can't get any execution or errors.

推荐答案

您必须将测试函数传递给Test构造函数,而不是传递给suite.addTest.因此,更改代码以添加测试,如下所示:

You have to pass the test function to the Test constructor, not to suite.addTest. So change your code to add your tests like this:

suite.addTest(new Test(test.name, () => {
    expect(1+1).to.equal(2);
}));

这是我正在运行的全部代码,根据您的问题改编而成:

Here is the entire code I'm running, adapted from your question:

import Mocha from 'mocha';
import { expect } from 'chai';
const Test = Mocha.Test;
const Suite = Mocha.Suite;
const mocha = new Mocha();

var tests = { todo:
  [ { name: 'POST /todos',
      should: 'create a new todo',
      method: 'POST',
      endpoint: '/todos',
      body: [Object] } ] };

for (let s in tests) {
  let suite = Suite.create(mocha.suite, s);
  tests[s].forEach((test) => {
      console.log('add test', test.name);
      suite.addTest(new Test(test.name, () => {
          expect(1+1).to.equal(2);
      }));
  });
}
mocha.run();

当我使用node_modules/.bin/babel-node test.es6运行以上命令时,我得到的输出是:

When I run the above with node_modules/.bin/babel-node test.es6, I get the output:

  todo
    ✓ POST /todos


  1 passing (5ms)

这篇关于动态运行Mocha测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆