How to run a single test in nightwatch
Asked Answered
E

6

44

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

module.exports = {
  'Test 1':function(){},
  'Test 2':function(){}
  'Test 3':function(){}
}
Escargot answered 3/2, 2015 at 21:21 Comment(0)
M
79

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

Meridithmeriel answered 17/4, 2015 at 13:52 Comment(0)
D
9

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

Disinfection answered 24/2, 2015 at 16:27 Comment(3)
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!Escargot
Yeah, but it doesn't work for this way. If you like, check filematcher.js util lib from github nightwatch repoDisinfection
@Disinfection could you provide a solution where using nightwatch programmatically? Meaning, running a node script that invokes nightwatch.runnerLeapt
S
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
Sexagesima answered 9/2, 2015 at 10:0 Comment(0)
F
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'],
...
}
Femi answered 30/4, 2020 at 23:34 Comment(0)
C
0

you can do somthing like:

node nightwatch.js -e chrome --test tests/login_test --testcase tc_001
Cuenca answered 10/8, 2015 at 21:36 Comment(0)
T
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.

Tetrahedral answered 21/6, 2017 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.