当前位置:网站首页>Mocha test
Mocha test
2022-07-25 22:43:00 【Cabbage 00】
Catalog
mocha The main characteristics of
About node.js Built in module assertion
mocha Of http Test and hook functions
unit testing
Unit testing is used for a module , A function or a class to test correctness ( Generally, testing is often used when modifying code )
The test case : Cases used in the test
Be careful : When more and more test cases pass the test , The more you can prove the reliability of this test
mocha
mocha yes js A unit testing framework of , It can run in browser environment , It can also be in node.js Operation in environment
Be careful : Use mocha, We just need to focus on writing unit tests themselves , And then let mocha To run all tests automatically , And the test results are given
mocha The main characteristics of
- It can test simple js function , You can also test asynchronous code , Because asynchrony is js One of the characteristics of
- All tests can be run automatically , You can also run only specific tests
- Can support before、after、beforeEach、afterEach To write initialization code
Write tests
node.js Built-in module assert No installation required
Built-in module assert Use
introduce assert:var assert=require("assert")
assert.strictEqural()
grammar : assert.strictEqural( Test results , Expected results )
effect : If the test result is the same as the expected result, it is successful , If it is different, it will fail , Success without any prompt , If you fail, you will report an error
function sum(...rest){
var sum=0
for(var one of rest){
sum+=one
}
return sum
}
// node.js Built-in module , Assertion module
var assert=require("assert")
assert.strictEqual(sum(),0)// Strict equality method
assert.strictEqual(sum(1),1)
assert.strictEqual(sum(1,2),3)About node.js Built in module assertion
- It can assert whether our results and expected results are the same , If it is the same, no error will be reported , If it's different, it's wrong
- No result will appear after the assertion is successful , Unless the assertion goes wrong
- Once the first test case goes wrong , It will directly affect the execution of subsequent test cases
Be careful :mocha Many test cases can be combined for unified execution , And there are good test results
mocha Use
Preface
npm initialization :npm init -y
install mocha modular :npm i mocha
1. establish test Folder will drag all the files that need to be tested into test In the folder
2. take package.json In the document scripts In script file test Of value Value changed to mocha
"scripts": {
"test": "mocha"
}3. Output in the current project console npm run test To perform tests , After execution, it will automatically scan the current project test Folder , Find it and put it inside js Files are executed one by one
keyword
- describe: Specify a set of tests , Can be nested
- it: Specify individual tests
function sum(...rest) {
var sum = 0
for (var one of rest) {
sum += one
}
return sum
}
// node.js Built-in module , Assertion module
var assert = require("assert")
describe(" Big group 1 test ", () => {
describe(" Small group 1 test ", () => {
it("sum() 0", () => {
assert.strictEqual(sum(), 0)// The test case
})
})
describe(" Small group 2 test ", () => {
it("sum(1) 1", () => {
assert.strictEqual(sum(1), 2)
})
it("sum(1,2) 3", () => {
assert.strictEqual(sum(1, 2), 3)
})
})
})
describe(" Big group 2 test ", () => {
})
Be careful :descripe There must be it Single test , however it A single test can exist alone
chai Assertions library
Use
npm initialization :npm init -y
install chai modular :npm i chai
introduce chai modular :var chai=require("chai")
assert style
var chai=require("chai")
var assert=chai.assert
describe("test2 Large group within ",()=>{
it("assert unit 1",()=>{
var value="hello"
assert.typeOf(value,"string")// monitoring value Is it right? string type
assert.equal(value,"hello")// testing value Is the value equal to hello
assert.lengthOf(value,5)// testing value Is the length equal to 5
})
})should style
var chai=require("chai")
chai.should()
describe("test3 Large group within ",()=>{
it("should unit 1",()=>{
var value="hello"
value.should.exist.and.equal("hello").and.have.length(5).and.be.a("string")
// Read from head to back
})
})expect style
var chai=require("chai")
var expect=chai.expect
describe("test4 Large group within ",()=>{
it("expect unit 1",()=>{
var value="hello"
var number=3
expect(number).to.be.at.most(5)// The expected result is less than 5
expect(number).to.be.at.least(3)// The expected result is greater than 3
expect(number).to.be.at.within(1,4)// The expected result is 1 and 4 Between
expect(value).to.exist// expect value There is
expect(value).to.be.a("string")// expect value yes string type
expect(value).to.equal("hello")// expect value by hello
expect(value).to.not.equal(" Hello! ")// expect value Not for your good
expect(value).to.have.length(5)// expect value The length is 5
})
})Asynchronous test
done() The role of : Tell the test case not to rush to the result ,done Then it means that the test case has been executed
const fs=require("fs")
const assert=require("assert")
// The way 1
describe(" Asynchronous test 1",()=>{
// Be careful : Asynchronously reading files requires adding done Parameters
it(" Asynchronous read file ",(done)=>{
fs.readFile("./1.txt","utf8",(err,data)=>{
if(err){
done(err)
}else{
assert.strictEqual(data,"hello")// test 1.txt Is the content of the document in hello
done()
}
})
})
})
// The way 2
const fsp=fs.promises// Back to fsp by promise object
describe(" Asynchronous test 2",()=>{
it(" Asynchronous read file ",async ()=>{
var data= await fsp.readFile("./1.txt","utf8")
assert.strictEqual(data,"hello")// test 1.txt Is the content of the document in hello
})
})mocha Of http Test and hook functions
install supertest modular :npm i supertest
//server.js In the file
var http=require("http")
var app=http.createServer((req,res)=>{
res.end("hello")
})
module.exports=appvar supertest=require("supertest")
const app = require("../server")
describe(" Test interface 1",()=>{
var server
it(" Return to the fragment test ",async ()=>{
await supertest(server).get("/")
.expect(200,"hello")
})
// Hook function mocha Bring their own
//before Represents the function executed at the beginning of this set of tests
before(()=>{
server=app.listen(3000)
})
//after Indicates when the test group ends , Complete all test cases of this group , Is executed after Method
after(()=>{
server.close()
})
// Functions executed before the start of each test case
beforeEach(()=>{
console.log(" The test case begins ");
})
// The function executed after the end of each test case
afterEach(()=>{
console.log(" The test case is over ");
})
})边栏推荐
- [training day15] paint road [minimum spanning tree]
- Data quality: the core of data governance
- Interpretation of English terms
- [training Day12] min ratio [DFS] [minimum spanning tree]
- [training Day12] tree! Tree! Tree! [greed] [minimum spanning tree]
- 【PMP学习笔记】第1章 PMP体系引论
- Platform architecture construction
- [training Day12] be go! [dynamic programming] [mathematics]
- 【自然语言处理】【向量表示】AugSBERT:改善用于成对句子评分任务的Bi-Encoders的数据增强方法
- JD quick navigation box
猜你喜欢

Data governance under data platform
![[training Day12] x equation [high precision] [mathematics]](/img/4f/51d902e925f9ec60da46d161ed4d17.png)
[training Day12] x equation [high precision] [mathematics]

Pyspark data analysis basis: pyspark.sql.sparksession class method explanation and operation + code display

【集训DAY15】简单计算【树状数组】【数学】

Von Neumann architecture

【集训DAY13】Backpack【动态规划】【贪心】
![[training day13] Internet [concurrent search]](/img/c6/327095c3ed3a0d4e2b034ff164a7af.png)
[training day13] Internet [concurrent search]

Xiaobai programmer's sixth day

JSON object

新媒体运营策略(以小红书为例)帮助你快速掌握爆款创作方法
随机推荐
PE格式: 分析IatHook并实现
自媒体人必备的4个素材网站,再也不用担心找不到素材
Explore the use of self increasing and self decreasing operators
According to the use and configuration of data permissions in the open source framework
Kibana~后台启动Kibana之后无法找到进程号
【Leetcode】502.IPO(困难)
Data governance under data platform
1000 okaleido tiger launched binance NFT, triggering a rush to buy
Gan, why '𠮷 𠮷'.Length== 3 ??
Can generic types be used in array
MatrixCube揭秘102——300行实现的完整分布式存储系统MatrixKV
Ribbon execution logic source code analysis
数据质量:数据治理的核心
Short circuit and &, short circuit or |, logic and &, logic or |; Conditional operator
torchvision
Xiaobai programmer's sixth day
QT的Tree View Model示例
[training day13] out race [mathematics] [dynamic planning]
面试题 17.11. 单词距离 ●●
[training day15] boring [tree DP]