Skip to content

How to run node webkit's test cases

cloud edited this page Apr 22, 2013 · 4 revisions

Prerequisite

  • 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 case that needs a http server e.g. apache. Please open port 80 and 8080 then put tests/automatic_tests/node-remote/node_remote_test.htmlto the http server document root.

So please run this:

$ npm install -g nw-gyp
$ cd src/content/nw/tests
$ npm install -d

How to run tests

The test suits is indeed a node-webkit app, so use node-webkit to run it:

$ /path-to-node-webkit src/content/nw/tests

Command line options

$ /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)

-S, --silent

Hide the browser window (run silently), and quit the app when tests are done, suitable for automatic tests system.

-o, --output

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.

-R, --reporter

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.

-g, --grep

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')
  })
})

-t, --timeout

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.

-s, --slow

Specify the slow test threshold, defaulting to 75ms. Mocha uses this to highlight test-cases that are taking too long.

-b, --bail

Only interested in the first exception? use --bail !

Automatic test

To make the testing automatic, we need to:

  1. Write results to a file.
  2. Print the results in a machine-readable format, like JSON.
  3. 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

Ignore tests that take too long to run

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

Tips

in test case node-remote we need to open a http server, e.g. apache to be the remote site. We use port 80, 8080 for test, and please put node_remote_test.html to the http server document root.

Native Modules

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`

Alao, please update node_modules using npm install -d.

Clone this wiki locally