标签 supertest 下的文章

公司有项目的API是使用json格式的,因此研究使用nodejs类别的测试框架看是否可以提高API测试效率。

网上使用supertest + Mocha 两个组件搭配进行测试的案例比如多,因此考虑使用这个。

首先需要安装nodejs。
官网: https://nodejs.org/en/
安装版本:v6.9.2
安装后支持npm包安装命令。

新建一个目录,例如project
安装两个组件,命令如下:

npm install supertest --save-dev
npm install mocha --save-dev

--save-dev参数是将组件安装到该目录,而不是全局系统目录。
两个组件版本:
"mocha": "^3.2.0",
"supertest": "^2.0.1"

supertest测试代码:

var serurl = 'http://IP/server/';
var request = require('supertest')(serurl);  

var userId = 'abc'
var password = '123456'

//创建登录请求
request
  .post('login/')
  .set('Content-Type', 'application/json')
  .send({userId: userId})
  .send({password: password})
  //发送请求
  .end(function(err, res){
  //原结果是text,将其转成json格式方便获取里面的数据
  var content = JSON.parse(res.text);
  //获取contact里面的error项的例子
  console.log(content.error);
    if(err) throw err;
  }
);

保存成test.js,然后执行node test.js就能看到结果了。

$node login.js
10000

使用Mocha生成报告,使用chai做断言库。用assert一直有问题,不知道原因。
先安装chai, npm install chai --save-dev

var serurl = 'http://IP/server/'
var request = require('supertest')(serurl)
var expect = require('chai').expect;

var userId = 'abc'
var password = '123456'

// Login
describe('Check errorCode', function() {
    it('check code return 10000', function(done) {
    request
    .post('login/')
    .set('Content-Type', 'application/json')
    .send({userId: userId})
    .send({password: password})
    .end(function(err,res){
        //将结果转成json格式
        content = JSON.parse(res.text)0
        //如果error返回10000就断定是pass
        expect(contact.error).to.be.equal('10000');
        done()
        })
    })
})

将以上保存成login.js后,用mocha login.js就可以看到测试结果。成功的结果:

$mocha login.js

Check error
✓ check it is 10000

1 passing (45ms)

模拟返回error不对等时的错误:

$mocha mocha_chai.js

Check errorCode
1) check it is 10000

0 passing (49ms)
1 failing

1) Check errorCode check it is 10000:

 Uncaught AssertionError: expected '10000' to equal '10001'
 + expected - actual

 -10000
 +10001

 at Test.<anonymous> (mocha_chai.js:25:35)
 at Test.assert (node_modules/supertest/lib/test.js:179:6)
 at assert (node_modules/supertest/lib/test.js:131:12)
 at node_modules/supertest/lib/test.js:128:5
 at Test.Request.callback (node_modules/superagent/lib/node/index.js:619:12)
 at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/index.js:795:18)
 at endReadableNT (_stream_readable.js:974:12)
 at _combinedTickCallback (internal/process/next_tick.js:74:11)
 at process._tickCallback (internal/process/next_tick.js:98:9)

expect断言的一些例子:

// 相等或不相等
expect(4 + 5).to.be.equal(9);
expect(4 + 5).to.be.not.equal(10);
expect(foo).to.be.deep.equal({ bar: 'baz' });

// 布尔值为true
expect('everthing').to.be.ok;
expect(false).to.not.be.ok;

// typeof
expect('test').to.be.a('string');
expect({ foo: 'bar' }).to.be.an('object');
expect(foo).to.be.an.instanceof(Foo);

// include
expect([1,2,3]).to.include(2);
expect('foobar').to.contain('foo');
expect({ foo: 'bar', hello: 'universe' }).to.include.keys('foo');

// empty
expect([]).to.be.empty;
expect('').to.be.empty;
expect({}).to.be.empty;

// match
expect('foobar').to.match(/^foo/);

参考:
http://www.jianshu.com/p/9c78548caffa
https://visionmedia.github.io/superagent/#test-documentation
https://github.com/visionmedia/supertest
http://wetest.qq.com/lab/view/145.html
http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html