nodejs webapi单元测试

本文介绍如何使用Mocha、Supertest及Istanbul等工具进行Node.js应用的接口测试。通过具体示例展示了登录验证和获取账户信息的过程,并分析了测试覆盖率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

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 )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值