当前位置:网站首页>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 ");
})
})边栏推荐
- ECMA 262 12 Lexical Grammer
- We media people must have four resource tools, each of which is very practical
- 1000 okaleido tiger launched binance NFT, triggering a rush to buy
- Binder principle
- Domain oriented model programming
- ECMA 262 12 Lexical Grammer
- ribbon 执行逻辑源码解析
- Platform architecture construction
- 【集训DAY15】简单计算【树状数组】【数学】
- Force deduction solution summary 919 complete binary tree inserter
猜你喜欢
![[training Day12] be go! [dynamic programming] [mathematics]](/img/63/689c17a0aae22ba25600b136178bf6.png)
[training Day12] be go! [dynamic programming] [mathematics]

MatrixCube揭秘102——300行实现的完整分布式存储系统MatrixKV

Qtreewidget control of QT

【自然语言处理】【向量表示】AugSBERT:改善用于成对句子评分任务的Bi-Encoders的数据增强方法

Kibana~ the process number cannot be found after kibana is started in the background

LabVIEW develops PCI-1680U dual port can card
![[training day15] paint road [minimum spanning tree]](/img/12/2d4ad1e2b8133b6c92875faa4b4182.png)
[training day15] paint road [minimum spanning tree]

Vodak software: Smart City solution

Xiaobai programmer's sixth day

自媒体人必备的4个素材网站,再也不用担心找不到素材
随机推荐
QT log file system
Can generic types be used in array
编译器引论
ML-Numpy
Qt中文编程遇C2001错误,提示“常量中有换行符”
C语言逆序打印字符串的两种方法
IPv4地址已经完全耗尽,互联网还能正常运转,NAT是最大功臣!
Anaconda~Upload did not complete.
字符型常量和字符串常量的区别?
Two methods of printing strings in reverse order in C language
Interview question 17.11. word distance ●●
[training day15] good name [hash]
【集训DAY13】Internet【并查集】
Why should we launch getaverse?
【集训DAY12】Bee GO!【动态规划】【数学】
【集训DAY13】Out race【数学】【动态规划】
Vodak software: Smart City solution
Matrix of C language
[database learning] redis parser & single thread & Model
Force deduction solution summary 919 complete binary tree inserter