BDD - 行为驱动开发
Behavior - 行为
Driven - 驱动
Develop - 开发
TDD - 测试驱动开发
test - 测试
Driven - 驱动
Develop - 开发
Behavior - 行为
Assert - 断言
should
expect
assert
单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作
可以理解为对功能的基本验证
目前node中的测试框架,一般使用的是 mocha + 断言库 chai
安装
npm install mocha -g
npm install mocha
npm install chai
mocha API
describe
describe 是一个 用例测试集, 他可以进行嵌套
describe('进行首页的测试', function() {
// ....
})
it
一个it对应一个单元测试用例
it('测试接口xxx', function() {
// ....
})
only skip
only – 在当前的父describe块下,只执行该单元的测试
skip – 在当前的父describe块下,跳过该单元的测试
describe('Array', function() {
describe.only('父describe块下只执行该测试单元', () => {
it.skip('跳过的测试单元', () => {
});
})
})
describe 和 it 都可以使用这两个方法
timeout - 设超时
测试集合上定义超时时间,会对这个测试集合中所有的测试用例和测试集合起作用
const sleep = time => new Promise(resolve => setTimeout(resolve, time))
it('timeout', async function () {
this.timeout(1000)
await sleep(3000)
expect(true).to.be.ok
})
hooks
提供了几个函数,在特定的事件发生时被触发
before()、after()、beforeEach()、afterEach()
同一个describe下的执行顺序为before、beforeEach、afterEach、after
before, after 执行一次
beforeEach,afterEach 每一个测试用例都会触发一次
before(() => console.info('首页测试开始'))
after(() => console.info('首页测试结束'))
beforeEach('check check check ', function() {
console.log('i am check')
})
chai API
chai有三种断言风格,expect,should,assert, 我的项