Mocha - javascript test framework

Mocha

Mocha is a feature-rich JavaScript test framework running on node and the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.

Features

Test requirements

Installation - I

Installation - II

Write the first test

test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var should = require('should'),
  request = require('request'),
  qs = require('querystring'),
  zlib = require('zlib');

describe('Qiupu APIs: /qiupu', function() {
  before(function() {
  });

  describe('/app/all', function() {
    before(function() {
    });

    it('show all apps.', function(done) {
      request.get({
        url: 'http://api.borqs.com/qiupu/app/all',
        encoding: null,
        headers: { 'accept-encoding': 'gzip,deflate'}
      }, function(err, resp, body) {
        resp.should.be.ok;
        resp.should.have.header('content-encoding', 'gzip');
        zlib.unzip(body, function(err, buffer) {
          should.not.exist(err);
          should.exist(buffer);
          var json = JSON.parse(buffer);
          json.should.not.be.empty;
          json.should.be.an.instanceof(Array);
          json[0].should.have.property('package');
          json[0].should.have.property('app_name');
          json[0].should.have.property('app_liked_users')
            .and.be.an.instanceof(Array);
          done();
        });
      });
    });
  });
});

Load required libraries

1
2
3
4
var should = require('should'),
  request = require('request'),
  qs = require('querystring'),
  zlib = require('zlib');

Define test suite

1
2
3
4
5
6
7
describe('Qiupu APIs: /qiupu', function() {
  before(function() {
    //......
  });

  //......
});

Define test case

1
2
3
it('show all apps.', function(done) {
  //......
});

Http request

1
2
3
4
5
6
7
  request.get({
    url: 'http://api.borqs.com/qiupu/app/all',
    encoding: null,
    headers: { 'accept-encoding': 'gzip,deflate'}
  }, function(err, resp, body) {
    //......
  });

Unzip http body

1
2
3
4
5
6
    zlib.unzip(body, function(err, buffer) {
      //......
      var json = JSON.parse(buffer);
      //...
      done();
    });

Asserting using should

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    resp.should.be.ok;
    resp.should.have.header('content-encoding', 'gzip');
    zlib.unzip(body, function(err, buffer) {
      should.not.exist(err);
      should.exist(buffer);
      var json = JSON.parse(buffer);
      json.should.not.be.empty;
      json.should.be.an.instanceof(Array);
      json[0].should.have.property('package');
      json[0].should.have.property('app_name');
      json[0].should.have.property('app_liked_users')
        .and.be.an.instanceof(Array);
      done();
    });

Should: https://github.com/visionmedia/should.js

Run the test

1
$ mocha --reporter list --timeout 5000 test.js

Simple steps to run the demo

1
2
3
4
$ git clone git://github.com/xiaocong/demo-mocha.git
$ cd demo-mocha
$ npm install .
$ make test

References

Alternative REST API test framework

Frisby.js(http://frisbyjs.com): REST API testing framework.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var frisby = require('frisby');

frisby.create('Get Brightbit Twitter feed')
  .get('https://api.twitter.com/1/statuses/user_timeline.json?screen_name=brightbit')
  .expectStatus(200)
  .expectHeaderContains('content-type', 'application/json')
  .expectJSON('0', {
    place: function(val) { expect(val).toMatchOrBeNull("Oklahoma City, OK"); },
    user: {
      verified: false,
      location: "Oklahoma City, OK",
      url: "http://brightb.it"
    }
  })
  .expectJSONTypes('0', {
    id_str: String,
    retweeted: Boolean,
    in_reply_to_screen_name: function(val) { expect(val).toBeTypeOrNull(String); },
    user: {
      verified: Boolean,
      location: String,
      url: String
    }
  })
.toss();

Thanks & QA

/