How to run node webkit's test cases
NOTE: some content in this wiki applies only to 0.12 and earlier versions. For official documentation on 0.13 and later, see http://docs.nwjs.io
- node-webkit didn't ship third party node modules required for testing, you need to install them.
- We use
nw-gyp
to build native modules. - We have test cases that need a http server. And we have set up a simple nodejs server with port 8123 and 8124
opened. The http server document root is
tests/server
. You can add ports intests/server/server.js
So please run this:
$ npm install -g nw-gyp
$ cd src/content/nw/tests
$ npm install -d
The test suits is indeed a node-webkit app, so use node-webkit to run it:
$ /path-to-node-webkit src/content/nw/tests
$ /path-to-node-webkit src/content/nw/tests --help
Usage: nw-test [options]
Options:
-h, --help output usage information
-V, --version output the version number
-S, --silent hide the browser window and quit when done (run silently)
-R, --reporter <name> specify the reporter to use
-g, --grep <pattern> only run tests matching <pattern>
-i, --invert inverts --grep matches
-t, --timeout <ms> set test-case timeout in milliseconds [2000]
-s, --slow <ms> "slow" test threshold in milliseconds [75]
-b, --bail bail after first test failure
-A, --async-only force all tests to take a callback (async)
Hide the browser window (run silently), and quit the app when tests are done, suitable for automatic tests system.
Output result to a file, this is usually used together with json
reporter, so we can write the results of test to the file in the format of
JSON.
The --reporter
option allows you to specify the reporter that will be used,
defaulting to html
. This flag may also be used to utilize third-party
reporters. For example if you npm install mocha-lcov-reporter
you may then
do --reporter mocha-lcov-reporter
.
The --grep
option when specified will trigger mocha to only run tests matching
the given pattern which is internally compiled to a RegExp.
Suppose for example you have api
related tests, as well as app
related
tests, as shown in the following snippet; One could use --grep api
or
--grep app
to run one or the other. The same goes for any other part of a
suite or test-case title, --grep users
would be valid as well, or even
--grep GET
.
describe('api', function(){
describe('GET /api/users', function(){
it('respond with an array of users')
})
})
describe('app', function(){
describe('GET /users', function(){
it('respond with an array of users')
})
})
Specifies the test-case timeout, defaulting to 2 seconds. To override you
may pass the timeout in milliseconds, or a value with the s suffix, ex:
--timeout 2s
or --timeout 2000
would be equivalent.
Specify the slow
test threshold, defaulting to 75ms. Mocha uses this to
highlight test-cases that are taking too long.
Only interested in the first exception? use --bail
!
To make the testing automatic, we need to:
- Write results to a file.
- Print the results in a machine-readable format, like JSON.
- Run tests silently and quit when done.
In node-webkit's test, we can do:
$ /path-to-node-webkit src/content/nw/tests --silent --output result.json --reporter json
Some tests like visiting html5test.com and getting its score would take too
long to run for normal tasks, these tests are usually marked as long-to-run
,
so you can use following command to filter out them:
$ /path-to-node-webkit src/content/nw/tests --grep long-to-run -i
in test case like node-remote
, we need to open a http server
to be the remote site. It is automatically opened when we run tests. We use port 8123, 8124 for test.
The http server document root is tests/server
. We have put some resources there e.g. node_remote_test.html
.
When there is a new nw release, We hope you to rebuild native modules.
We use tools/build_native_modules.py
to build our native modules:
$ cd src/content/nw
$ python tools/build_native_modules.py`
Also, please update node_modules
using npm install -d
.