Wednesday, October 12, 2011

Headless Jasmine testing with Hudson/Jenkins

There is some literature out there on this, but parsing through it was super-hard, especially for a person like me who's a newb to all these TDD contraptions. For instance, this very excellent link shows you how to set up headless testing, but is vague when it comes to Hudson integration: http://www.build-doctor.com/2010/12/08/javascript-bdd-jasmine/

So assuming you have Jasmine working in your browser, do the following:

1) Go to the Larry Myers' Jasmine-Reporters repo on github, and clone/download the repository you find here: https://github.com/larrymyers/jasmine-reporters

2) Set up a lib folder in the same directory as your SpecRunner.html file (this is the html file that references all the scripts Jasmine will be using for testing; you have one of these if you got Jasmine to run in a browser), with the following files in it:
    * js.jar
    * env.rhino.1.2.js
    * jasmine.console_reporter.js
    * jasmine.junit_reporter.js
    * envjs.bootstrap.js

3) To SpecRunner.html, between the script tags that contain the jasmine reporters, add the JUnitXMLReporter() as follows:
    jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter());


4) Run the following command from terminal in the same directory as the SpecRunner.html file:
    java -jar lib/js.jar -opt -1 lib/envjs.bootstrap.js SpecRunner.html

Et voila! That should produce the headless testing you needed. Now for the Hudson integration (I'm assuming Jenkins has something similar) do the following:

1) Navigate to <project-name>/Configure.

2) Under the 'Build' heading, click 'Add Build Step', and select 'Execute Shell'.

3) Add the following lines to the textbox that shows up, replacing <dir-containing-specrunner> with the appropriate path. $WORKSPACE is simply a predefined variable that contains the path to the Hudson workspace:
    #!/bin/sh
    cd $WORKSPACE/<dir-containing-specrunner>
    java -jar lib/js.jar -opt -1 lib/envjs.bootstrap.js SpecRunner.html

4) Under the 'Post-Build Actions' heading, select 'Publish JUnit test result report'

5) In the field that appears, enter the following:
    <dir-containing-specrunner>/TEST-*.xml

6) Save, and tell Hudson/Jenkins to do a build. The console output should show your tests being run, and Hudson will do a report on them. :)

Special thanks to Zac Thompson over at Stack Overflow, for helping me with steps 4-5 of the Hudson integration part. See: http://stackoverflow.com/questions/7735149/parsing-junit-xml-with-hudson/7735455#7735455

Hope this is useful!

6 comments:

  1. Hi Khalid,

    I hope you still read your blog. I am SCM/build and release guy, not a developer.

    so Jasmine is working fine in my browser, Now I am planning to integrate jasmine with Jenkins.

    Now few doubts I have for your post.

    1) Before I do anything, Do I need PhantomJs or anything else configured? like node.js etc?

    2) in your first #1 point, you said to download the "jasmine-reporters", Does this complete code have to check-in under my application project? if yes then Which direct would it be? under Libs or where SpecRunner.html located?

    The above question may sound stupid but please help.

    Thanks,
    Giriraj

    ReplyDelete
    Replies
    1. Hi Giriraj,

      1. I didn't implement Phantom or NodeJs

      2. You may look at the project setup I used here: https://github.com/mkaziz/EECS-394---Red-Team It looks like I had it in the libs folder

      Delete
    2. Thanks Khalid for replying me. Really appreciate the generosity .

      So I went thru your github project and setup the similar structure but getting following error:

      ####
      [jenkins@localhost javascript]$ java -jar lib/js.jar -opt -1 lib/envjs.bootstrap.js spec-runner.html
      [ Envjs/1.6 (Rhino; U; Linux amd64 2.6.18-348.12.1.el5xen; en-US; rv:1.7.0.rc2) Resig/20070309 PilotFish/1.2.13 ]
      Loading: spec-runner.html
      timer error
      function () {
      V = 0;
      C();
      }
      Error: Load timeout for modules: spec-config
      http://requirejs.org/docs/errors.html#timeout

      #####

      Any idea about it?

      Thanks,
      Giriraj

      Delete
  2. Thanks Khalid for replying me. Really appreciate the generosity .

    So I went thru your github project and setup the similar structure but getting following error:

    ####
    [jenkins@localhost javascript]$ java -jar lib/js.jar -opt -1 lib/envjs.bootstrap.js spec-runner.html
    [ Envjs/1.6 (Rhino; U; Linux amd64 2.6.18-348.12.1.el5xen; en-US; rv:1.7.0.rc2) Resig/20070309 PilotFish/1.2.13 ]
    Loading: spec-runner.html
    timer error
    function () {
    V = 0;
    C();
    }
    Error: Load timeout for modules: spec-config
    http://requirejs.org/docs/errors.html#timeout

    #####

    Any idea about it?

    Thanks,
    Giriraj

    ReplyDelete