nodejs webapi单元测试

目录

  • 目录
      • packages
        • 1 mocha
        • 2 supertest
        • 3 istanbul
      • demo
        • 1 code
        • 2 run test

1. packages

1.1 mocha

最广泛使用的js测试框架

npm install -g mocha
或者只在项目的开发环境下安装
npm install --save-dev mocha

1.2 supertest

http代理服务引擎,用来模拟http请求测试nodejs 接口

npm install --save-dev supertest

1.3 istanbul

用来生成测试用例覆盖率报表

npm install -g istanbul

2. demo

2.1 code

这里保存了登录之后的cookie,后面的请求都需要。
尝试用过agent = request.agent(app)的办法but not work

var should = require('should');
var assert = require('assert');
var request = require('supertest');

describe('Routing', function() {
  var url = 'http://localhost:3001';
  var userCookie = '';

  before(function(done) {
   done();
  });

  describe('#login', function() {
    it('should login success', function(done){
      var profile = {
        email: 'xuyawenwen',
        password: '123456',
      };
      request(url)
        .post('/login')
        .send(profile)
        .expect('Content-Type', /json/)
        .expect(200) //Status code
        .end(function(err,res) {
          if (err) {
            throw err;
          }
          console.log("success res==>",res.body);
          // Should.js fluent syntax applied
          res.body.status.should.equal(0);
          userCookie = res.headers['set-cookie'];
          done();
        });
    });
  });

  describe('#account', function() {
    it('should return account info', function(done){
      request(url)
        .get('/account/info')
        .set('Cookie', userCookie)
        .expect('Content-Type', /json/)
        .expect(200) //Status code
        .end(function(err,res) {
          if (err) {
            throw err;
          }
          console.log("success res==>",res.body);
          // Should.js fluent syntax applied
          res.body.status.should.equal(0);
          done();
        });
    });
  });
});

2.2 run test

command

mocha src/server/modules/account/test.js
或者
istanbul cover _mocha src/server/modules/account/test.js 

result

Routing
    #login
success res==> { status: 0,
  result: 
   { url: '/product/overview',
     userToken: '12b1d0f503125f0c582f494f080443c6' } }
      ✓ should login success (394ms)
    #account
success res==> { status: 0,
  result: 
   { spendSummary: 
      { totalUsedData: 144582296,
        times: 33,
        usersCount: 2,
        totalCost: 0 },
     accountInfo: 
      { balance: 11262,
        dataUsage: 107192452412,
        accountId: '436806e0-ab16-11e6-a3b1-7d052e670cc0',
        planInfo: [Object] } } }
      ✓ should return account info (85ms)


  2 passing (489ms)

=============================================================================
Writing coverage object [/Users/steven/mediaexpress/coverage/coverage.json]
Writing coverage reports at [/Users/steven/mediaexpress/coverage]
=============================================================================

=============================== Coverage summary ===============================
Statements   : 92.31% ( 24/26 )
Branches     : 50% ( 2/4 )
Functions    : 100% ( 8/8 )

你可能感兴趣的:(nodejs)