# 1.能够全局安装
npm install -g mocha
# 2.也能够在项目中安装
npm install -D mocha
复制代码
mocha
会默认执行当前路径的test/
目录下的全部测试文件# 1.若mocha是全局安装的
mocha
# 2.若mocha是在项目中安装的
node_module\mocha\bin\mocha
复制代码
在项目下新建一个
test/
目录,为默认的测试文件存放位置javascript
my_project/
—— hello.js // 含有待测试内容的文件
—— test/ // test/目录存放全部的测试文件
———— hello_test.js // 测试文件
———— ... // ...
—— package.json // 项目描述文件
—— node_modules/ // npm安装包
复制代码
一个测试文件的内容主要写法有三部分:java
describe(groupName, callback)
:测试分组,describe
能够多层嵌套使用it('testItemName', callback)
:测试项
callback
内无需传入参数callback
内须要传入一个参数,一般命名为done
断言
:对测试内容的判断,断言是否知足
assert
模块should.js
:should语法风格
(BDD
风格)的断言库expect.js
:expect语法风格
(简约BDD风格
)的断言库chai
:可自由在(assert()
、should()
、expect()
)三种风格进行选择的断言库better-assert
:~unexpected
: ~require
引入断言库依赖,而后便可在当前的测试文件中使用了test/
目录下添加一个mocha.opts
配置文件,在里面写入要使用的断言库,以后便可在全部的测试文件中直接使用了,例如:// test/mocha.opts
--require should
复制代码
// hello.js
module.exports = function (...rest) {
var sum = 0;
for (let n of rest) {
sum += n;
}
return sum;
};
复制代码
// test/hello.test.js
const assert = require('assert');
const sum = require('../hello');
describe('#hello.js', () => {
describe('#sum()', () => {
it('sum() should return 0', () => {
assert.strictEqual(sum(), 0);
});
it('sum(1) should return 1', () => {
assert.strictEqual(sum(1), 1);
});
it('sum(1, 2) should return 3', () => {
assert.strictEqual(sum(1, 2), 3);
});
it('sum(1, 2, 3) should return 6', () => {
assert.strictEqual(sum(1, 2, 3), 6);
});
})
})
复制代码
命令行输入mocha
开启测试,控制台输出以下的结果,说明编写的4个测试项所有经过了node
#hello.js
#sum()
✓ sum() should return 0
✓ sum(1) should return 1
✓ sum(1, 2) should return 3
✓ sum(1, 2, 3) should return 6
4 passing (7ms)
复制代码
mocha
为每一个测试组(describe
)提供了以下几个钩子函数:before(fn)
:fn
会在该组中的全部测试项被测试以前调用after(fn)
:fn
会在该组中的全部测试项被测试以后调用beforeEach(fn)
:fn
会在该组中每个测试项被调用以前调用afterEach(fn)
:fn
会在该组中每个测试项被调用以前调用describe('hooks', function() {
before(function() {
// runs before all tests in this block
});
after(function() {
// runs after all tests in this block
});
beforeEach(function() {
// runs before each test in this block
});
afterEach(function() {
// runs after each test in this block
});
// test cases
it('测试项1', () => {
...
})
it('测试项2', () => {
...
})
});
复制代码
// async.js
const fs = require('fs');
module.exports = async () => {
let expression =await fs.readFile('./data.txt', 'utf-8');
let fn = new Function('return ' + expression);
let r = fn();
console.log(`Calculate: ${expression} = ${r}`);
return r;
}
复制代码
// data.txt
1 + (2 + 4) * (9 - 2) / 3
复制代码
it(测试项名, callback(done) {...})
的callback
中须要传入一个done
参数
callback
内部手动调用done()
callback
内部手动调用done(err)
ps:针对不一样写法的异步函数,其对应的测试写法也有所不一样:express
// async-test.js
const assert = require('assert');
const hello = require('../hello.js');
describe('#async hello', () => {
describe('#asyncCalculate()', () => {
// 1.若是待测试的异步函数是回调函数语法的,则必须在回调函数中来进行测试判断(写法麻烦)
it('test async function', function(done) {
fs.readFile('filepath', function (err, data) {
if (err) {
done(err);
} else {
done();
}
});
})
// 2.若是待测试的异步函数是async/await 语法的,可经过try catch来测试判断(写法仍是比较麻烦)
it('#test async function', (done) => {
(async function () {
try {
let r = await hello();
assert.strictEqual(r, 15);
done();
} catch (err) {
done(err);
}
})();
});
// 3.待测试的异步函数是async/await语法的,最简单的写法——把async函数看成同步函数来测试!!!
it('test async function', async () => {
let r = await hello();
assert.strictEqual(r, 15);
})
})
})
复制代码
async\await
关键字,其在编写测试用例时很简单,基本跟同步函数的测试用例写法同样