首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态运行Mocha测试

动态运行Mocha测试
EN

Stack Overflow用户
提问于 2015-09-29 15:55:30
回答 2查看 3K关注 0票数 6

我正在尝试动态运行一系列测试。我有以下设置,但它似乎没有运行,也没有收到任何错误:

代码语言:javascript
复制
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如下所示:

代码语言:javascript
复制
{ todo: 
  [ { name: 'POST /todos',
      should: 'create a new todo',
      method: 'POST',
      endpoint: '/todos',
      body: [Object] } ] }

(虽然在这一点上,我的测试只是试图检查一个基本的预期)

基于console.logs,迭代看起来很好,它似乎在添加测试,所以我对操作流程很有信心,我只是无法得到任何执行或错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-29 16:25:29

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

代码语言:javascript
复制
suite.addTest(new Test(test.name, () => {
    expect(1+1).to.equal(2);
}));

下面是我正在运行的全部代码,根据您的问题进行了修改:

代码语言:javascript
复制
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运行上面的内容时,我得到了输出:

代码语言:javascript
复制
  todo
    ✓ POST /todos


  1 passing (5ms)
票数 8
EN

Stack Overflow用户

发布于 2018-04-04 21:40:11

测试您的测试系统并确保它处理传递和失败的测试以及引发的异常是至关重要的。由于人们依赖于构建过程来警告他们错误,所以如果有任何错误,您还必须将退出代码设置为非零。下面是一个测试脚本(您必须使用node test.js而不是mocha test.js来调用它),它通过测试套件执行所有路径:

代码语言:javascript
复制
const Mocha = require('mocha')
const expect = require('chai').expect
var testRunner = new Mocha()
var testSuite = Mocha.Suite.create(testRunner.suite, 'Dynamic tests')

var tests = [ // Define some tasks to add to test suite.
  { name: 'POST /todos', f: () => true }, //              Pass a test.
  { name: 'GET /nonos',  f: () => false }, //             Fail a test.
  { name: 'HEAD /hahas', f: () => { throw Error(0) } } // Throw an error.
]

tests.forEach(
  test =>
    // Create a test which value errors and caught exceptions.
    testSuite.addTest(new Mocha.Test(test.name, function () {
      expect(test.f()).to.be.true
    }))
)
var suiteRun = testRunner.run() //             Run the tests
process.on('exit', (code) => { //              and set exit code.
  process.exit(suiteRun.stats.failures > 0) // Non-zero exit indicates errors.
}) // Falling off end waits for Mocha events to finish.

考虑到这在异步摩卡测试的web搜索中很突出,我将为人们提供更多有用的模板来进行复制。

嵌入式执行:第一个直接添加调用异步伪网络调用的测试,并在.then中检查结果。

代码语言:javascript
复制
const Mocha = require('mocha')
const expect = require('chai').expect
var testRunner = new Mocha()
var testSuite = Mocha.Suite.create(testRunner.suite, 'Network tests')

var tests = [ // Define some long async tasks.
  { name: 'POST /todos', pass: true, wait: 3500, exception: null },
  { name: 'GET /nonos', pass: false, wait: 2500, exception: null },
  { name: 'HEAD /hahas', pass: true, wait: 1500, exception: 'no route to host' }
]

tests.forEach(
  test =>
    // Create a test which value errors and caught exceptions.
    testSuite.addTest(new Mocha.Test(test.name, function () {
      this.timeout(test.wait + 100) // so we can set waits above 2000ms
      return asynchStuff(test).then(asyncResult => {
        expect(asyncResult.pass).to.be.true
      }) // No .catch() needed because Mocha.Test() handles them.
    }))
)
var suiteRun = testRunner.run() //             Run the tests
process.on('exit', (code) => { //              and set exit code.
  process.exit(suiteRun.stats.failures > 0) // Non-zero exit indicates errors.
}) // Falling off end waits for Mocha events to finish.

function asynchStuff (test) {
  return new Promise(function(resolve, reject) {
    setTimeout(() => {
//    console.log(test.name + ' on ' + test.endpoint + ': ' + test.wait + 'ms')
      if (test.exception)
        reject(Error(test.exception))
      resolve({name: test.name, pass: test.pass}) // only need name and pass
    }, test.wait)
  })
}

此代码处理传递和失败的数据,报告异常,并在出现错误时以非零状态退出。输出报告了所有预期的问题,并且还抱怨测试花费了相同的时间(3.5s):

代码语言:javascript
复制
  Network tests
    ✓ POST /todos (3504ms)
    1) GET /nonos
    2) HEAD /hahas
  1 passing (8s)
  2 failing

  1) Network tests GET /nonos:
      AssertionError: expected false to be true
      + expected - actual    
      -false
      +true

  2) Network tests HEAD /hahas:
     Error: no route to host

延迟执行:这种方法在填充和启动mocha测试套件之前调用所有慢任务:

代码语言:javascript
复制
const Mocha = require('mocha')
const expect = require('chai').expect
var testRunner = new Mocha()
var testSuite = Mocha.Suite.create(testRunner.suite, 'Network tests')

var tests = [ // Define some long async tasks.
  { name: 'POST /todos', pass: true, wait: 3500, exception: null },
  { name: 'GET /nonos', pass: false, wait: 2500, exception: null },
  { name: 'HEAD /hahas', pass: true, wait: 1500, exception: 'no route to host' }
]

Promise.all(tests.map( // Wait for all async operations to finish.
  test => asynchStuff(test)
    .catch(e => { // Resolve caught errors so Promise.all() finishes.
      return {name: test.name, caughtError: e}
    })
)).then(testList => // When all are done,
  testList.map( //     for each result,
    asyncResult => //  test value errors and exceptions.
      testSuite.addTest(new Mocha.Test(asyncResult.name, function () {
        if (asyncResult.caughtError) { // Check test object for caught errors
          throw asyncResult.caughtError
        }
        expect(asyncResult.pass).to.be.true
      }))
  )
).then(x => { //                                 When all tests are created,
  var suiteRun = testRunner.run() //             run the tests
  process.on('exit', (code) => { //              and set exit code.
    process.exit(suiteRun.stats.failures > 0) // Non-zero exit indicates errors.
  })
})

function asynchStuff (test) {
  return new Promise(function(resolve, reject) {
    setTimeout(() => {
//    console.log(test.name + ' on ' + test.endpoint + ': ' + test.wait + 'ms')
      if (test.exception)
        reject(Error(test.exception))
      resolve({name: test.name, pass: test.pass}) // only need name and pass
    }, test.wait)
  })
}

输出是相同的,除了摩卡没有抱怨缓慢的测试,而是相信测试工具不到10毫秒。Promise.all等待所有要解决或拒绝的承诺,然后创建测试来验证结果或报告异常。这比嵌入式执行要长几行,因为它必须:

  1. 解决异常,以便Promise.all()解决。
  2. 在最终的Promise.all().then()中执行测试

描述人们如何选择使用哪种风格的评论可以指导其他人。分享你的智慧!

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32848584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档