41

How do I run only Test 3 from the following tests?

module.exports = {
  'Test 1':function(){},
  'Test 2':function(){}
  'Test 3':function(){}
}
Rob Bednark
  • 19,968
  • 18
  • 67
  • 100
Mohamed El Mahallawy
  • 11,102
  • 12
  • 44
  • 77

6 Answers6

75

A new parameter --testcase has been added to run a specified testcase.

nightwatch.js --test tests\demo.js --testcase "Test 1"

It's a new feature since the v0.6.0

https://github.com/beatfactor/nightwatch/releases/tag/v0.6.0

Nicolas Pennec
  • 6,036
  • 21
  • 37
10

You must use specific tags before function and separate all functions in diferent files under tests directory, and then call command with --tag argument. See wiki nightwatch tags page and watch this example:

// --- file1.js ---
module.exports = {
    tags: ['login'],
    'Test 1':function(){
        //TODO test 1
    }
};

// --- file2.js ---
module.exports = {
    tags: ['special', 'createUser'],
    'Test 2':function(){
        //TODO test 2
    },
};

// --- file3.js ---
module.exports = {
    tags: ['logoff', 'special'],
    'Test 3':function(){
        //TODO test 3
    },
}

If you run:

nightwatch.js --tag login

only runs Test 1, however if you run:

nightwatch.js --tag special

Test 2 and Test 3 will be executed.

You can specific more than one tag

nightwatch.js --tag tag1 --tag tag2

Separate each test function is mandatory because Nightwatch handled with filematcher each file. See Github code.

PD: If file has syntax errors, is possible that test don't run or test hasn't been found

albertoiNET
  • 1,189
  • 17
  • 28
  • 1
    This is pretty awesome - I think what would be optimal is if it worked like mocha and grepping. That's what I was hoping for! – Mohamed El Mahallawy Feb 25 '15 at 02:19
  • Yeah, but it doesn't work for this way. If you like, check [filematcher.js util lib](https://github.com/beatfactor/nightwatch/blob/6e49ffdb9afa7644c4ecf9dc6bc84fc5bd2f00df/lib/runner/filematcher.js) from github nightwatch repo – albertoiNET Feb 25 '15 at 10:41
  • @albertoiNET could you provide a solution where using nightwatch programmatically? Meaning, running a node script that invokes `nightwatch.runner` – Jeremy Jan 15 '19 at 17:18
8

The --testcase flag can since version 0.6 be used to run a single test from the commandline, e.g.

nightwatch.js --test tests\demo.js --testcase "Test 1"

This could be done using either test groups or test tags. You can also execute a single test with the --test flag, e.g.

nightwatch.js --test tests\demo.js
Daniel Wärnå
  • 598
  • 8
  • 16
1

For me, it only works with:

npm run test -- tests/01_login.js --testcase "Should login into Dashboard"

npm run <script> -- <test suite path> --testcase "<test case>"

my script in package.json:

"test": "env-cmd -f ./.env nightwatch --retries 2 --env selenium.chrome",

at nightwatch version 1.3.4

You can also use tags:

npm run <script> -- <enviroment> <tag>
npm run test -- --env chrome --tag login

just add it to your test case:

module.exports = {
  '@tags': ['login', 'sanity', 'zero1'],
...
}
Daniel M.
  • 11
  • 2
0

you can do somthing like:

node nightwatch.js -e chrome --test tests/login_test --testcase tc_001
Kai
  • 146
  • 1
  • 7
0

Another possible way of doing so, would be to use the following on each test case that you want to omit:

'@disabled': true,

This can simply be set to false or removed if you wish to test it.

DoN_Dan
  • 59
  • 1
  • 1
  • 7