Nodejs 测试框架 Mocha

mocha

mocha 是一个简单、灵活有趣的 JavaScript 测试框架,用于 Node.js 和浏览器上的 JavaScript 应用测试。Mocha 可以持续运行测试,支持灵活又准确的报告,当映射到未捕获异常时转到正确的测试示例。

特性

1
2
3
4
5
6
7
8
9
10
11
支持浏览器
支持简单异步,包括 promises
测试覆盖报告
支持不同字符串比较
提供 javascript API 来运行测试
适当的退出状态,支持 CI
non-ttys 自动检测和禁用颜色
支持异步测试超时
支持 node debugger
TextMate 绑定
等等

安装

1
# npm install mocha -g

first example – Synchronous code

1
2
3
4
5
6
7
8
9
var assert = require("assert");
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
})
})
});

describe (moduleName, testDetails) 由上述代码可看出,describe是可以嵌套的,比如上述代码嵌套的两个describe就可以理解成测试人员希望测试Array模块下的#indexOf() 子模块。module_name 是可以随便取的,关键是要让人读明白就好。

it (info, function) 具体的测试语句会放在it的回调函数里,一般来说info字符串会写期望的正确输出的简要一句话文字说明。当该it block内的test failed的时候控制台就会把详细信息打印出来。一般是从最外层的describe的module_name开始输出(可以理解成沿着路径或者递归链或者回调链),最后输出info,表示该期望的info内容没有被满足。一个it对应一个实际的test case

assert.equal (exp1, exp2) 断言判断exp1结果是否等于exp2, 这里采取的等于判断是== 而并非 === 。即 assert.equal(1, ‘1’) 认为是True。这只是nodejs里的assert.js的一种断言形式,下文会提到同样比较常用的should.js。

如果exp1和exp2均为字符串,字符串比较出错时则控制台会用颜色把相异的部分标出来。

second example – Asynchronous Code

Frist example 中的代码显然是个 Synchronous 的代码,那么对于异步代码应该怎么做呢?很简单,在你最深处的回调函数中加done()表示结束。

1
2
3
4
5
6
7
8
9
10
11
fs = require('fs');
describe('File', function(){
describe('#readFile()', function(){
it('should read test.ls without error', function(done){
fs.readFile('test.ls', function(err){
if (err) throw err;
done();
});
})
})
})

done () 按照瀑布流编程习惯,取名done是表示你回调的最深处,也就是结束写嵌套回调函数。但对于回调链来说done实际上意味着告诉mocha从此处开始测试,一层层回调回去

third example – many tesks

这里可能会有个疑问,假如我有两个异步函数(两条分叉的回调链),那我应该在哪里加done()呢?实际上这个时候就不应该在一个it里面存在两个要测试的函数,事实上一个it里面只能调用一次done,当你调用多次done的话mocha会抛出错误。所以应该类似这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fs = require('fs');
describe('File', function(){
describe('#readFile()', function(){
it('should read test.ls without error', function(done){
fs.readFile('test.ls', function(err){
if (err) throw err;
done();
});
})
it('should read test.js without error', function(done){
fs.readFile('test.js', function(err){
if (err) throw err;
done();
});
})
})
});

Before && After

单元测试里经常会用到before和after。mocha同时还提供了beforeEach()和afterEach()。
这里为方便阅读用livescript表示,!->可理解成function(){}。细节无需细读,只需通过框架了解这几个函数如何使用便可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
});

Pending

即省去测试细节只保留函数体。一般适用情况比如负责测试框架的写好框架让组员去实现细节,或者测试细节尚未完全正确实现先注释以免影响全局测试情况。这种时候mocha会默认该测试pass。
作用有点像Python的pass。

1
2
3
4
5
6
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
})
})
});

Exclusive && Inclusive

分别对应only和skip函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
用法一
describe('Array', function() {
describe.only('#indexOf()', function() {
// ...
});
});
用法二
describe('Array', function() {
describe('#indexOf()', function() {
it.only('should return -1 unless present', function() {
// ...
});
it('should return the index when present', function() {
// ...
});
});
});

cofe-mocha

文章目录
  1. 1. mocha 是一个简单、灵活有趣的 JavaScript 测试框架,用于 Node.js 和浏览器上的 JavaScript 应用测试。Mocha 可以持续运行测试,支持灵活又准确的报告,当映射到未捕获异常时转到正确的测试示例。
  2. 2. 特性
  3. 3. 安装
  4. 4. first example – Synchronous code
  5. 5. second example – Asynchronous Code
  6. 6. third example – many tesks
  7. 7. Before && After
  8. 8. Pending
  9. 9. Exclusive && Inclusive
,