Running QUnit tests with Jenkins and Apache Ant?
Asked Answered
S

4

14

Is it possible to execute my QUnit (javascript) unit tests from Jenkins? My build script is Apache Ant. Would Jenkins execute this as a separate Build Step, or would I need to add something in the config of my Ant build script?

Spiller answered 23/4, 2012 at 5:54 Comment(1)
For QUnit/Jenkins you can use the Arquillian-QUnit extension as well: https://mcmap.net/q/829392/-jenkins-qunit/…Villager
S
18

So, I have finally managed to figure this out.

Here's my end-to-end implementation:

  1. Install PhantomJS (http://phantomjs.org/) - I installed this in my build/tools folder

  2. Install the PhantomJS QUnit Runner script (https://gist.github.com/1588423) - also installed this in my build/tools folder

  3. Added the following target to my build.xml file:

    <target name="qunit" description="runs QUnit tests using PhantomJS">
      <!-- QUnit Javascript Unit Tests -->
      <echo message="Executing QUnit Javascript Unit Tests..."/>
      <apply executable="path-to-your-phantomjs-bin-folder/phantomjs" >
        <arg value="-path-to-your-build-tools/qunit-runner.js" />
        <arg line="--qunit path-to-your-qunit-folder/qunit.js --tests path-to-your-test-folder --juni path-where-you-want-to-write-the-JUnit-style-output/qunit-results.xml" />
        <fileset dir="${basedir}/${dir.test}" includes="tests.js" />
        <srcfile/>
      </apply>
    </target>
  1. Under my Jenkins project config, I now invoke Ant with "minify qunit"

  2. I point Jenkins to the JUnit-style output XML file

And, here is the workflow:

  1. Check changes into my repo
  2. Jenkins will poll GitHub for changes
  3. If there are any changes, Jenkins will pull down
  4. Ant will be invoked, doing the build, then running my unit tests
  5. The test results will be published in a JUnit-like XML format
  6. Jenkins will analyse this output file. If no tests failed, the build will be marked as "Success". If any tests failed, the build will be marked as "Unstable"
  7. Jenkins will deploy the web changes
  8. Jenkins will cleanup the work-area

PS: At the moment, you have to manually delete the JUnit-type XML output file. I will fix this later.

PS: Download the customized qunit.js (https://gist.github.com/2488794)

Spiller answered 3/5, 2012 at 11:21 Comment(2)
Why the customized QUnit? Aren't you now stuck with that version?Ambulator
"--juni path-where-you-want-to-write-the-JUnit-style-output/qunit-results.xml"" It should be --junit :lol:Oneman
L
4

I've written an Ant task specifically for this

https://github.com/philmander/ant-jstestrunner

Luthern answered 22/11, 2012 at 14:48 Comment(1)
Thanks for this task, Phil. It is just what I need at the moment.Caucasoid
D
1

If I understand your setup correctly, you can run Ant build step providing it with the location of your build.xml, top-level target, and -D parameters (if any). This may be of some help.

Decimate answered 23/4, 2012 at 13:46 Comment(0)
H
0

Qunit itself now maintains a phantomjs runner:

https://github.com/jquery/qunit/tree/master/addons/phantomjs

So assuming you've already installed phantomjs, grab runner.js from the link above (or get it using bower/whatever js package manager), put it somewhere jenkins can find it, and then use:

phantomjs path/to/runner.js path/to/your/qunit_tests.html

It gives minimal output like this:

$ phantomjs superlists/static/tests/runner.js accounts/static/tests/tests.html 
Took 29ms to run 11 tests. 11 passed, 0 failed.

Or like this if it fails:

$ phantomjs superlists/static/tests/runner.js accounts/static/tests/tests.html 
Test failed: sinon tests of navigator.id.watch: watch sees current user
    Failed assertion: check user, expected: current user, but was: baz
    at file:///home/harry/superlists/superlists/static/tests/qunit.js:556
    at file:///home/harry/superlists/accounts/static/tests/tests.html:69
    at file:///home/harry/superlists/superlists/static/tests/qunit.js:203
    at file:///home/harry/superlists/superlists/static/tests/qunit.js:361
    at process (file:///home/superlists/superlists/static/tests/qunit.js:1453)
    at file:///home/harry/superlists/superlists/static/tests/qunit.js:479
Took 29ms to run 11 tests. 10 passed, 1 failed.

No junit-xml integration, but at least it returns an error code if it fails, so it'll fail the build in jenkins...

$ echo $?
1
Honesty answered 6/11, 2013 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.