Module test

A test runner compliant to the CommonJS Unit Testing specification. It manages the execution of unit tests and processes test results. The runner reports the total number of failures as exit status code.

The runner treats a module like a test case. A test case defines the fixture to run multiple tests. Test cases can provide optional setUp() and tearDown() functions to initialize and destroy the fixture. The test runner will run these methods prior to / after each test.

The following example test case testDatabase.js starts a new test runner if executed with ringo testDatabase.js

Example

// testDatabase.js
exports.setUp = function() { ... open db connection ... }
exports.tearDown = function() { ... close db connection ... }

exports.testCreateTable = function() { ... }
exports.testInsertData = function() { ... }
exports.testTransactions = function() { ... }
exports.testDeleteTable = function() { ... }

if (require.main == module.id) {
  // Get a runner and run on the current module
  require("test").run(exports);
}

See

The assert module is an assertion library to write unit tests.

Functions


getStackTrace (trace)

Creates a stack trace and parses it for display.

Parameters

java.lang.StackTraceElement trace The trace to parse. If not given a stacktrace will be generated

Returns

String The parsed stack trace

getType (obj)

Returns the type of the object passed as argument.

Parameters

obj

Returns

String The type of the object passed as argument

jsDump (value, lvl)

Converts the value passed as argument into a nicely formatted and indented string

Parameters

Object value The value to convert into a string
Number lvl Optional indentation level (defaults to zero)

Returns

String The string representation of the object passed as argument

run (scope, name, writer)

The main runner method. This method can be called with one, two or three arguments: run(scope), run(scope, nameOfTest), run(scope, writer) or run(scope, nameOfTest, writer)

Parameters

String|Object scope Either the path to a module containing unit tests to execute, or an object containing the exported test methods or nested scopes.
String name Optional name of a test method to execute
Object writer Optional writer to use for displaying the test results. Defaults to TermWriter.