1.首先用node啟動(dòng)一個(gè)后臺(tái)服務(wù)井联,安裝包express
npm install express --save
新建文件app.js寫(xiě)入如下代碼:
var express = require("express");
var app = new express();
app.get("/test",function(req,res){
res.send({
data:"hello world"
})
});
var server = app.listen(3000,function(){
console.log("server start");
})
module.exports = server;
2.新建文件router.spec.js切省,下載包axios
npm install axios --save
const axios = require('axios');
const {expect} = require("chai");
describe("后臺(tái)接口測(cè)試",function(){
it('test接口',(done) => {
axios.get('http://localhost:3000/test')
.then(function (res) {
expect(res.status).to.equal(200);
if(res.data.data == 'hello world'){
done();
}else{
done(new Error("結(jié)果不符合預(yù)期"));
}
})
.catch(function(error){
done(error);
})
})
})
npm install mocha --save
npm install mochawesome --save
npm install chai --save
新建mochaRunner.js
const Mocha = require("mocha");
const mocha = new Mocha({
reporter: 'mochawesome',
reporterOptions: {
reportDir: './docs/mochawesome-reporter',
}
})
mocha.addFile("./router.spec.js");
mocha.run(function(){
console.log("done");
process.exit();
})
4.node ./service/mochaRunner.js