Flu*_*yte 5 testing mocha.js node.js
我正在尝试动态运行一系列测试.我有以下设置,但它似乎没有运行,我没有收到任何错误:
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();
Run Code Online (Sandbox Code Playgroud)
在tests我跑这个样子的:
{ todo:
[ { name: 'POST /todos',
should: 'create a new todo',
method: 'POST',
endpoint: '/todos',
body: [Object] } ] }
Run Code Online (Sandbox Code Playgroud)
(虽然此时我的测试只是试图检查一个基本的期望)
基于console.logs迭代看起来很好,它似乎是添加测试,所以我对操作流程充满信心,我无法获得任何执行或错误.
您必须将测试函数传递给Test构造函数,而不是suite.addTest.因此,更改您的代码以添加您的测试,如下所示:
suite.addTest(new Test(test.name, () => {
expect(1+1).to.equal(2);
}));
Run Code Online (Sandbox Code Playgroud)
这是我正在运行的整个代码,根据您的问题改编:
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();
Run Code Online (Sandbox Code Playgroud)
当我运行上面的node_modules/.bin/babel-node test.es6,我得到输出:
todo
? POST /todos
1 passing (5ms)
Run Code Online (Sandbox Code Playgroud)
测试您的测试系统并确保它处理通过和失败的测试以及抛出的异常至关重要。由于人们依靠构建过程来警告他们有关错误,因此如果任何失败,您还必须将退出代码设置为非零。下面是一个测试脚本(你必须用node test.js而不是调用它mocha test.js),它在你的测试套件中运行所有路径:
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.
Run Code Online (Sandbox Code Playgroud)
鉴于这在异步 mocha 测试的网络搜索中很突出,我将提供一些更有用的模板供人们复制。
嵌入式执行:第一个直接添加调用异步虚假网络调用并检查结果的测试.then:
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)
})
}
Run Code Online (Sandbox Code Playgroud)
此代码处理传递和失败的数据,报告异常,并在出现错误时以非零状态退出。输出报告了所有预期的问题,并额外抱怨测试花费了类似的时间(3.5 秒):
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
Run Code Online (Sandbox Code Playgroud)
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)
})
}
Run Code Online (Sandbox Code Playgroud)
输出是相同的,除了 mocha 没有抱怨测试缓慢,而是相信测试工具少于 10 毫秒。在Promise.all所有的承诺等待解决或拒绝,然后创建一个测试,以验证结果或报告异常。这比嵌入式执行长几行,因为它必须:
Promise.all()解决了。Promise.all().then()描述人们如何选择使用哪种风格的评论可以指导他人。分享你的智慧!
| 归档时间: |
|
| 查看次数: |
1506 次 |
| 最近记录: |