# 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
關鍵字,其在編寫測試用例時很簡單,基本跟同步函數的測試用例寫法同樣