Book Image

Instant Testing with QUnit

By : Dmitry Sheiko
Book Image

Instant Testing with QUnit

By: Dmitry Sheiko

Overview of this book

<p>Automated testing is a significant part of agile development. QUnit is widely used in the jQuery Project for testing jQuery, jQuery UI, and jQuery Mobile; it can also be used to test any generic JavaScript code. As for JavaScript testing in particular, QUnit is a good choice for a framework as users can grasp the basics in a short span of time. The framework is a leading tool for unit and acceptance testing and can be used in conjunction with a wide range of third-party software. Instant Testing with QUnit covers all the essentials of QUnit and explains how you can use the framework in combination with other tools to improve your development process.</p><p>"Instant Testing with QUnit" is a hands-on guide that will help you achieve beneficial automated testing with QUnit and its plugins. This book also shows you how to engage QUnit for automated cross-browser testing and utilize it in conjunction with development automation and Continuous Integration tools.</p><p></p><p>"Instant Testing with QUnit" provides a comprehensive look into QUnit essentials and shows how you can benefit from the framework in conjunction with other tools. You will start by considering QUnit fundamentals before learning how QUnit can be used for functional testing and cross-browser automated testing with the Bunyip tool. You will also walk through a tutorial on popular QUnit plugins and then write one of your own. By the end of Instant Testing with QUnit, you will have learned how to run QUnit in the command line and how to set up the Jenkins CI server and make it perform QUnit tests.</p>
Table of Contents (7 chapters)

Building a web project (Advanced)


While working with a JavaScript project build may sound odd, it's quite relevant here. Using build , we can perform a bunch of project-related tasks with a single command. It's common practice nowadays to run tasks such as JavaScript linting, code standard validation, CSS preprocessing, and API documentation update with a build script, manually or using continuous integration tools. This recipe shows how the QUnit testing task can be added to the project build script.

Getting ready

For this task, we will use Apache Ant ( http://ant.apache.org/ ) for build automating. On Mac OS X, Ant is available by default. On Unix/Linux, it is available in ports and you can use a package manager to install Ant. As for Windows you can find the installation instructions in the official manual at ant.apache.org/manual/index.html.

Since Ant runs QUnit tests on the command line, we will use PhantomJS as it is discussed in . However, if we are to satisfy third-party software executing the build script such as continuous integration tools, we need to make the QUnit report in a specific format. Most tools accept JUnit XML. We can translate the QUnit output to JUnit format with a plugin called JUnit Logger (https://github.com/jquery/qunit-reporter-junit). So, we add to the test runner HTML the plugin JavaScipt module and the following configuration option:

     
QUnit.jUnitReport = function(report) {
console.log(report.xml);
};

But here pops up another problem. The PhantomJS Runner script now breaks JUnit XML with its own report version. You can simply comment out console.log occurrences in the QUnit.done and QUnit.testDone callback functions at the end of runner.js and save it, say as runner-muted.js.

How to do it

  1. Set up the build script as build.xml in the root of the project working directory:

    <?xml version="1.0"?>
    <!DOCTYPE project>
    <project name="tree"  basedir="." default="build">
    <target name="build" description="runs QUnit tests using PhantomJS">
      <!-- Clean up output directory -->
      <delete dir="./build/qunit/"/>  
      <mkdir dir="./build/qunit/"/>  
      <!-- QUnit Javascript Unit Tests -->
      <echo message="Executing QUnit Javascript Unit Tests..."/>
      
    </target>
    </project>
  2. Extend it with the build target to run QUnit tests:

    <?xml version="1.0"?>
    <!DOCTYPE project>
    <project name="tree"  basedir="." default="build">
    <target name="build" description="runs QUnit tests using PhantomJS">
      <!-- Clean up output directory -->
      <delete dir="./build/qunit/"/>
      <mkdir dir="./build/qunit/"/>
      <!-- QUnit Javascript Unit Tests -->
      <echo message="Executing QUnit Javascript Unit Tests..."/>
      <exec executable="/usr/local/bin/phantomjs" output="./build/qunit/qunit-results.xml">
        <arg value="./vendors/Runner/runner-muted.js" />
        <arg value="http://test.dev/qunit/tests/test11/index.html" />
      </exec>
    </target>
    </project>
  3. Run Ant in the same directory where build.xml is located:

    ant
    
  4. Examine the results shown in the following screenshot: