<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SwiftLizard Interactive {Design, Development} &#187; qunit</title>
	<atom:link href="http://www.swift-lizard.com/tag/qunit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.swift-lizard.com</link>
	<description>all about a it - freelancers life</description>
	<lastBuildDate>Thu, 18 Mar 2010 09:10:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Development with jQuery &amp; Qunit</title>
		<link>http://www.swift-lizard.com/2009/11/24/test-driven-development-with-jquery-qunit/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://www.swift-lizard.com/2009/11/24/test-driven-development-with-jquery-qunit/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 01:13:30 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[agile development]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[qunit]]></category>

		<guid isPermaLink="false">http://www.swift-lizard.com/?p=172</guid>
		<description><![CDATA[Though a friend of mine and myself lately started again with continuous integration projects for the company&#8217;s we work for, and we both like test driven development, if it is no overkill, I came across Qunit which is a library provided by jQuery for Unittesting javascript. Until last week I did not even know something [...]]]></description>
			<content:encoded><![CDATA[<p>Though a friend of mine and myself lately started again with continuous integration projects for the company&#8217;s we work for, and we both like test driven development, if it is no overkill, I came across Qunit which is a library provided by jQuery for Unittesting javascript. Until last week I did not even know something like this existed for Javascript, but I am very happy that I came across this .  Because I think unittests are a good way to assure that your code works as expected, I´d like to show you how to setup and write jQuery unittests.<span id="more-172"></span></p>
<p>First of all you will need to download the following two files from the jQuery Api &#8211; Docs Website:</p>
<p><a href="http://github.com/jquery/qunit/raw/master/qunit/qunit.js" target="_blank">Qunit.js</a><br />
<a href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" target="_blank">Qunit.css</a></p>
<p>Afterwards you will need to create a new html file with the following code:</p>
<pre name="code" class="html">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;link rel="stylesheet" href="core/qunit.css" type="text/css"/&gt;
    &lt;script src="http://code.jquery.com/jquery-latest.js"&gt; &lt;/script&gt;
    &lt;script type="text/javascript" src="core/qunit.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;
     // --- Unittest Code goes here ---
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1 id="qunit-header"&gt;QUnit example&lt;/h1&gt;
    &lt;h2 id="qunit-banner"&gt;&lt;/h2&gt;
    &lt;h2 id="qunit-userAgent"&gt;&lt;/h2&gt;
    &lt;ol id="qunit-tests"&gt;
    &lt;/ol&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>
<p>This Html file will be the page we can view the testresults on.</p>
<p>In this tutorial I only like to give you a brief overview  of what can be done how, because most of you will be familiar with writing unittests at other languages, and in the case of javascript and jQuery this will be almost the same.</p>
<p>So lets go write our first unittest:</p>
<pre name="code" class="javascript">  $(document).ready(function(){
    module("Basic Unit Test");
    test("Sample test", function()
    {
       expect(1);
       equals(divide(4,2),
        2,
        'Expected 2 as the result, result was: ' + divide(4,2));
    });
  });</pre>
<p>If you now open the the html page in your prefered browser you will see something like this:</p>
<p><a href="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-00.40.58.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" rel="shadowbox"><img class="alignnone size-medium wp-image-193" title="Qunit first test" src="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-00.40.58-300x108.png" alt="Qunit first test" width="300" height="108" /></a></p>
<p>Your unittest failes, just like one might have expected because we never implemented the divide method into our code. But lets go step by step. You might have recognized several different parts of code in the js &#8211; snippet that need a little explanation.</p>
<p>First of all we have a line &#8220;module(&#8220;Basic Unit Test&#8221;);&#8221;. By adding this we are able to separate the modules we want to test visually on the test page. If you take a look on that page after running the tests you will read &#8220;<strong>1. Basic Unit Test module</strong>: Sample test (1, 0, 1)&#8221; at the moment. So the string we have set by adding &#8220;module(&#8220;Basic Unit Test&#8221;);&#8221; to our code is displayed there as well as some more information we have provided by the lines that followed this one.</p>
<p>Next line we have added so far was &#8220;test(&#8220;Sample test&#8221;, function(){});&#8221;. This is basically the declaration of a single test that you run. The first parameter is a String that simply gives the test a name(&#8220;1. Basic Unit Test module:<strong> Sample test</strong> (1, 0, 1)&#8221;), the second one is a callback function that is called the moment we run the test by loading the page.  Lets go on analyzing the code we just tested.</p>
<p>Next thing you might have recognized is &#8220;expect(1);&#8221;, this is a simple declaration how many assertions we expect at this single test. At the moment we run only one so this is set to one. If we had set this one to two for instance, the result of our test would be that not one assertion has failed but two, because in that case we would also have forgotten to implement the second assertion.</p>
<p>Lets move on to the last line we just implemented &#8220;equals(divide(4,2),2,&#8217;Expected 2 as the result, result was: &#8216; + divide(4,2));&#8221;.  This is the actual assertion we wanted to make, just to make sure that the method &#8220;divide&#8221; works properly and returns the mathematical correct result. The Qunit method equals takes three parameters. The first one and the second one are the ones compared, the third is a String, a message you might want to print out. This message is handled by the Qunit.log() method I will show you later.</p>
<p>Besides the method equals there are two more methods provided to you for testing. They are called &#8220;ok&#8221; and &#8220;same&#8221;. While &#8220;ok&#8221; is a simple Boolean check and for this only takes two parameters, the Boolean value that has to be true in order to not make the test fail, and a message, the &#8220;same&#8221; method is kind of like equal. The only difference between those two is that equal only returns true if both, the first and the second parameter, are 100% equal ( like a === b), and same only compares the variables values vs. each other.</p>
<p>But let me give you some example code:</p>
<pre name="code" class="javascript">  var arrayContainer = {a: 'firstElement'};
  equals(arrayContainer, {a: 'firstElement'}, 'Will fail');
  same(arrayContainer, {a: 'firstElement'}, 'Will pass');</pre>
<p>After describing the basics to you we can now start to make our test working. This is quite easy just add the following to your code:</p>
<pre name="code" class="javascript">  function divide(a,b)
  {
    return a / b;
  }</pre>
<p><a href="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-01.24.46.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" rel="shadowbox"><img class="alignnone size-medium wp-image-202" title="Qunit test pass" src="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-01.24.46-300x115.png" alt="Qunit test pass" width="300" height="115" /></a></p>
<p>Your unit test passed, congratulations. But if you are a little bit like me you are missing the message we added to the equals assertion in our test, it is not displayed anywhere at our page, and even if you look at the console there will be nothing. This is because the public method Qunit.log() was indeed declared in the class file, but no code that would display the messages has been implemented. To my mind this was because the developers of Qunit wanted it to be up to your decision whether or not you like to display them.</p>
<p>It is done really easy and in this tutorial I will show you how to display them in the console of the browser (though it has one), just add the following on top of the ready callback :</p>
<pre name="code" class="javascript">  QUnit.log = function(result, message)
  {
    if (window.console &amp;&amp; window.console.log)
    {
      window.console.log(result +' :: '+ message);
    }
  }</pre>
<p>If you now reload the Unittest page,  open up Firebug for instance (in prior), and switch to the console view of Firebug you will see this:</p>
<p><a href="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-01.38.33.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" rel="shadowbox"><img class="alignnone size-medium wp-image-205" title="Qunit Test with Firebug" src="http://www.swift-lizard.com/wp-content/uploads/2009/11/Bildschirmfoto-2009-11-24-um-01.38.33-300x150.png" alt="Qunit Test with Firebug" width="300" height="150" /></a></p>
<p>By now you should have the gear to develop some unittest for jQuery or any other javascript  you use might in your project. I hope this tutorial has given you a good overview and a nice point to start from. </p>
<p>The following link provides a zip file with the summary of this tutorial:<br />
<a href='http://www.swift-lizard.com/wp-content/uploads/2009/11/jQunit.zip'>jQunit-tutorial.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.swift-lizard.com/2009/11/24/test-driven-development-with-jquery-qunit/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

