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 calldoneto 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 whendonefunction is called.before(function): Runs before each test.after(function): Runs after each test.run([cb]): Runs the tests in serial.cbwill 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:
vartestosterone=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$nodetest.js✿ Testosterone : ✓✓✓✓✓»3responses,5assertsvartestosterone=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$nodetest.js✿Testing async :
Firsttest=>✓Secondtest=>✓✓»0responses,3assertsExample with gently stubbing and sync: true:
vartestosterone=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$nodetest.js✿Testingwith stubs :
GIVENfoo.txtWHENitsemptyTHENitreturnnull=>✓✓✓GIVENfoo.txtWHENithavecontentTHENitreturnthatcontent=>✓✓✓»6assertsIn order to run the tests type:
npm install
make test_app
makeThe _call function of this library is a shameless copy from expresso response assert done by TJ Holowaychuk (visionmedia)
