以前項目開發由於改進度,基本都是粗放式開發。爲了提升代碼質量,單元測試是必不可少的。node
針對restful api ,用supertest 測試框架。針對nodejs,引入mocha 和should 能夠方便編寫單元測試。web
首先談談supertest,它封裝了mocha和expect 模塊。用法也比較簡潔,例子:express
var request = require('supertest'); var express = require('express'); let should=require('should'); var app = require('../app'); var port = process.env.PORT || 3000; app.set('port', port); describe('supertest 驗證', function() { beforeEach(function() { console.log('before every test in every suit'); }); it('get 請求', function(done) { request(app) .get('/') .set('Cache-control', 'no-cache') .expect(200, done); }); it('Post 請求', function(done) { request(app) .post('/sayHello') .set('Content-Type', 'application/json') .send({ username:'ryan', password:'chen' }) .expect('Content-Type', /json/) .expect(200,{message:"ryan你好"}, done); }); });
如上代碼所示,總體看是比較easy清爽的。能夠設置header和對比返回值,至於token的驗證,在測試時根據環境變量取消便可。json
should 是個好同志,能夠幫助咱們方便比較。至於用法,以下:api
1 describe('should test', function () { 2 "use strict"; 3 beforeEach(function () { 4 }); 5 it('ok', function (done) { 6 true.should.be.ok; 7 (5).should.be.exactly(6).and.be.a.number; 8 done(); 9 }); 10 it('true',function (done) { 11 true.should.be.true; 12 'true'.should.not.be.true; 13 done(); 14 }); 15 //content compare,not reference 16 it('eql', function () { 17 ({ foo: 'bar',num:1 }).should.eql({ foo: 'bar',num:1}); 18 //[1, 2, 3].should.eql({ '0': 1, '1': 2, '2': 3 }); 19 20 }); 21 //to be exactly, 22 it('equal',function () { 23 (4).should.equal(4); 24 'test'.should.equal('test'); 25 [1,2,3].should.be.not.exactly([1,2,3]); 26 }); 27 //>= or <= 28 it('within', function () { 29 (5).should.be.within(5, 10).and.within(5, 5); 30 (5).should.be.above(0); 31 }); 32 //.Infinity 33 it('infinity', function () { 34 (1/0).should.be.Infinity; 35 }); 36 //instanceof 37 it('instanceof', function () { 38 let ball=new Ball(11,"red"); 39 ball.should.be.an.instanceOf(Ball); 40 [].should.be.an.instanceof(Array); 41 }); 42 43 it('properties',function () { 44 let ball=new Ball(11,"red"); 45 46 ball.should.have.properties('color'); 47 ball.should.have.properties({size:11,color:"red"}); 48 }); 49 it('empty', function () { 50 [].should.be.empty; 51 ''.should.be.empty; 52 ({}).should.be.empty; 53 (function() { 54 arguments.should.be.empty; 55 })(); 56 }); 57 it('containEql', function () { 58 'hello boy'.should.containEql('boy'); 59 [1,2,3].should.containEql(3); 60 [[1],[2],[3]].should.containEql([3]); 61 [[1],[2],[3, 4]].should.not.containEql([3]); 62 }); 63 //regex 64 it('regex or function', function () { 65 ['a', 'b', 'c'].should.match(/[a-z]/); 66 (5).should.match(function(n) { return n > 0; }); 67 (5).should.not.match(function(n) { return n < 0; }); 68 }); 69 it('match each', function () { 70 [10, 11, 12].should.matchEach(function(it) { return it >= 10; }); 71 }); 72 73 it('exception', function () { 74 (function(){ 75 throw new Error('fail'); 76 }).should.throw(); 77 }); 78 79 it('status',function () { 80 //res.should.have.status(200); 81 //res.should.have.header('Content-Length', '123'); 82 //res.should.be.json 83 84 }) 85 86 }); 87 88 class Ball 89 { 90 constructor(size,color) 91 { 92 Object.assign(this,{size,color}); 93 } 94 }
能夠在控制檯,用mocha 命令 測試,會默認找項目根目錄下test文件夾,因此單元測試要放到此文件下。執行後,會測試當前文件夾下全部的單元測試:restful
開發工具若是用的是webstorm,有個比較方便的插件。可在插件裏面搜 nodejs,安裝後,可在啓動配置中添加mocha,app
配置後,直接啓動框架
能夠看到,這樣的話能夠選擇其中某個測試用例測試,比輸入腳本方便了好多。bingowebstorm