Writing tests

victorjonsson edited this page Feb 7, 2013 · 16 revisions
Clone this wiki locally

You can have one or several dokimon scripts, each containing one or several tests. All dokimon scripts should have the extension .djs and be located in the test directory defined in config.json. The scripts is written as ordinary node modules (http://howtonode.org/creating-custom-modules).

Example, tests for a RESTful API

Example, tests for wordpress

Basic example (myscript.djs)

var dokimon = require('dokimon'),
    assert = require('assert');

var checkHomepage = new dokimon.Test(
  'HomePageIsRunning', 
  {url : '/'}, 
  function(res, body) {
    assert.equal(res.statusCode, 200, 'My website is not responding');
  }
);

module.exports = checkHomepage;

You can also export an array with tests.

var dokimon = require('dokimon'),
    assert = require('assert');

var checkHomepage = new dokimon.Test(
  'HomePageIsRunning'...
);

var checkSiteSearch = new dokimon.TestPostForm(
  'SearchIsWorking', 
  {
    url : '/search/',
    write : {s : 'hockey', sortby : 'date', sortorder : 'desc'}
  },
  function(res, body) {
    assert.equal(res.statusCode, 200, 'Search is down');
    // and some other assertions that validates expected search result
  }
);

module.exports = [checkHomepage,checkSiteSearch];

Assuming that I've written this code in a file residing in my test directory (defined in config.json) and that the file has .djs as extension I can now run my tests by calling dokimon -r in the project directory. I can also choose to only run one of the tests by calling dokimon -rs myscript HomePageIsRunning

Available tests

dokimon.Test(name, options, callback, blocking)

name (String) - Display name of this test. Also used then you want to run this specific test dokimon -rs myscript MyTest

options (Object) - Configuration for this test, url is the only mandatory property (read more about the configuration in the bottom of this page)

callback (Function) - The function where you place your assertions, validating the repsonse form the server

blocking (Boolean) - Optional argument, default is false. This argument will make the test manager to wait until the callback is executed before running any more tests.

dokimon.TestPostForm(name, options, callback, blocking)

Same as dokimon.Test except that the manager will simulate a posted form request. When using this test you will have to add property write to the options argument.

The callback

When server response has finished the callback function will be called by the test manager. As long as no Error is thrown by the callback function the test is considered successful. It's advised that you use the assert library provided by node

Options

  • method : String — The method used when requesting the server (default GET)
  • url : String — The path that is requested, e.g. /home/some-url (Host is defined in config.json)
  • port : Number — Port used when requesting server (default 80)
  • headers : Object — An object containing headers that is sent with the request (default {})
  • write : Object — Data that will be sent as body of the request (default {})
  • keepCookies : Boolean — Determines whether or not cookies sent out by the server should be sent together with following requests, makes it possible to keep a server session alive (default false)
  • timeout : Number — The time in milliseconds before the test manager will consider that the test has failed because the server isn't responding (default 8000)
  • dependsOn : String — Makes it possible to define a reference to another test that has to be executed before running this particular test. Example:
var loginTest = new dokimon.Test(
  'loginTest',
  ...
);
var statusBarExistTest = new dokimon.Test(
  'statusBarExists',
  {
    dependsOn : 'loginTest'
  }
  ...
);

module.exports = [loginTest, statusBarExistTest];