Silicon Forks Home
Blog
JSCoverage
NRuler
Documentation
Forum

Silicon Forks

Using JSCoverage with the JsUnit test suite

This is the seventh and final (for now) article in a series on using the JSCoverage tool with various JavaScript testing frameworks:

  1. Using JSCoverage with the script.aculo.us test suite
  2. Using JSCoverage with the MochiKit test suite
  3. Using JSCoverage with the jQuery test suite
  4. Using JSCoverage with the MooTools test suite
  5. Using JSCoverage with the YUI test suite
  6. Using JSCoverage with the Dojo test suite
  7. Using JSCoverage with the JsUnit test suite (this article)

Unlike the previous articles in this series, today we will not examine the test suite for an all-purpose JavaScript library, but instead we will look at JsUnit, dedicated JavaScript test framework. JsUnit comes with its own test suite, used to test the JsUnit code itself. To run this test suite, we will download the latest release (version 2.2 as of this writing). JsUnit is easiest to use running on a web server, so we will install it in the document root of a local Apache web server, running on port 8080. Then we can load the following URL in a web browser to run the test suite:

http://127.0.0.1:8080/jsunit/testRunner.html?testPage=http://127.0.0.1:8080/jsunit/tests/jsUnitTestSuite.html&autoRun=true

The JsUnit test suite

To collect code coverage data for this test suite, we will need to run it in JSCoverage’s “inverted mode,” since it does not run well inside a frame. This means we will have to add a button to the JsUnit user interface to launch the jscoverage.html file in a separate window. Looking at the jsunit/app/main-data.html file, we see that the existing buttons are generated dynamically via script; we will generate our button the same way:

document.writeln('<input type="button" name="runButton" value="Run" onclick="top.startTests()" class="' + buttonClass + '">');
document.writeln('<input type="button" name="stopButton" value="Stop" onclick="top.stopTests()" class="' + buttonClass + '">');
document.writeln('<input type="button" name="jscoverageButton" value="Coverage Report" onclick="window.open(\'../jscoverage.html\');" class="' + buttonClass + '">');

Then we can instrument the code using the jscoverage program:

jscoverage --no-instrument=tests jsunit instrumented-jsunit

We can run the instrumented test suite at the following URL:

http://127.0.0.1:8080/instrumented-jsunit/testRunner.html?testPage=http://127.0.0.1:8080/instrumented-jsunit/tests/jsUnitTestSuite.html&autoRun=true

The JsUnit test suite, instrumented using JSCoverage

We can click on the “Coverage Report” button to view code coverage statistics:

The JSCoverage "Summary" tab

Comments are closed.