Chapter 3. FuncUnit

FuncUnit is a functional testing framework with jQuery-like syntax. It is built on top of the QUnit unit test framework.

Using FuncUnit, we can run tests in all the modern web browsers under OS X, GNU/Linux, or Windows.

Writing a test is really easy and fast, especially if the reader is familiar with the jQuery syntax and/or the QUnit framework.

FuncUnit allow us to run tests in web browsers as well as integrates it with automation tools such as Selenium, or run tests from the command line using wrappers such as PhantomJS.

FuncUnit can be integrated with build tools, such as Maven, to run as a part of the build process. It can be integrated with continuous integration tools, such as Jenkins. More information on FuncUnit can be found at the following URLs:

According to Wikipedia, functional testing is defined as follows:

Functional testing is a type of black box testing that bases its test cases on the specifications of the software component under test. Functions are tested by feeding them input and examining the output, and internal program structure is rarely considered.

In this chapter, we are going to get an overview of the FuncUnit functional testing framework, create tests, and run them against our Todo application.

Note

Unit testing versus functional testing

Unit testing tests an individual unit-like method or function, while functional testing tests the entire functionality usually through the product user interface.

Creating tests

Creating tests is writing a code that runs against application code to ensure that the code meets its design and behaves as intended. Writing tests can save time by finding bugs at the early development stage.

Lets add our first test for the Todo app:

  1. In the Todo/todo folder, create a folder named tests. Inside it, create a file named todo_test.html with following content:
    <!doctype html>
    
    <html>
      <head>
        <title>Todo List - tests</title>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="../../funcunit/qunit/qunit.css" />
      </head>
      <body>
        <h1 id="qunit-header">AutoSuggest Test Suite</h1>
    
        <h2 id="qunit-banner"></h2>
    
        <div id="qunit-testrunner-toolbar"></div>
        <h2 id="qunit-userAgent"></h2>
        <ol id="qunit-tests"></ol>
     <script src="../../steal/steal.js?todo/tests/todo_test.js"></script>	
      </body>
    </html>

    This file provides a page skeleton that is populated with the FuncUnit output, which remains the same for all future test cases—use it as a template; only the title and path to the test file will change (highlighted code).

  2. Next, create a todo_test.js file with following content:
    steal(
      'funcunit',
      function ($) {
    
        module('Todo app', {
          setup: function () {
            S.open('//todo/todo.html'),
          }
        });
    
        test('page has #todos placeholder', function () {
          ok(S('body > #todos').size(), 'The #todo is child of the body'),
        });
      }
    );

Module

A module signature is given by module(name, [lifecycle]);.

The module method comes from the QUnit project and provides the functionality to divide test into modules.

The first parameter is a string with a module name. The second one is an object with two possible methods, setup and teardown. The setup callback method runs before each test, while the teardown runs after each test in the module.

Open

An open signature is given by open(path, [success], [timeout]).

In our example, we used the open method to open a given URL and run it against the test 'page has #todos placeholder'.

Test

A test signature is given by test(name, [expected], test).

This method runs the actual test code.

The first parameter is the name of the test, while the second is required to actually run the code.

Ok

An ok signature is given by ok(state, [message]).

The ok method is a Boolean assertion. If the first parameter evaluates to true, the test passes. The second parameter is optional and it describes test.

S

S is basically copy of the jQuery shortcut $, extended by FuncUnit-specific methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.181