mocha默认加载test目录下的test.js,也能够经过参数指定须要运行的测试文件,如运行test目录下的test.math.js。html
mocha test/test.math.js
mocha的测试脚本node
describe('test of math', function () { it('should return 2 when 1 + 1', function () { assert.equal(math.add(1, 1), 2); }); });
上面使用的是nodejs的assert断言库,也能够安装其余断言库,如npm
mocha容许在test目录下经过mocha.opts文件,进行参数配置。
mocha.optsasync
--timeout 5000 --reporter mochawesome
控制台显示的测试结果:测试
test of math ✓ should return 2 when 1 + 1 ✓ use expect assertion: should return 2 when 1 + 1 ✓ asyncAdd, should return 2 when 1 + 1 (2004ms) - a pending test 3 passing (2s) 1 pending
--reporter参数能够配置使用的测试报表名称,如spec, dot, nyan, mochawesomeui
mochawesome须要安装this
npm install --save-dev mochawesome
报表生成在mochawesome-report目录下,为html文件。code
Istanbulhtm
npm install --save-dev nyc
在mocha命令前增长nycip
nyc mocha test/test.math.js
运行改命令后:
----------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ----------|----------|----------|----------|----------|-------------------| All files | 100 | 100 | 100 | 100 | | math.js | 100 | 100 | 100 | 100 | | ----------|----------|----------|----------|----------|-------------------|
如须要生成代码覆盖率的报告,能够修改命令为:
nyc --reporter=html mocha test/test.math.js
运行命令后,覆盖率报表会生成在coverage目录下。
var assert = require('assert'); var expect = require('expect.js'); var Math = require('../math'); var math; describe('test of math', function () { //hooks before(function () { // runs before all tests in this block math = new Math(); }); 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 }); it('should return 2 when 1 + 1', function () { assert.equal(math.add(1, 1), 2); }); it('should return 1 when 1 * 1', function () { assert.equal(math.mutiply(1, 1), 1); }); it('should return max value 9 of [1,3,6,9,0]', function () { assert.equal(math.max([1, 3, 6, 9, 0]), 9); }); it('should return 2 when 1 + 1', function () { assert.equal(math.add(1, 1), 2); }); it('use expect assertion: should return 2 when 1 + 1', function () { expect(math.add(1, 1)).to.be(2); }); it('asyncAdd, should return 2 when 1 + 1', function (done) { math.asyncAdd(1, 1).then(function (result) { assert.equal(result, 2); done(); }, function (err) { assert.fail(err); done(); }); }); it('a pending test'); });