Integrating JsTestDriver tests with Hudson

JsTestDriver is one of the most efficient JavaScript unit testing tools. Integrating JsTestDriver with continuous integration tools is a mandatory step for ensuring the web application quality. In this post, I will illustrate how this sort of integration can be done between JsTestDriver and Hudson.

Installing Hudson

To install Hudson on your machine, you need to download it from http://hudson-ci.org/.

Run the following command to start Hudson:

java -jar hudson-2.2.0.war

You can start launching Hudson by typing the following address in your browser:

http://localhost:8080/

You should now be redirected to the Hudson home page.

Creating Hudson Job

First of all, you need to create a new Job in Hudson. You can do this by clicking on “New Job” and entering the Job name and selecting “Build a free-style software project” as shown in the figure below:

Configuring JsTestDriver with Hudson

Click the “Configure” link of the project. In the configuration page, enter the project description. In order to run the job periodically, you need to configure the “Build Triggers”. Check the “Build periodically” option and enter the cron-like expression. To run the job every 5 mins past the hour (for example), enter the following expression 5 * * * * as shown below in the figure:

In order to execute the JsTestDriver test command, you need to configure the “Build” section. There are many ways to do so, you can either add “Execute Shell”, “Invoke Ant or Maven2 or Maven3″, or “Execute Windows Batch command”. The latter case can be used when you are working on Windows environment. For the case of this example, I use the “Execute Windows Batch command” option and specify the path of the batch file as shown in the screenshot below:

The batch file has the following content:

java -jar JsTestDriver-1.3.4.b.jar 
      --config jsTestDriver.conf 
      --tests all 
      --testOutput c:/outputFolder 
      --reset

For the matter of simplicity, I assume that the JsTestDriver server is already started and some browsers are already captured. This step is not included in this post, however, This step also can be automated if required.

Testing the integration of JsTestDriver with Hudson

In order to test the integration of JsTestDriver with Hudson, this can be done by manually or automatically (as indicated in the “Build periodically” option). To test the integration manually, click on the “Build Now” link as shown in the screenshot below:

Click the “console output” link to check the output of the results:

setting runnermode QUIET
Firefox: Reset
...
Total 3 tests (Passed: 3; Fails: 0; Errors: 0) (3.00 ms)
  Firefox 12.0 Windows: Run 3 tests (Passed: 3; Fails: 0; Errors 0) (3.00 ms)

Notices

You may face this error while trying to run a new Job:

hudson java.io.IOException: Cannot run program "sh"

In order to fix this error, check this post:
http://www.technicaladvices.com/2012/05/04/hudson-java-io-ioexception-cannot-run-program-sh-resolution/.

Review #2 about the JavaScript Unit Testing Book

Attached below the review of Juanjo Fern├índez about the “JavaScript Unit Testing” book:

JavaScript Unit Testing

JavaScript Unit Testing Book

Improving your JavaScript code
“I suppose at least in a generic way you know what is unit testing, but to sum it up in a few words, according to Wikipedia:

Unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use.

By starting an application development, when it has relatively few lines of code all “fits” perfectly, but later when you want to add, modify, or delete certain functionality, this can become a big problem since when changing anything probably you will be breaking a few by the way.

Thanks to unit testing you can modify any module in your code, launch the tests, check failures, correct, and ready. Everything will still work in a clean, quick, and tidy way.

Once completed this short introduction, I’m going to review a book I’ve been reading these days and is about this theme: JavaScript Unit Testing:

Chapter 1 – Unit Testing JavaScript Applications: You’ll find the only theoretical part of the entire book on the first pages of this chapter, where you’ll learn what are the unit tests, why they are needed, and different approaches to create the tests: traditional unit testing and TDD.

Once this part is completed, you’ll see how works the application on which you’re going to make all the tests you’ll find in the book. It’s an application with the backend programmed in Java and a good amount of JavaScript in the frontend, that it really is what you’ll be interested to make the tests. The application allows you to complete a new user registration, authenticate and check the weather in some cities.

Don’t be scared by the fact that the application’s backend is programmed in Java: you won’t see anything of Java throughout the entire book however if you want to run the application in your computer, you’ll need to install Apache Tomcat.

Chapter 2 – Jasmine: Jasmine is a behaviour-driven development framework for testing JavaScript code. The behaviour-driven development is based on TDD.

The Jasmine test cases give more importance to the application business value over the technical details. Also are written in natural language so they can be understood by people without technical background.

In this chapter you’ll learn to install, configure and write tests with Jasmine on the application JavaScript code you saw in the first chapter, including Ajax.

Finally you’ll use together jQuery and Jasmine thanks to an available plugin that allows an easy integration between these 2 frameworks.

Chapter 3 – YUI Test: In this chapter you’ll learn to use one of the most popular unit testing frameworks: YUI Test.

YUI Test is part of the open source library for JavaScript and CSS called YUI, but with YUI Test you can test any JavaScript code, even if you don’t use YUI.

As with the previous chapter with Jasmine, in this case you’ll make unit testing with YUI Test on simple JavaScript code for later create the necessary unit testing for the application included with the book.

In addition to this you’ll see how to automate your unit testing integrating YUI Test with Selenium Driver.

Chapter 4 – QUnit: The third unit testing framework that you’ll learn to use is QUnit. It’s the framework used by all the projects related to jQuery (jQuery, jQuery UI, and jQuery Mobile) but you can use it to test any JavaScript code.

QUnit is very popular and has a simple syntax that allows you to run the tests from a browser.

Like in previous chapters, you’ll see how to install and configure QUnit, run your firsts tests, and once you’ve got a solid basis create the necessary tests for the weather application.

Chapter 5 – JsTestDriver: JsTestDriver (JSTD) is one of the most powerful frameworks for unit testing because in addition to the framework, it incorporates a test runner that can run other unit testing frameworks. All contained in a JAR file that includes everything you need to start to create your tests.

This last chapter goes similar path than previous: you’ll learn to install and configure JsTestDriver, you’ll make basic unit testing to learn its functioning and at last you’ll create the tests for the application you already know, but also you’ll learn to generate complete reports with the testing data, and to integrate JSTD with other JavaScript frameworks like Jasmine or QUnit.

Finally you’ll see how to integrate JSTD with the Eclipse IDE thanks to the JSTD Eclipse plugin.

It’s a short book compared with other books published by PacktPub but the content quality hasn’t been reduced. In any case I’ve missed information about other frameworks based on node.js that are becoming very popular like Mocha or CasperJS.

Another thing that could have improved is the application included with the book code. Since it’s a book about JavaScript it should be an application developed with node.js and not with Java, further it would have been easier to run on your computer, since to run the included application you need to install Apache Tomcat.

Beyond these minor flaws that I mentioned, It’s a highly recommended book, which includes a wealth of information and code from the most popular unit testing frameworks in the JavaScript community.

When you have finished reading it you will have a strong foundation to begin creating unit tests for your applications, which will improve your code and will optimize the time that you spend to modify existing applications.”

Reference:
http://www.amazon.com/gp/cdp/member-reviews/A3EZCWDMX7BCAQ/ref=cm_pdp_rev_title_1?ie=UTF8&sort_by=MostRecentReview#R2ZGNORO9BNK6K

The book on PacktPub:
http://www.packtpub.com/javascript-unit-testing/book

“Efficient JavaScript Unit Testing” presentation in CONFESS 2012

This presentation shows how to make “Efficient JavaScript unit Testing” using JsTestDriver. It was presented by Hazem Saleh in the CONFESS International Conference 2012 that was held in Austria. The slides are attached below.

Review #1 about the JavaScript Unit Testing Book

Attached below the review of Zubin Wadia (the CEO & Co-Founder of CiviGuard, Inc, CEO & Co-Founder of RedRock IT Solutions, and Chief Technology Officer at ImageWork Technologies) about the “JavaScript Unit Testing” book:

JavaScript Unit Testing

JavaScript Unit Testing Book

A Key Book for Complex Javascript Projects
“Javascript Unit Testing does a phenomenal job covering the Javascript Unit Testing landscape. As Javascript becomes faster and faster within the confines of a Browser, it becomes more likely that complex code will be written with it. It is also natural that larger teams will be writing code for these projects across a variety of modules and separations of concern. All these factors increase the need for disciplined Unit Tests to be written, using some of the most prominent tools available today.

Hazem Saleh sets the book up in the first chapter by emphasizing the WHY around JS unit testing, their purpose in a code-base and how they should be written/designed. If you are a seasoned programmer with a solid unit test writing habit, this chapter is less useful, and you can proceed to the next few chapters. The author also introduces the reader to an appropriate sample application that gives us a canvas on which to apply the fundamentals and variety of Unit Test tools and frameworks in subsequent chapters. The example is a classic weather-oriented one, yet complex enough to put the power and need for JS unit testing into perspective.

Chapters 2-5 walk you through some of the most prominent tools for JS unit testing available today: Jasmine, YUI Test, Q Unit and JSTD. Each one has its own quirks and idiosyncrasies that are competently elaborated upon and applied to the Weather Application sample by the Author. Two other aspects I liked about the book were (1) the Author’s care to include configuration instructions and architecture reviews for frameworks that had some pre-preparation required before you could get into them (2) the progression from some of the lighter-weight frameworks (Jasmin) to the most complex and comprehensive (JSTD). JSTD is a natural capstone chapter for the book given that it has the ability to integrate the other frameworks within its Test Run suite.

All in all, an essential book if you are a good JS developer looking to become great at what you do, and perhaps lead a team in writing orderly and well engineered code on what is the heart of the web today!”

Reference:
http://www.amazon.com/review/R1GNK4NL7XQ524

The book on PacktPub:
http://www.packtpub.com/javascript-unit-testing/book

JavaScript Unit Testing book for Hazem Saleh

JavaScript Unit Testing cover

JavaScript Unit Testing

The JavaScript Unit Testing book is a comprehensive practical guide that illustrates in details how to efficiently perform and automate JavaScript testing for web applications using the most popular JavaScript unit testing frameworks.

The book starts by explaining the concept of JavaScript unit testing and by exploring the bits of an interactive Ajax web application. Throughout the book chapters, the JavaScript part of the web application is tested using popular JavaScript unit testing frameworks (Jasmine, YUITest, QUnit, and JsTestDriver). The book illustrates how to generate JavaScript test reports, how to automate running the tests in the build and the continuous integration environments, and how to make all of the mentioned JavaScript unit testing frameworks work together in order to test the web applications in the most efficient way.

After reading this book, you will learn everything you need to know in order to make efficient JavaScript tests for the web applications. You will learn how to generate test and code coverage reports, how to automate running the JavaScript tests, and the bits of the JavaScript testing by following the book practical hands-on detailed examples.

You can order the book from: http://www.amazon.com/dp/1782160620/.