NodeJS
代替人工进行功能测试
单元测试
代码示例:
// sum.js
module.exports = function (...rest) {
let sum = 0
for (let i of rest) {
sum += i
}
return sum
}
// test.js
const sum = require('./sum')
const assert = require('assert')
// 断言(成功时不会在控制台返回任何东西,只有错误时才会在控制台报错),第一个测试用例报错了会影响到后面的测试用例执行
assert.strictEqual(sum(), 0)
Mocha
测试文件放到根目录的test文件夹下,在终端执行mocha命令
代码示例:
// test.js
const sum = require('../sum')
const assert = require('assert')
describe('根组测试01', () => {
describe('组测试01', () => {
it('sum()的返回结果应该是:0', () => {
assert.strictEqual(sum(), 0)
})
it('sum(7)的返回结果应该是:7', () => {
assert.strictEqual(sum(7), 7)
})
it('sum(1, 2)的返回结果应该是:3', () => {
assert.strictEqual(sum(1, 2), 3)
})
})
describe('组测试02', () => {
})
})
describe('根组测试02', () => {
describe('组测试01', () => {
})
describe('组测试02', () => {
})
})
Chai
Assert
代码示例:
// test.js
const chai = require('chai')
const assert = chai.assert
describe('根组测试01', () => {
describe('组测试01', () => {
const value = 'hqsy'
assert.typeOf(value, 'string')
assert.equal(value, 'hqsy')
assert.lengthOf(value, 4)
})
})
// test.js
const sum = require('../sum')
const chai = require('chai')
const assert = chai.assert
describe('根组测试01', () => {
describe('组测试01', () => {
it('sum()的返回结果应该是:0', () => {
assert.equal(sum(), 0)
})
it('sum(7)的返回结果应该是:7', () => {
assert.equal(sum(7), 7)
})
it('sum(1, 2)的返回结果应该是:3', () => {
assert.equal(sum(1, 2), 3)
})
})
})
Should
代码示例:
// test.js
const sum = require('../sum')
const chai = require('chai')
chai.should()
describe('根组测试01', () => {
describe('组测试01', () => {
const value = 'hqsy'
value.should.exist.and.equal('hqsy').and.have.length(4).and.be.a('string')
// value.should.equal('hqsy')
// value.should.length(4)
// value.should.be.a('string')
})
})
// test.js
const sum = require('../sum')
const chai = require('chai')
chai.should()
describe('根组测试01', () => {
describe('组测试01', () => {
it('sum()的返回结果应该是:0', () => {
sum().should.exist.and.equal(0)
})
it('sum(7)的返回结果应该是:7', () => {
sum(7).should.exist.and.equal(7)
})
it('sum(1, 2)的返回结果应该是:3', () => {
sum(1, 2).should.exist.and.equal(3)
})
})
})
Expect
代码示例:
// test.js
const chai = require('chai')
const expect = chai.expect
describe('根组测试01', () => {
describe('组测试01', () => {
const value = 'hqsy'
const number = 7
expect(number).to.be.at.most(8) // 小于
expect(number).to.be.at.least(4) // 大于
expect(number).to.be.within(1, 8) // 之间
expect(value).to.exist // 是否存在
expect(value).to.be.a('string')
expect(value).to.equal('hqsy')
expect(value).to.not.equal('0721') // 不等于
expect(value).to.have.length(4)
})
})
// test.js
const sum = require('../sum')
const chai = require('chai')
const expect = chai.expect
describe('根组测试01', () => {
describe('组测试01', () => {
it('sum()的返回结果应该是:0', () => {
expect(sum()).to.equal(0)
})
})
})
异步环境
代码示例:
// test.js
const fs = require('fs')
const fsp = fs.promises
const chai = require('chai')
const assert = chai.assert
// describe('异步环境测试', () => {
// it('读取文件的内容是否正确', (done) => {
// fs.readFile('./hello.txt', 'utf8', (err, data) => {
// if(err) {
// done(err) // 主动抛出错误
// } else {
// assert.equal(data, 'hello')
// done() // 防止提前抛出
// }
// }) // 对应执行npm test命令的目录
// })
// })
describe('异步环境测试', () => {
it('读取文件的内容是否正确', async () => {
const data = await fsp.readFile('./hello.txt', 'utf8')
assert.equal(data, 'hello')
})
})
HTTP请求
代码示例:
// test.js
// const axios = require('axios')
// const chai = require('chai')
// const assert = chai.assert
//
// describe('测试接口', () => {
// it('返回HTML代码片段', async () => {
// const res = await axios.get('http://localhost:3000/')
// assert.equal(res.data, `<h1>h1</h1>`)
// })
// })
// test.js
const supertest = require('supertest')
const app = require('../app')
describe('测试接口', () => {
const server = app.listen(3000)
it('返回HTML代码片段', async () => {
await supertest(server)
.get('/')
.expect('Content-Type', /text\/html/)
.expect(200, `<h1>h1</h1>`)
})
after(() => { // mocha自带的钩子函数(测试结束后)
server.close()
})
})
钩子函数
代码示例:
// test.js
const supertest = require('supertest')
const app = require('../app')
describe('测试接口', () => {
let server
it('返回HTML代码片段', async () => {
await supertest(server)
.get('/')
.expect('Content-Type', /text\/html/)
.expect(200, `<h1>h1</h1>`)
})
before(() => { // 测试开始前
server = app.listen(3000)
})
after(() => {
server.close()
})
// 以下两个钩子函数放在根测试组里面会应用在每个测试组的用例,放在测试组里面仅应用在测试组中的用例
beforeEach(() => { // 测试用例开始之前
console.log('beforeEach')
})
afterEach(() => { // 测试用例结束时
console.log('afterEach')
})
})