Virile testing for http servers or any nodejs application.
npm install testosterone
Testosterone allows you to follow BDD or TDD on any of your projects using the same testing library.
host
: Host to do the http calls. localhostport
: Port to do the http calls. 80output
: Configure the amount of verbosity you want for your testsspecs
: Print the specs trueticks
: Print the ✓ and ✗ ticks truesummary
: Prints the summary truetitle
: Prints the title true
title
: Test title, it will be printed out. Testosteronesync
: If set totrue
, you don't need to calldone
to specify when your tests are done. false
testosterone API is simple and flexible.
get|post|head|put|delete...(url, req, response, cb)
: Does a http call with the given request. If a response is given, testosterone will assert that the real response matches.add(spec, function(done))
: Adds a test. The test is considered executed whendone
function is called.before(function)
: Runs before each test.after(function)
: Runs after each test.run([cb])
: Runs the tests in serial.cb
will be called once all the tests are executed.assert
: You must use this assert object instead of the native one.
All the functions are chainable.
You have more examples on the test
folder:
var testosterone = require('testosterone')({port: 3000})
, assert = testosterone.assert;
testosterone
.get('/', function (res) {
assert.equal(res.statusCode, 200)
})
.get('/hi', function (res) {
assert.equal(res.statusCode, 500);
assert.equal(res.body, 'use post instead');
})
.post('/hi', {data: {message: 'hola'}}, {
status: 200
, body: 'hola'
});
// Output
$ node test.js
✿ Testosterone : ✓ ✓ ✓ ✓ ✓
» 3 responses, 5 asserts
var testosterone = require('testosterone')({post: 3000, title: 'Testing async'})
, assert = testosterone.assert;
testosterone
.before(function () {
console.log('test about to run!');
})
// using done to tell testosterone when the test is done
.add('First test', function (done) {
setTimeout(function () {
assert.ok(true);
done();
}, 999);
})
// same but currying
.add('Second test', function (spec) {
assert.ok(true);
setTimeout(done(function () {
assert.ok(true);
}), 10);
})
.run(function () {
require('sys').print('All tests passed!');
});
// Output
$ node test.js
✿ Testing async :
First test => ✓
Second test => ✓ ✓
» 0 responses, 3 asserts
Example with gently stubbing and sync: true
:
var testosterone = require('testosterone')({post: 3000, title: 'Testing with stubs', sync: true})
, gently = new (require('gently'))
, fs = require('fs')
, assert = testosterone.assert;
testosterone
.add('GIVEN foo.txt \nWHEN its empty \nTHEN it return null', function (spec) {
gently.expect(fs, 'readFile', function (path, encoding, cb) {
assert.equal(path, 'foo.txt');
cb(null, null);
});
fs.readFile('foo.txt', 'utf-8', function (er, data) {
assert.equal(er, null);
assert.equal(data, null);
});
})
.add('GIVEN foo.txt \nWHEN it have content \nTHEN it return that content', function (spec) {
gently.expect(fs, 'readFile', function (path, encoding, cb) {
assert.equal(path, 'foo.txt');
cb(null, 'foo');
});
fs.readFile('foo.txt', 'utf-8', function (er, data) {
assert.equal(er, null);
assert.equal(data, 'foo');
});
})
.run(function () {
require('sys').print('done!');
});
// Output
$ node test.js
✿ Testing with stubs :
GIVEN foo.txt
WHEN its empty
THEN it return null => ✓ ✓ ✓
GIVEN foo.txt
WHEN it have content
THEN it return that content => ✓ ✓ ✓
» 6 asserts
In order to run the tests type:
npm install
make test_app
make
The _call function of this library is a shameless copy from expresso response assert done by TJ Holowaychuk (visionmedia)