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)

Organizing test cases (Simple)


In this recipe, we will learn how to keep the tests logically organized by using the QUnit.module method.

Getting ready

While working on a real project, the number of tests can be pretty huge; for example, the jQuery test suite contains about 6,000 tests shown in the following screenshot:

Navigation on such lists without any grouping abilities would be a tough task. Luckily, QUnit provides the module method, which allows us to organize the tests and run a specific group of tests individually.

Imagine we have an application handles user interaction with the Sign In and Password Reset forms. We don't need any implementation details for now, but a high-level concept as follows:

var ResetPasswordForm = function() {
    return {
      /** Render and synchronize UI */
      init: function() {
      },
      /** Show panel */
      show: function() {},
      /** Hide panel */
      hide: function() {},
      /** Validate form input */
      validateInput: function() {}
    };
  },
  SignInForm = function() {
    return {
      /** Render and synchronize UI */
      init: function() {
      },
      /** Show panel */
      show: function() {},
      /** Hide panel */
      hide: function() {},
      /** Validate form input */
      validateInput: function() {}
    };
  }

Thus, we have two object constructors and three methods each that must be tested. Let's split up the test into two groups, one for the SignIn form and one for Rest PasswordForm.

How to do it

  1. Define a test group (module) for the SignInForm constructor.

    QUnit.module("SignInForm");
  2. Put underneath all the tests relevant to the group.

    QUnit.test( "show method", function( assert ){
        // Stub assertion
        assert.ok( true );
      });
      QUnit.test( "hide method", function( assert ){
        // Stub assertion
        assert.ok( true );
      });
      QUnit.test( "validateInput method", function( assert ){
        // Stub assertion
        assert.ok( true );
      });
    Repeat the flow for the ResetPasswordForm constructor.
    // Define the group
      QUnit.module("ResetPasswordForm");
      QUnit.test( "show method", function( assert ){
        // Stub assertion
        assert.ok( true );
      });
      QUnit.test( "hide method", function( assert ){
        // Stub assertion
        assert.ok( true );
      });
      QUnit.test( "validateInput method", function( assert ){
        // Stub assertion
        assert.ok( true );
      });
  3. Load the test runner in a browser and examine the results shown in the following screenshot: