Using JSCoverage with JSUnit

Questions, problems, suggestions, bug reports, and so on.
Post Reply
DHRoth

Using JSCoverage with JSUnit

Post by DHRoth » 2007-08-13 2:15 pm

I'm interested in finding out whether anyone has used JSCoverage to get the code coverage of JavaScript code that is tested with the JSUnit testing framework. My team has JavaScript classes that are referenced as a result of custom tags that are used in JSP pages. We use the JSUnit testing framework to test these classes by having the framework invoke test JSP pages that include the custom tags to instantiate the classes and then execute some JavaScript code within the JSPs to exercise the functions within the classes. We would like to have a code coverage tool that measures the coverage that we are getting from these unit tests.

Will JSCoverage be able to provide this ability? Does anyone have any tips on how to go about doing this?

Thanks.

siliconforks
Site Admin
Posts: 34
Joined: 2007-05-26 5:25 am

Post by siliconforks » 2007-08-13 3:49 pm

Hello DHRoth,

Using JSPs and custom tags should work fine, as long as your tags generate HTML which references static .js files. If you are actually generating JavaScript code from a .jsp file, JSCoverage will not be able to instrument that.

JSUnit is somewhat tricky: the JSUnit testRunner.html does not like being placed inside a frame, but JSCoverage requires the system under test to run within the iframe in jscoverage.html.

As a workaround, try hacking jsunit/app/main-data.html:

Code: Select all

    <script>
    function jscoverageWindow() {
      /* you may have to adjust the path here; it should point to jscoverage.html in the top of your instrumented directory */
      var coverageWindow = window.open('../../jscoverage.html');
      coverageWindow._$jscoverage = top._$jscoverage;
    }
    </script>
    <button onclick="jscoverageWindow();">Coverage Report</button>
Now, instead of running testRunner.html within jscoverage.html, you can run testRunner.html normally, run your tests (on your instrumented code), and click on the "Coverage Report" button to launch JSCoverage; then you can skip the "Browser" tab and go directly to the "Summary" tab for your coverage report.

DHRoth

It worked! . . . Now for the next question

Post by DHRoth » 2007-08-13 7:39 pm

Thank you for the work around. It worked well for me. I was able to run my JSUnit tests against the instrumented code and from the coverage button launch JSCoverage and see the summary report and drill down into the details. Excellent!!

Now, I suspect you have encountered my next question. Briefly looking at the instrumented code and jscoverage.js, it appears that all of the calculations and result generation is being done on the client. Is there any way to persist that information? Our scenario is that we deploy our web application and run our tests through CruiseControl, so there is no human interaction. What we are looking for would be similar to what we are doing with our Java code coverage where the unit tests run and the output is a series of web pages containing the code coverage summary and details that we can access at any time and for any particular build.

Have you had any requests or need for this?

Your feedback would be appreciated.

Thanks.

Dave

siliconforks
Site Admin
Posts: 34
Joined: 2007-05-26 5:25 am

Post by siliconforks » 2007-08-14 2:36 am

There is currently no way to save a coverage report, although that has occurred to me before as a potential feature. There would be several different ways of going about it:

How to persist the coverage data? Either
  1. Send the data to a server and let the server store the data, OR
  2. Write the data to the filesystem directly from JavaScript using some browser-specific I/O and appropriate security permissions.
What to persist? Either
  1. Persist the data as formatted by jscoverage.html, OR
  2. Persist the raw data (e.g., as JSON). Right now, all the code coverage data is stored in the _$jscoverage global variable. I have not yet documented the format of this - it is probably best considered internal to JSCoverage and hence still subject to change in the future - but right now it is a very simple JavaScript object:

    _$jscoverage['foo/bar.js'] = a sparse array representing coverage data for file foo/bar.js
    _$jscoverage['foo/bar.js'][n] = number of times line n in foo/bar.js was executed

    If line n in foo/bar.js does not contain code (it is blank or comment) then n is not present in the sparse array.

    Note that, to store the raw _$jscoverage object, the files jscoverage.html, jscoverage.js, etc. would not be needed; only the instrumented code is necessary (it automatically creates and populates the _$jscoverage object when it runs).

Abhishek

Post by Abhishek » 2007-10-23 2:21 pm

Thanks a lot for this nice application. Helped a lot.

Jeya

Re: Using JSCoverage with JSUnit

Post by Jeya » 2008-09-11 10:19 am

Hi,

I am new to jsunit and jscoverage. I have opened the testrunner.html and i have selected a location of a html file and pressed the run button. But the testRunner is not running the html page. So , I could not see the coverage report also. Please help me. :(

siliconforks
Site Admin
Posts: 34
Joined: 2007-05-26 5:25 am

Re: Using JSCoverage with JSUnit

Post by siliconforks » 2008-09-11 2:44 pm

The easiest way to get JsUnit working is as follows:

1. Run a web server on your local machine
2. Place all the contents of the "doc/example-jsunit" directory in the JSCoverage distribution in the document root directory of your web server.
3. Visit this URL in your web browser:

Code: Select all

http://127.0.0.1/jsunit/testRunner.html?testPage=127.0.0.1/test.html
4. Click the "Run" button. The progress bar should turn green.

Does that work for you?

Guest

Re: Using JSCoverage with JSUnit

Post by Guest » 2009-04-07 3:23 am

Hi,

Were there any enhancements made towards persisting the reports through scripts for continuous integration builds. If not can you give me some suggestions on how to accomplish that in your latest release. It would be very much helpful, now that we are starting to use it heavily.

Thanks,
Reva.

control
Posts: 1
Joined: 2009-04-23 9:37 am

Re: Using JSCoverage with JSUnit

Post by control » 2009-04-23 9:39 am

Hi,

I want to use JSCoverage with JSUnit via maven. I have found maven plugin for JSUnit but not for JSCoverage. Can someone please help me on this.

Regards,
Chirag Trivedi

Post Reply