Created
January 20, 2011 12:22
-
-
Save mikehadlow/787815 to your computer and use it in GitHub Desktop.
A little javascript test runner for node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run unit tests in node like this: | |
// node Suteki.TestRunner.js | |
// | |
// Unit tests should be named like this: | |
// Suteki.<name target>.Tests.js | |
// | |
// A test looks like this: | |
// | |
// (function(){ | |
// | |
// exports.this_test_is_ok = function(assert) { | |
// assert(true, "should work fine?"); | |
// }; | |
// | |
// exports.this_test_will_fail = function(assert) { | |
// assert(false, "hi, I've failed"); | |
// }; | |
// | |
// exports.this_test_throws = function(assert) { | |
// throw new Error("Damn!"); | |
// }; | |
// | |
// }()); | |
(function(){ | |
var getFilesInCurrentDirectory = function(){ | |
var fs = require('fs'); | |
return fs.readdirSync('.'); | |
}; | |
var filterUnitTests = function(allFiles){ | |
var i, fileName; | |
var unitTests = []; | |
for(i=0; i<allFiles.length; i++){ | |
fileName = allFiles[i]; | |
// identify test: | |
// Suteki.<name of target>.Tests.js | |
if(/Suteki\.([0-9\.\-_A-Za-z]+)\.Tests\.js/.test(fileName)){ | |
// add the filename minus the .js extension | |
unitTests.push(fileName.replace(/\.js$/, "")); | |
} | |
} | |
return unitTests; | |
}; | |
var runTest = function(testName){ | |
var testFunction; | |
var unitTest = require("./" + testName); | |
var run = 0; | |
var succeeded = 0; | |
var failed = false; | |
console.log(""); | |
console.log(testName); | |
console.log("-------------------------------------------------------"); | |
if(unitTest){ | |
for(testFunction in unitTest){ | |
if(unitTest.hasOwnProperty(testFunction)){ | |
run++; | |
failed = false; | |
console.log(">>> " + testFunction.replace(/_/g, " ")); | |
try{ | |
unitTest[testFunction](function(passed, message){ | |
if(!passed){ | |
console.log("FAILED " + (message || "")); | |
failed = true; | |
} | |
}); | |
} catch(exception) { | |
failed = true; | |
console.log("FAILED with error: '" + exception.message + "'"); | |
console.log(exception.stack); | |
} | |
if(!failed){ | |
succeeded++; | |
} | |
} | |
} | |
console.log("Ran " + run + ", Failed " + (run - succeeded) + ", Passed " + succeeded); | |
} | |
else { | |
console.log("Could not load test '" + testName + "'") | |
} | |
return { ran:run, succeeded:succeeded }; | |
}; | |
// main | |
(function(){ | |
var i, unitTest, testResult; | |
var unitTests = filterUnitTests(getFilesInCurrentDirectory()); | |
var ran = 0; | |
var succeeded = 0; | |
for(i=0; i<unitTests.length; i++){ | |
unitTest = unitTests[i]; | |
testResult = runTest(unitTest); | |
ran = ran + testResult.ran; | |
succeeded = succeeded + testResult.succeeded; | |
} | |
console.log(""); | |
console.log("Tests complete"); | |
console.log("Fixtures: " + unitTests.length + ", Ran: " + ran + ", Failed: " + (ran - succeeded)); | |
console.log(""); | |
}()); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output looks like this: