Running tests directly and as part of a suite

39 views
Skip to first unread message

josh

unread,
Jan 26, 2013, 11:32:51 PM1/26/13
to nod...@googlegroups.com
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 modules
var assert = require('assert'); // npm package var request = require('request'); // GET /not-exist should return 404 - not found function 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 server if (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 modules var assert = require('assert'); // npm package var request = require('request'); // GET /not-exist should return 404 - not found function 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 server if (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 modules
var 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));
  }
});

Daniel Rinehart

unread,
Jan 27, 2013, 7:49:26 AM1/27/13
to nodejs
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.

--
--
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 nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
 
 

josh

unread,
Jan 27, 2013, 7:25:15 PM1/27/13
to nod...@googlegroups.com
Daniel, your suggestion will make my test files shorter and DRY, but on the other hand running my tests might be less intuitive than before -
node unit/test/test_404.js vs node unit/test/runner.js unit/test/test_404.js.
I want to make it easy as possible to run the tests, so at the moment I prefer to have some verbosity in the test file itelf.

Benjamin, Joe looks interesting but I am not sure if I want to introduce another dependency to my project, unless it really add a lot of value.
in addition it's written in coffeescript, which will introduce some difficulties for me and future developers on this code base.
I guess to solve this issue I can create a copy of joe in javascript, but not sure if it adds a ton of value that justify it. we'll see about that.
at this point I prefer to stick to one language. of course this is very subjective topic and I might change my mind in the future.

Thanks for those suggestions guys. I really appreciate it!

Jamison Dance

unread,
Jan 27, 2013, 10:59:47 PM1/27/13
to nod...@googlegroups.com
Just put the call to runner in the npm test command, so people have an example to look at how to run the tests.
Reply all
Reply to author
Forward
0 new messages