kumar303 / nosejs (http://pypi.python.org/pypi/NoseJS)
NoseJS is a Nose plugin for integrating JavaScript tests into a Python test suite.
Clone this repository (size: 149.7 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/kumar303/nosejs/
| commit 56: | 30972e781c9a |
| parent 55: | 2343f9d4544c |
Added note about bugfix release
6 months ago
| filename | size | last modified | ||
|---|---|---|---|---|
| example | ||||
| nosejs | ||||
| .hgignore | 69 B | 13 months ago | initial working version of Rhinous | |
| .hgtags | 102 B | 13 months ago | Added tag rel-0.9.1 for changeset 0f4e5193b26b | |
| LICENSE.txt | 1.1 KB | 13 months ago | stamped with MIT license. have fun! | |
| MANIFEST.in | 36 B | 13 months ago | restructured module so that everything is in a nosejs⦠| |
| README.txt | 8.5 KB | 6 months ago | Added note about bugfix release | |
| selftest.sh | 282 B | 13 months ago | Changed --js-test-dir to --javascript-dir | |
| setup.py | 640 B | 13 months ago | re-worded project summary |
README
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | NoseJS is a `Nose`_ plugin for integrating JavaScript tests into a Python test suite.
.. _Nose: http://www.somethingaboutorange.com/mrl/projects/nose/
.. _Rhino: http://www.mozilla.org/rhino/
.. contents::
Overview
========
NoseJS is designed for Python projects that require JavaScript code for some Web functionality. It currently has two features:
- Discover and run JavaScript tests alongside Python tests
- Validate JavaScript syntax (check for lint).
Install
=======
There are some optional external dependencies in the sections below.
You can get NoseJS with `easy_install <http://peak.telecommunity.com/DevCenter/EasyInstall>`_ ::
$ easy_install NoseJS
Or you can clone the source using `Mercurial <http://www.selenic.com/mercurial/wiki/index.cgi/Mercurial>`_ from http://bitbucket.org/kumar303/nosejs/ and install it with ::
$ python setup.py develop
Checking JavaScript Syntax
==========================
NoseJS will find JavaScript files (e.g. ``app.js``) along the Nose path and run them through the `jsl`_ command line tool to check for "lint." In other words, show you syntax errors, warnings, etc. You can install jsl on most systems using your package manager (i.e. ``port install javascript-lint`` on Mac OS X) otherwise it is available for download on the `jsl`_ site.
.. _jsl: http://www.javascriptlint.com/
Usage
-----
To check for lint without running any JavaScript unit tests, type::
$ nosetests --with-javascript --no-javascript-tests path/to/javascript
To disable lint checking, add ``--no-javascript-lint``
Running JavaScript Tests
========================
NoseJS will also find and run JavaScript test files, those that match Nose's test pattern and end in ``.js``. Currently, NoseJS supports running executing tests in `Rhino`_, a Java implementation of the JavaScript language. There is experimental support for `python-spidermonkey <http://github.com/davisp/python-spidermonkey/tree/master>`_ using the ``--spidermonkey`` switch.
Usage
-----
Assuming you've downloaded `Rhino`_ into ``~/src``, discover and run JavaScript tests with this command::
$ nosetests --with-javascript --rhino-jar ~/src/rhino1_7R1/js.jar path/to/javascript/tests
This command would look for any files along Nose's path ending in .js that match Nose's current test pattern, collect them all, then execute them using Rhino in a single Java subprocess at the end of all other tests. By default, files looking like ``test*.js`` will be collected and run.
The idea behind NoseJS is that you might have a Python web application that relies on JavaScript for some of its functionality and you want to run both Python and JavaScript tests with one command, `nosetests <http://www.somethingaboutorange.com/mrl/projects/nose/>`_. You can put these JavaScript tests wherever you want in your project.
Here is a more realistic example that shows how the `Fudge <http://farmdev.com/projects/fudge/>`_ project is tested simultaneously for Python and JavaScript functionality. Its project layout looks roughly like this::
|-- fudge
| |-- __init__.py
| |-- patcher.py
| |-- tests
| | |-- __init__.py
| | |-- test_fudge.py
| | |-- test_patcher.py
|-- javascript
| |-- fudge
| | |-- fudge.js
| | |-- tests
| | | |-- test_fudge.html
| | | `-- test_fudge.js
`-- setup.py
Both Python and JavaScript tests can be run with this command::
$ nosetests --with-javascript \
--rhino-jar ~/src/rhino1_7R1/js.jar \
--with-dom \
--javascript-dir javascript/fudge/tests/
......................................................
----------------------------------------------------------------------
Test Fake
can find objects
can create objects
expected call not called
call intercepted
returns value
returns fake
Test ExpectedCall
ExpectedCall properties
call is logged
Test fudge.registry
expected call not called
start resets calls
stop resets calls
global stop
global clear expectations
Loaded 6 JavaScript files
OK
----------------------------------------------------------------------
Ran 54 tests in 0.392s
OK
The dots are the Python tests that were run and the output below that is what Fudge's JavaScript test files printed out. Be sure to read the Caveats section below ;)
Specifying a path to JavaScript files
-------------------------------------
If JavaScript files are nested in a subdirectory, like the above example, specify that directory with::
$ nosetests --with-javascript --javascript-dir javascript/fudge/tests/ --javascript-dir ./another/dir
nosejs JavaScript namespace
---------------------------
All JavaScripts have the ``nosejs`` JavaScript namespace available for use. The following methods are available:
- **nosejs.requireFile(path)**
- Load a JavaScript file from your test script. If you require the same file multiple times, it will only
be loaded once. If the file does not start with a slash, then it should be a path relative to the directory
of the script where requireFile() was called from. For example, here is how test_fudge.js requires the
fudge library before testing::
if (typeof nosejs !== 'undefined') {
nosejs.requireFile("../fudge.js");
}
- **nosejs.requireResource(name)**
- Require a JavaScript file that is bundled with NoseJS. There are a few available resources:
jquery-1.3.1.js
Will load the `JQuery <http://jquery.com/>`_ library before loading any other tests
jquery/qunit-testrunner.js
Will load a very minimal set of JavaScript functions for testing. It is a partial implementation of the `QUnit test runner <http://docs.jquery.com/QUnit>`_ interface.
Supported methods: module(), test(), equals(), ok(), and expect()
For example, test_fudge.js uses jquery and the testrunner ::
if (typeof nosejs !== 'undefined') {
nosejs.requireResource("jquery-1.3.1.js");
nosejs.requireResource("jquery/qunit-testrunner.js");
nosejs.requireFile("../fudge.js");
}
Using the DOM
-------------
If your JavaScript under test relies on a browser-like DOM environment, it might still work! Just run::
$ nosetests --with-javascript --with-dom
This will load a copy of John Resig's `env.js <http://ejohn.org/projects/bringing-the-browser-to-the-server/>`_ script to simulate a DOM before loading any other JavaScript. There are a few very minor patches made to env.js, marked with @@nosejs in the NoseJS source.
Caveats
-------
- Currently if JavaScript tests fail, nosetests **will not** indicate failure and its exit status will be unaffected. I can't figure out a clean way to do this. Please get in touch if you'd like to help.
- In general, JavaScript tests are not very well integrated into Nose.
Wait ... Python *and* Java?
---------------------------
`Rhino`_ is pretty much the only stable, command line oriented implementation of JavaScript I know of and it's well supported by Mozilla.
Alternatively, John J. Lee created a Python binding to the fast `Spider Monkey engine <http://www.mozilla.org/js/spidermonkey/>`_, Atul Varma revived it once in a fork on `Google Code <http://code.google.com/p/python-spidermonkey/>`_, and now Paul J. Davis is working on it (in `git <http://github.com/davisp/python-spidermonkey/tree/master>`_ and it is released on `PyPI <http://pypi.python.org/pypi/python-spidermonkey>`_).
There is current experimental support for Python Spidermonkey in NoseJS using the ``--spidermonkey`` option, which switches out the JavaScript engine.
Contributing
============
Please submit `bugs and patches <http://bitbucket.org/kumar303/nosejs/issues/>`_. All contributors will be acknowledged. Thanks!
Changelog
=========
- 0.9.4
- Multiple paths for the --javascript-dir option can now be specified on multiple lines in setup.cfg
- 0.9.3
- Added --with-javascript-only option to stop execution of any other Nose tests when needed.
- 0.9.2
- **CHANGED**: The --js-test-dir option is now known as --javascript-dir
- Added experimental support for python-spidermonkey
- Added JavaScript lint check using the jsl tool
- Fixed bugs in how custom paths to JavaScript were expanded
- 0.9.1
- Fixed distribution problem
- 0.9
- Initial release
To Do
=====
- Better spidermonkey support
- Distribute a Rhino js.jar with NoseJS
- Upgrade env.js using git repo
|
