Nightwatch: Add ability to tag individual test(s)

Created on 26 Jun 2015  路  8Comments  路  Source: nightwatchjs/nightwatch

As number of tests goes up need of tags also goes up. We have use case to run selected tests from multiple folders/features. It would be great if we can run them by tag. Currently It's only possible to add tag at file level.

These will allow me to run grunt nightwatch --tag tag9

Option 1: Use comment interpreter:

module.exports = {
  //tags: t1, t2, t3
  'Demo test Google 1' : function (client) {
    client
      .url('http://google.no')
      .end();
  },
  //tags: t1, t3, t9
  'Demo test Google 2' : function (client) {
    client
      .url('http://google.no')
      .end();
  }
};

Option 2: Use parameter:

module.exports = {
  'Demo test Google 1' : function (client, {tags: [t1, t2]}) {
    client
      .url('http://google.no')
      .end();
  },
  'Demo test Google 2' : function (client, [t1, t3]) {
    client
      .url('http://google.no')
      .end();
  }
};

Most helpful comment

@beatfactor can you re-open this one or provide an alternate solution?

All 8 comments

This feature already exists: http://nightwatchjs.org/guide#test-tags

Well what I was looking for is still not possible. For example please look at following simple test file. How do I run login only prod-ready tests by tag?

module.exports = {
  tags: ['login', 'sanity', 'prod-ready'],
  'demo login test': function (client) {
     console.log('In login test');
     client.end();
  },
  tags: ['checkout', 'sanity'],
  'demo checkout test': function (client) {
    console.log('In checkout test');
    client.end();
  },
  tags: ['signup', 'sanity', 'prod-ready'],
  'demo sign up test': function (client) {
    console.log('In sign up test');
    client.end();
  }
};

I am having the same exact problem ^

Need a way to use different tags on test cases in the same file.

Same issue, need a way to use different tags on same file

@beatfactor can you re-open this one or provide an alternate solution?

Maybe this comment belongs in a new issue, but I'd rather not create a duplicate so starting here.

@beatfactor mentioned in another post that the feature to tag individual test cases within a module is not easily possible to implement in a consistent manner, so it seems like that won't happen anytime soon.

In the mean time: Has anyone come up with a good workaround for this? (where "this" = tagging a subset of test cases within a module(s) and then running (or excluding) all test cases with that tag)

This is not the most elegant solution, but here's a simple workaround:

1. Create a lib file

const argv = require('minimist')(process.argv.slice(2))
module.exports = {
  filterTaggedTests (testModule) {
    const tagToExclude = argv['foo-tags-exclude']
    if (tagToExclude) {
      const filteredTestModule = {}
      for (let testName in testModule) {
        if (!testName.includes(`[${tagToExclude}]`)) {
          filteredTestModule[testName] = testModule[testName]
        }
      }
      return filteredTestModule
    }
    return testModule
  }
}

2. Include lib and use the filter function

In your spec file, wrap your tests:

const lib = require('../lib')
module.exports = lib.filterTaggedTests({
  'Test A': function(){ ... },
  'Test B [my-tag]': function(){ ... }
})

You can now exclude tests tagged my-tag by using the new option --foo-tags-exclude:

$ npm run e2e -- --foo-tags-exclude my-tag

I'm using the namespace foo to avoid collision with any existing or future cli option from nightwatch.

@beatfactor , this question is still relevant today. Is it possible to get a workaround that doesn't involve adding our own custom cli options?

Here are our use cases:

  1. We want to identify tests which are stubs to be populated later.
  2. We want to keep our similar tests together but would like to indicate that some are long-running or modify the environment when they run.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxgalbu picture maxgalbu  路  3Comments

chaseconey picture chaseconey  路  4Comments

Zechtitus picture Zechtitus  路  4Comments

lgaticaq picture lgaticaq  路  3Comments

betweenbrain picture betweenbrain  路  4Comments