Software QA FYI - SQAFYI

Acceptance Testing for Node.js

By: Mike Cantelon and TJ Holowaychuk

For Node, there are two types of automated testing: unit testing and acceptance testing. Unit testing tests code logic directly and is applicable to all types of applications. Acceptance testing, however, is an additional layer of testing most commonly used for web applications. This article discusses the Tobi and Soda frameworks for acceptance testing.

This article is based on “Node.js in Action“, to be published in May 2012. It is being reproduced here by permission from Manning Publications. Manning early access books and ebooks are sold exclusively through Manning. Visit the book’s page for more information.

Acceptance Testing
Node.js in Action Acceptance testing, also called functional testing, tests outcome, not logic. So, once you’ve created a suite of unit tests for your project, acceptance testing provides an additional level of protection against bugs that unit testing might not have covered. Acceptance testing is similar, conceptually, to testing by end users following a list of things to test. Being automated, however, it’s fast and doesn’t require human labor. Acceptance testing also deals with complications created by client-side JavaScript behavior. If there’s a serious problem created by client-side JavaScript, server-side unit testing won’t catch it but thorough acceptance testing will. For example, your application may use client-side JavaScript form validation. Acceptance testing will ensure that your validation logic works, rejecting and accepting input appropriately. Or, for another example, you may have AJAX-driven administration functionality—such as the abilities to browse content to selected featured content for a website’s front page—that should only be available to authenticated users. To deal with this, you could write a test to ensure the AJAX request produces expected results when the user is logged in and write another test to make sure that those who aren’t authenticated can’t access this data.

In this article, you’ll learn how to use two acceptance testing frameworks: Tobi and Soda. While Soda provides the benefit of harnessing real browsers for acceptance testing, Tobi, which we’ll look at next, is easier to learn and get up and running.
Tobi
Tobi is an easy-to-use acceptance testing framework that emulates the browser and leverages should.js, offering access to its assertion capabilities. The framework leverages two third-party modules, jsdom and htmlparser, to simulate a web browser, allowing access to a virtual DOM.

In the world of client-side JavaScript development, web developers often use the JQuery library when they need to manipulate the DOM. JQuery can also be used on the server side, and Tobi’s use of JQuery minimizes the learning required to create tests with it. Tobi can test external sites over the network or can interface directly with Express/Connect applications.

Enter the following to install Tobi:
$ npm install tobi

The following test script is an example of using Tobi to test the login functionality of a website. The test attempts to create a todo item and then looks for it on the response page. If you run the script using Node and no exceptions are thrown, the test passes.
Listing 1 Testing a remote site’s login capability using Tobi var tobi = require(‘tobi’)
, browser = tobi.createBrowser(3000, ‘127.0.0.1’); #1
browser.get(‘/’, function(res, $){ #2
$(‘form’)
.fill({ item: ‘Floss the cat’ }) #3
.submit(function(res, $) { #4
$(‘ul’)
.html()
.indexOf(‘Floss the cat’)
.should.not.equal(-1);
});
});
#1 Creates browser
#2 Gets todo form
#3 Fills in form
#4 Submits data


The script creates a simulated browser, uses it to perform an HTTP GET request for a login form, fills in the form’s name and password fields, and then submits the form. The script then checks the contents of a with the div class messages.

If the div contains the text “Login successful.” then the test passes. If you want to test an Express application you’ve built rather than a remote site, it’s similarly straightforward. Following is an example of a one-page Express site. Note that the last line populates the exports object with the application object. This allows Tobi to use require to access the application for testing.

Full article...


Other Resource

... to read more articles, visit http://sqa.fyicenter.com/art/

Acceptance Testing for Node.js