Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added regex command line argument to filter tests #136

Merged
merged 2 commits into from

2 participants

@einaros

Useful to only run tests with e.g. 'binary' in them -- which I'm currently using while adding new parsers to socket.io.

@einaros

ee1362e is a bonus bugfix, unrelated to e38a86a.

@tj
Owner
tj commented

looks fine to me, thanks

@tj tj merged commit e410d82 into visionmedia:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 59 additions and 2 deletions.
  1. +24 −2 bin/expresso
  2. +25 −0 test/match.test.js
  3. +10 −0 test/match/test.js
View
26 bin/expresso
@@ -67,6 +67,12 @@ var testcount = 0;
var only = [];
/**
+ * Regex expression filtering tests to run.
+ */
+
+var match = '';
+
+/**
* Boring output.
*/
@@ -123,6 +129,7 @@ var usage = ''
+ '\n -t, --timeout MS Timeout in milliseconds, defaults to 2000'
+ '\n -r, --require PATH Require the given module path'
+ '\n -o, --only TESTS Execute only the comma sperated TESTS (can be set several times)'
+ + '\n -m, --match EXP Execute only tests matching a given regular expression (can be set several times)'
+ '\n -I, --include PATH Unshift the given path to require.paths'
+ '\n -p, --port NUM Port number for test servers, starts at 5555'
+ '\n -s, --serial Execute tests serially'
@@ -166,6 +173,14 @@ while (args.length) {
throw new Error('--only requires comma-separated test names');
}
break;
+ case '-m':
+ case '--match':
+ if (arg = args.shift()) {
+ match += (match.length > 0 ? '|' : '') + arg;
+ } else {
+ throw new Error('--match requires argument');
+ }
+ break;
case '-p':
case '--port':
if (arg = args.shift()) {
@@ -828,7 +843,10 @@ function runSuite(title, tests, callback) {
var keys = only.length
? only.slice(0)
: Object.keys(tests);
-
+
+ // Regular expression test filter
+ var filter = new RegExp('(?:' + (match.length == 0 ? '.' : match) + ')');
+
// Setup
var setup = tests.setup || function(fn, assert) { fn(); };
var teardown = tests.teardown || function(fn, assert) { fn(); };
@@ -840,6 +858,10 @@ function runSuite(title, tests, callback) {
if (keys.length) {
var key,
fn = tests[key = keys.shift()];
+
+ // Filter
+ if (filter.test(key) === false) return next();
+
// Non-tests
if (key === 'setup' || key === 'teardown') return next();
@@ -928,7 +950,7 @@ Test.prototype.runSerial = function(callback) {
test.teardown(callback);
} else {
var id = setTimeout(function() {
- throw new Error("'" + key + "' timed out");
+ throw new Error("'" + test.title + "' timed out");
}, timeout);
test.callback = function() {
clearTimeout(id);
View
25 test/match.test.js
@@ -0,0 +1,25 @@
+var assert = require('assert');
+var spawn = require('child_process').spawn;
+
+module.exports = {
+ 'will run only matched tests': function() {
+ var proc = spawn('bin/expresso', ['-m', 'p..s', 'test/match/test.js']);
+ proc.on('exit', function(code) {
+ completed = true;
+ assert.equal(0, code, 'failing test was not filtered out');
+ });
+ setTimeout(function() {
+ proc.kill('SIGINT');
+ }, 1000);
+ },
+ 'will run tests matched in a subsequent expression': function() {
+ var proc = spawn('bin/expresso', ['-m', 'nothing', '--match', 'p..s', 'test/match/test.js']);
+ proc.on('exit', function(code) {
+ completed = true;
+ assert.equal(0, code, 'failing test was not filtered out');
+ });
+ setTimeout(function() {
+ proc.kill('SIGINT');
+ }, 1000);
+ },
+};
View
10 test/match/test.js
@@ -0,0 +1,10 @@
+var assert = require('assert');
+
+module.exports = {
+ 'this test will pass': function() {
+ assert.ok(true);
+ },
+ 'this test will fail': function() {
+ assert.ok(false);
+ },
+}
Something went wrong with that request. Please try again.