I would suggest modifying your runner.js so that it can take optional command line options to just run a single test or directory. That way you avoid any duplication of setup code or logic in individual tests. You just execute "node test/unit/runner.js test/unit/test_404.js" to run an individual test or "node test/unit/runner.js" to run them all.
-- Daniel R. <[email protected]> [http://danielr.neophi.com/] On Sat, Jan 26, 2013 at 11:32 PM, josh <[email protected]> wrote: > I want to run my test file alone - node test/unit/test_404.js > but also with the test suite - node test/unit/runner.js > > to achieve that I added an if/else statement in each test file to check if > the code is running directly with the node executable or if it's required > by other file. > if it was run directly, I run my server before running the test. > if the file was required by the test suite runner, I just export the test > so the runner will call it (module.exports = test). > > Here are 2 version of one of the test files - > http://hastebin.com/fovofufome.js > They are doing the same thing, except that in version 2 I moved the > runServerBeforeTest function to it's own helper file. > I found the first version more readable but it's more verbose and I need > to repeate that in each test file. > Any opinions or even different approaches for testing? > > btw, I deliberately uses node's built-in asserts and not using any test > framework. I would like to feel pain before adding libraries and so far I > am happy with this 'leas' approach. > > Pasting my code here as well: > > // test_404.js version 1 > // core modulesvar assert = require('assert'); > // npm packagevar request = require('request'); > > // GET /not-exist should return 404 - not foundfunction test(config) { > console.log('GET /not-exist'); > > request({ > uri: "http://localhost:" + config.webSitePort + "/not-exist", > followRedirect: false > }, > function (err, res, body) { > if (err) { > console.log('Error in ' + __filename + '. ' + err); > } else { > assert.equal(res.statusCode, 404) > // process.exit(); > }; > }); > }; > // The following code let us run this test file by itself or as part of the > runner.js// if this file is by itself with 'node test_foo.js' we need to run > the server before calling the test// else - expose the test function since > the runner.js already run the serverif (module === require.main) { > runServerBeforeTest(); > } else { > module.exports = test; > }; > function runServerBeforeTest() { > // my modules > var config = require('../../config/test.js'); > var app = require('../../app.js'); > > app.init(config, function (err, msg) { > if (err) { > console.log('Error during application init: ' + err); > } else { > console.log(msg); > app.start(test(config)); > } > }); > }; > > // test_404.js version 2 > > // core modulesvar assert = require('assert'); > // npm packagevar request = require('request'); > > // GET /not-exist should return 404 - not foundfunction test(config) { > console.log('GET /not-exist'); > > request({ > uri: "http://localhost:" + config.webSitePort + "/not-exist", > followRedirect: false > }, > function (err, res, body) { > if (err) { > console.log('Error in ' + __filename + '. ' + err); > } else { > assert.equal(res.statusCode, 404) > // process.exit(); > }; > }); > }; > // The following code let us run this test file by itself or as part of the > runner.js// if this file is by itself with 'node test_foo.js' we need to run > the server before calling the test// else - expose the test function since > the runner.js already run the serverif (module === require.main) { > runServerBeforeTest(); > require(__dirname + '/helper.js').runServerBeforeTest(test); > } else { > module.exports = test; > }; > > > > // helper.js - after my server is listening, run a given test > > module.exports.runServerBeforeTest = runServerBeforeTest; > function runServerBeforeTest(test) { > // my modules > var config = require('../../config/test.js'); > var app = require('../../app.js'); > > app.init(config, function (err, msg) { > if (err) { > console.log('Error during application init: ' + err); > } else { > console.log(msg); > app.start(test(config)); > } > }); > }; > > // runner.js - after my server is listening - run all test*.js files > // my modulesvar config = require('../../config/test.js');var app = > require('../../app.js');var helper = require(__dirname + '/helper.js'); > function runTests() { > console.log('running tests:'); > > testFiles = []; > var test = null; > testFiles = require("fs").readdirSync(__dirname).filter(function(file) { > return (/^test/.test(file)) > }); > > testFiles.forEach(function(file) { > test = require("./" + file); > if (typeof(test) === 'function') { > test(config); > } > }); > }; > > app.init(config, function (err, msg) { > if (err) { > console.log('Error during application init: ' + err); > } else { > console.log(msg); > app.start(runTests(config)); > } > }); > > > -- > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > > > > -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
