单元测试
- TDD 测试驱动开发 倡导首先测试程序,然后编码实现其功能
- BDD 对测试开发的回应, 倡导软件项目开发者、测试人员和非技术人员进行协作,通过自然语言书写非程序员可度的测试用例扩展测试驱动开发方法。
Chai断言库
Chai 包含3个断言库,其中BDD风格的 Expect/Should和 TDD风格的Assert。
npm install chai --save
测试demo
- 使用 assert
const { assert } = require('chai');
const name = 'cc';
const stu = { name: 'cc', hobby: ['run', 'play basketball', 'play code'] };
assert.typeOf(name, 'string'); //判断name 是否是string
assert.typeOf(name, 'string', 'name is string'); //添加判断说明
assert.equal(name, 'c', `name equal 'cc'`); //判断name是不是等于 cc
assert.lengthOf(name, 2, 'name value has length of 2'); //判断name的字符串长度
assert.lengthOf(stu.hobby, 3, 'stu has 3 type of hobby'); //判读bobby数组的长度
- 使用expect
const {expect}=require('chai')
const name='cc';
const stu={hobby:['paly basketball']};
expect(name).to.be.a('string');
// expect(name).to.equal('cc');
expect(name).to.have.lengthOf(2);
expect(stu).to.have.property('hobby').with.lengthOf(2);
Mocha 使用
Mocka 是一个功能丰富且流行的Javascript测试框架。所谓测试框架就是运行测试的工具会接管单元测试代码的执行。
安装
npm i mocha -D
使用
- describe 和it
const { expect } = require('chai');
const name = 'cc';
describe('String', () => {
it('name should be a string', () => {
expect(name).to.be.a('String');
})
});
- 异步代码
//方式一、回调函数
describe('Asynchronous', () => {
it('done should be executed after 200s', done => {
const fn = () => {
expect(name).to.have.lengthOf(2);
done();
};
setTimeout(fn, 200);
})
});
//方式二、Promise
describe('Promise', () => {
it('use Promise', () => {
return new Promise(resolve => {
expect(name).to.equal('cc');
resolve();
})
})
});
//方式三、 使用 saync/await
describe('use async', () => {
it('use async', async () => {
var testPromise = ()=>new Promise((resolve) => {
setTimeout(() => {
resolve('cc');
}, 200);
});
const result = await testPromise();
expect(result).to.equal('cc');
});
});