<commit_before>[![Build Status](https://secure.travis-ci.org/alexfernandez/testing.png)](http://travis-ci.org/alexfernandez/testing)

Simple async testing library for node.js.

## Installation

Just run:
    $ npm install testing

Or add package testing to your package.json dependencies.

## Usage

Add asynchronous testing to your code very easily. Require testing:

    var testing = require('testing');

Add a test function to your code, checking if results are what should be expected:

    function testAdd(callback)
    {
		testing.assertEquals(add(1, 1), 2, 'Maths fail', callback);
		testing.success(callback);
    }

Run an async test to read the contents of a file and check it is not empty:

    function testAsync(callback)
    {
        function fs.readFile('file.txt', function(error, result)
        {
            if (error)
            {
                testing.failure('File not read', callback);
            }
            testing.assert(result, 'Empty file', callback);
            testing.success(callback);
        });
    }

Run all tests:

    testing.run({
        this: testAdd,
        async: testAsync,
    }, callback);

Will run tests sequentially.

## API

Implementation is very easy, based around three functions.

### Basics

Callbacks are used for asynchronous testing. They follow the usual node.js convention:

    callback(error, result);

When no callback is passed, synchronous testing is performed.

#### testing.success([message], [callback])

Note success for the current test. An optional message is shown if there is no callback.

If there is a callback, then it is called with the message. Default message: true.

Example:

    testing.success(callback);

#### testing.failure([message], [callback])

Note failure for the current test.

If the callback is present, calls the callback with the error:

    callback(message);

Otherwise the message is shown using console.error(). Default message: 'Error'.

Example:

    testing.failure('An error happened', callback);

#### testing.run(tests, [timeout], [callback])

Run a set of tests. The first parameter is an object containing one attribute for every testing function.

The tests are considered as a failure when a certain configurable timeout has passed.
The timeout parameter is in milliseconds. The default is 1 second per test.

When the optional callback is given, it is called after a failure or the success of all tests.

Example:

    testing.run({
        first: testFirst,
        second: testSecond,
    }, 1000, callback);

For each attribute, the key is used to display success; the value is a testing function that accepts an optional callback.
<commit_msg>Improved the README a bit.<commit_after>[![Build Status](https://secure.travis-ci.org/alexfernandez/testing.png)](http://travis-ci.org/alexfernandez/testing)

Simple async testing library for node.js.
Better suited to asynchronous tests than other libraries since it uses callbacks to get results.

Now shows results in pretty colors!

## Installation

Just run:
    $ npm install testing

Or add package testing to your package.json dependencies.

## Usage

Add asynchronous testing to your code very easily. Require testing:

    var testing = require('testing');

Add a test function to your code, checking if results are what should be expected:

    function testAdd(callback)
    {
		testing.assertEquals(add(1, 1), 2, 'Maths fail', callback);
		testing.success(callback);
    }

Run an async test to read the contents of a file and check it is not empty:

    function testAsync(callback)
    {
        function fs.readFile('file.txt', function(error, result)
        {
            if (error)
            {
                testing.failure('File not read', callback);
            }
            testing.assert(result, 'Empty file', callback);
            testing.success(callback);
        });
    }

Run all tests:

    testing.run({
        this: testAdd,
        async: testAsync,
    }, callback);

Will run tests sequentially.

## API

Implementation is very easy, based around three functions.

### Basics

Callbacks are used for asynchronous testing. They follow the usual node.js convention:

    callback(error, result);

When no callback is passed, synchronous testing is performed.

#### testing.success([message], [callback])

Note success for the current test. An optional message is shown if there is no callback.

If there is a callback, then it is called with the message. Default message: true.

Example:

    testing.success(callback);

#### testing.failure([message], [callback])

Note failure for the current test.

If the callback is present, calls the callback with the error:

    callback(message);

Otherwise the message is shown using console.error(). Default message: 'Error'.

Example:

    testing.failure('An error happened', callback);

#### testing.run(tests, [timeout], [callback])

Run a set of tests. The first parameter is an object containing one attribute for every testing function.

The tests are considered as a failure when a certain configurable timeout has passed.
The timeout parameter is in milliseconds. The default is 2 seconds per test.

When the optional callback is given, it is called after a failure or the success of all tests.

Example:

    testing.run({
        first: testFirst,
        second: testSecond,
    }, 1000, callback);

For each attribute, the key is used to display success; the value is a testing function that accepts an optional callback.

{'fraction_non_alphanumeric': 0.08473032069970846, 'fraction_numerical': 0.0029154518950437317, 'mean_word_length': 3.35289452815226, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '36521', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1383, 'n_words': 635}