Skip to content

Commit

Permalink
Fixes issue nightwatchjs#186: test case tagging
Browse files Browse the repository at this point in the history
You can now tag a test case by adding the `tags` property to a test module.

```
module.exports = {
  tags: ['login', 'sanity'],
  'demo login test': function (client) {
    // test code
  }
};
```

Select tags to run by specifying the `--tag` flag (`-a` for short):

```
bin/nightwatch --tag login --tag something_else
```
  • Loading branch information
sethmcl committed Jun 24, 2014
1 parent c89a983 commit d37e915
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 4 deletions.
4 changes: 4 additions & 0 deletions bin/_clirunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ CliRunner.prototype = {
this.test_settings.filename_filter = this.argv.f;
}

if (this.argv.a) {
this.test_settings.tag_filter = this.argv.a;
}

if (this.test_settings.disable_colors) {
Logger.disableColors();
}
Expand Down
11 changes: 9 additions & 2 deletions bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ cli.command('filter')
.defaults('')
.alias('f');

// $ nightwatch -s
// $ nightwatch --skipgroup
// $ nightwatch -a
// $ nightwatch --tag
cli.command('tag')
.description('Only run tests with the given tag.')
.defaults('')
.alias('a');

// $ nightwatch -h
// $ nightwatch --help
cli.command('help')
.description('Shows this help.')
.alias('h');
Expand Down
1 change: 1 addition & 0 deletions examples/tests/github.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
tags: ['git'],
'Demo test GitHub' : function (client) {
client
.url('https://github.com/beatfactor/nightwatch')
Expand Down
1 change: 1 addition & 0 deletions examples/tests/google.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
tags: ['google'],
'Demo test Google' : function (client) {
client
.url('http://www.google.com')
Expand Down
51 changes: 51 additions & 0 deletions lib/runner/matcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module.exports = {
/**
* @param {string} testFilePath - file path of a test
* @param {array} tags - tags to match
* @returns {boolean} true if specified test matches given tag
*/
tags: function (testFilePath, tags) {

try {
test = require(testFilePath);
} catch (e) {
// could not load test module
return false;
}

return this.checkModuleTags(test, tags);
},

/**
* @param {object} test - test module
* @param {array} tags - tags to match
* @returns {boolean}
*/
checkModuleTags: function (test, tags) {
var testTags = test.tags;
var match = false;

if (typeof tags === 'string') {
tags = [tags];
}

if (!Array.isArray(testTags)) {
return false;
}

tags = tags.map(function (tag) {
return tag.toLowerCase();
});

testTags
.map(function (testTag) {
return testTag.toLowerCase();
})
.some(function (testTag) {
match = (tags.indexOf(testTag) !== -1);
return match;
});

return match;
}
};
13 changes: 12 additions & 1 deletion lib/runner/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var mkpath = require('mkpath');
var minimatch = require('minimatch');
var Nightwatch = require('../../index.js');
var Logger = require('../util/logger.js');
var matcher = require('./matcher');

module.exports = new (function() {
var globalStartTime;
Expand Down Expand Up @@ -305,6 +306,10 @@ module.exports = new (function() {
return minimatch(filename, opts.filename_filter);
}

if (opts.tag_filter) {
return matcher.tags(filePath, opts.tag_filter);
}

return extensionPattern.exec(filePath);
});

Expand Down Expand Up @@ -435,10 +440,16 @@ module.exports = new (function() {
}

runFiles(paths, function runTestModule(err, fullpaths) {
var errorMessage = ['No tests defined! using source folder', paths];

if (opts.tag_filter) {
errorMessage.push('; using tags:', opts.tag_filter);
}

opts.modulesNo = fullpaths && fullpaths.length || 0;

if (!fullpaths || fullpaths.length === 0) {
finishCallback({message: 'No tests defined! using source folder ' + paths});
finishCallback({message: errorMessage.join(' ')});
return;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/sampletests/tags/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
tags: ['login'],
demoTagTest: function (client) {
client.url('http://localhost')
.assert.elementPresent('#weblogin')
.end();
}
};
50 changes: 50 additions & 0 deletions tests/src/runner/testMatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var BASE_PATH = process.env.NIGHTWATCH_COV
? 'lib-cov'
: 'lib';
var matcher = require('../../../'+ BASE_PATH +'/runner/matcher.js');

module.exports = {
'tag: test matching tags': function (test) {
var tags = ['home', 'login', 'sign-up'];
var testModule = {
tags: ['home', 'siberia']
};

var matched = matcher.checkModuleTags(testModule, tags);

test.ok(matched === true);
test.done();
},

'tag: test non-matching tags': function (test) {
var tags = ['home', 'login', 'sign-up'];
var testModule = {
tags: ['boroboro', 'siberia']
};

var matched = matcher.checkModuleTags(testModule, tags);

test.ok(matched === false);
test.done();
},

'tag: test undefined tags': function (test) {
var tags = ['home', 'login', 'sign-up'];
var testModule = {};

var matched = matcher.checkModuleTags(testModule, tags);

test.ok(matched === false);
test.done();
},

'tag: test loading module with tags': function (test) {
var tags = ['home', 'login', 'sign-up'];
var testModule = require('../../sampletests/tags/sample');

var matched = matcher.checkModuleTags(testModule, tags);

test.ok(matched === true);
test.done();
}
};
19 changes: 18 additions & 1 deletion tests/src/runner/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,22 @@ module.exports = {
test.ok('demoTestMixed' in results.modules.sample);
test.done();
});
}
},

testRunWithTags : function(test) {
Runner.run([process.cwd() + '/sampletests/withexclude'], {
seleniumPort : 10195,
silent : true,
output : false,
globals : {
test : test
},
tag_filter : ['login']
}, {
output_folder : false
}, function(err, results) {
test.ok(!('demoTagTest' in results.modules));
test.done();
});
},
};

0 comments on commit d37e915

Please sign in to comment.