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)

Cross-browser-distributed testing (Advanced)


This recipe will guide you on automating client-side cross-platform/browser testing using the command-line tool Bunyip.

Getting ready

In contrast to the server-side software, JavaScript applications are being executed on the client side and therefore depend on the user browser. Normally, project specification includes the list of the browsers and platforms that the application must support. The longer the list, the harder is cross-browser-compatibility testing. For example, jQuery supports 13 browsers on different platforms. The project is fully tested in every declared environment with every single commit. That is possible thanks to the distributed testing tool TestSwarm (swarm.jquery.org). You may also hear of other tools such as Js TestDriver (code.google.com/p/js-test-driver) or Karma (karma-runner.github.io). We will take Bunyip (https://github.com/ryanseddon/bunyip) as it has swiftly been gaining popularity recently.

How does it work? You launch the tool for a test runner HTML and it provides the connect end-point (IP:port) and launches a locally installed browser if configured. As soon as you fire up the address in a browser, the client is captured by Bunyip and the connection is established. With your confirmation, Bunyip runs the tests in every connected browser to collect and report results. See the following figure:

Bunyip is built on top of the Yeti tool (www.yeti.cx) works with YUI Test, QUnit, Mocha, Jasmine, or DOH. Bunyip can be used in conjunction with BrowserStack. So, with a paid account at BrowserStack (www.browserstack.com), you can make Bunyip run your tests on hundreds of remotely hosted browsers.

To install the tool type in the console as follows:

npm install -g bunyip

Here, we recourse to the Node.js package manager is part of Node.js. So if you don't have Node.js installed, find the installation instructions on the following page:

https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

Now, we are ready to start using Bunyip.

How to do it

  1. Add to the QUnit test suite (test-suite.html) the following configuration option to prevent it from auto-starting before the plugin callback is set up:

    if (QUnit && QUnit.config) { 
        QUnit.config.autostart = false; 
    }
  2. Launch a Yeti hub on port 9000 (default configuration) and use test-suite.html.

    bunyip -f test-suite.html
    
  3. Copy the connector address (for example, http://127.0.0.1:9000) from the output and fire it up in diverse browsers. You can use Oracle VirtualBox (www.virtualbox.org) to launch browsers in virtual machines set up on every platform you need.

  4. Examine the results shown in the following screenshot: