Data driven testing using Mocha.
npm install mocha-ddt
$ npm install mocha-ddt --save
`
Getting started
`javascript
$ npm install mocha-ddt --save
$ mkdir test
$ $EDITOR test/test.js
$ $EDITOR index.js
`
In test.js:
`javascript
var assert = require('chai').assert;
var mochaddt = require('mocha-ddt');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
var array = mochaddt.input(this, "array");
var value = mochaddt.input(this, "value");
assert.equal(array.indexOf(value), -1);
});
});
})
`
In index.js:
`javascript
var mochaddt = require('mocha-ddt');
mochaddt.setTestDir("./test");
mochaddt.addTests(/#indexOf() should return -1 when the value is not present/, {
input: {
array: [1,2,3],
value: 4
}
});
mochaddt.run();
`
Features
$3
input is used to retrieve inputs for running a set of test cases.
`javascript
var some_var_name = mochaddt.input(this, "", );
`
INPUT_NAME: Name of the input that you need. Refer to addTests.
DEFAULT_VALUE: Optionally give a default value. In case no input is specified when running test, default value will be used. It is encouraged to provide a default value, so that you can run tests using standard Mocha interface.
Example:
`javascript
var mochaddt = require('mocha-ddt');
describe('country API', function() {
var country = mochaddt.input(this, "country", "USA");
describe('province API', function() {
var province = mochaddt.input(this, "province", "CA");
describe('city API', function() {
var city = mochaddt.input(this, "city", "San Francisco");
it(can give me the location of ${city}, function() {
var config = mochaddt.input(this, "config", {});
// You can use country, province, city and config here
});
});
});
describe('country overview API', function() {
it(can give an overview of ${country}, function() {
});
});
});
`
As can be seen in the example, input can be used inside describe block and it block.
output, fetch
output is used inside a test case to output values, so that other test cases can use the output values.
fetch, on the other hand, is used to fetch the output from a test case.
Note that you can only fetch from a test case that is within the same describe block.
Example:
`javascript
describe('account management api', function() {
describe('functional test', function() {
it('can create a new user', function() {
var cuid = .res.cuid;
mochaddt.output(this, cuid);
});
it('can get the newly created user', function() {
var cuid = mochaddt.fetch(this, "can create a new user");
// ...
});
it('can put the newly created user', function() {
var cuid = mochaddt.fetch(this, "can create a new user");
// ...
});
// ...
});
});
`
This certainly breaks the independency between test cases, but it is necessary sometimes especially for functional/E2E test suites. However, test cases in unit testing should remain independent and this feature shouldn't be used.
Calling API
mocha-ddt provides tools to send API requests.
#### Supported methods to call API
Synchronous API calls: getSync, putSync, deleteSync, postSync, patchSync, headSync, optionsSync
Asynchronous API calls (Promise based): get, put, delete, post, patch, head, options
Example of calling a GET API synchronously:
`javascript
var options = {
path: '/customers',
headers: {
:,
//...
},
qs: {
:,
//...
}
};
mochaddt.utils.getSync(options);
``