<?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>ZeaLake</title>
	<atom:link href="http://www.zealake.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zealake.com</link>
	<description>Software Consulting</description>
	<lastBuildDate>Sun, 14 Apr 2013 04:30:25 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4</generator>
		<item>
		<title>Run all your JavaScript Jasmine tests on every commit</title>
		<link>http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/</link>
		<comments>http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/#comments</comments>
		<pubDate>Sun, 07 Apr 2013 21:44:54 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Test]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=825</guid>
		<description><![CDATA[<p>I&#8217;ve previously described how to run your QUnit tests and produce a coverage report on every commit. This works great if you happen to have chosen QUnit as your unit testing framework.</p> <p>Jasmine is another popular JavaScript unit testing framework. Here I will show how you can get the same automated feedback from your Jasmine <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/">Run all your JavaScript Jasmine tests on every commit</a></span>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve previously described how to <a href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/" title="Run all your JavaScript QUnit tests on every commit">run your QUnit tests</a> and <a href="http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/" title="No line of your JavaScript code uncovered">produce a coverage report</a> on every commit. This works great if you happen to have chosen QUnit as your unit testing framework.</p>
<p>Jasmine is another popular JavaScript unit testing framework. Here I will show how you can get the same automated feedback from your Jasmine tests using Grunt. Refer to the full sample project on GitHub: <a href="https://github.com/larsthorup/jsdevenv-jasmine" target="_blank">github.com/larsthorup/jsdevenv-jasmine</a>.</p>
<p>Assume that we have the following very simple Jasmine test in src/js/addition.test.js:</p>

<div class="wp_syntax"><table><caption>src/test/addition.test.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*global describe it expect */</span>
describe<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'addition'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">'use strict'</span><span style="color: #339933;">;</span>
&nbsp;
    it<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'should return 4 when adding 2 and 2'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        expect<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toBe</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>To run this test from the command-line, we need to install the Jasmine-plugin for Grunt. Add this line to package.json:</p>

<div class="wp_syntax"><table><caption>package.json</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">        <span style="color: #3366CC;">&quot;grunt-contrib-jasmine&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0.4.1&quot;</span><span style="color: #339933;">,</span></pre></td></tr></table></div>

<p>And install it with <code>npm install</code>. You can then configure the plugin in Gruntfile.js:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">    grunt.<span style="color: #660066;">loadNpmTasks</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'grunt-contrib-jasmine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gruntConfig.<span style="color: #660066;">jasmine</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        src<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            src<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                <span style="color: #3366CC;">'src/js/**/*.js'</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">'!src/js/**/*.test.js'</span>
            <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
            options<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                specs<span style="color: #339933;">:</span> <span style="color: #3366CC;">'src/js/**/*.test.js'</span><span style="color: #339933;">,</span>
                junit<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                    path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'output/testresults'</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    grunt.<span style="color: #660066;">registerTask</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'jasmine:src'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can now run your Jasmine tests from the command line with <code>grunt test</code>. If you try to make the test fail, it will produce output like this:</p>
<pre>
>grunt test
Running "jasmine:src" (jasmine) task
Testing jasmine specs via phantom
x.........
addition:: should return 4 when adding 2 and 2: failed
  Expected 4 to be 5. (1)
10 specs in 0.003s.
>> 1 failures
</pre>
<p>Notice that the Jasmine plugin supports JUnit-formatted test results out of the box, so you can configure your CI-server to display test results and trend lines from the files that Jasmine produces in <code>output/testresults</code>.</p>
<p>To produce a coverage report we can use the JavaScript code coverage tool Istanbul that has a plugin for the Jasmine-plugin for Grunt. To install it add this line to package.json:</p>

<div class="wp_syntax"><table><caption>package.json</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">        <span style="color: #3366CC;">&quot;grunt-template-jasmine-istanbul&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0.2.0&quot;</span></pre></td></tr></table></div>

<p>and run <code>npm install</code>. You can then configure the plugin in Gruntfile.js:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">    gruntConfig.<span style="color: #660066;">jasmine</span>.<span style="color: #660066;">istanbul</span><span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        src<span style="color: #339933;">:</span> gruntConfig.<span style="color: #660066;">jasmine</span>.<span style="color: #660066;">src</span>.<span style="color: #660066;">src</span><span style="color: #339933;">,</span>
        options<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            specs<span style="color: #339933;">:</span> gruntConfig.<span style="color: #660066;">jasmine</span>.<span style="color: #660066;">src</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">specs</span><span style="color: #339933;">,</span>
            template<span style="color: #339933;">:</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'grunt-template-jasmine-istanbul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            templateOptions<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                coverage<span style="color: #339933;">:</span> <span style="color: #3366CC;">'output/coverage/coverage.json'</span><span style="color: #339933;">,</span>
                report<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                    <span style="color: #009900;">&#123;</span>type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'html'</span><span style="color: #339933;">,</span> options<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>dir<span style="color: #339933;">:</span> <span style="color: #3366CC;">'output/coverage'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #009900;">&#123;</span>type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'cobertura'</span><span style="color: #339933;">,</span> options<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>dir<span style="color: #339933;">:</span> <span style="color: #3366CC;">'output/coverage/cobertura'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #009900;">&#123;</span>type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'text-summary'</span><span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#93;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    grunt.<span style="color: #660066;">registerTask</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'coverage'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'jasmine:istanbul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can now run your Jasmine tests with coverage analysis with the command <code>grunt coverage</code>. It will produce output like this:</p>
<pre>
>grunt coverage
Running "jasmine:istanbul" (jasmine) task
Testing jasmine specs via phantom
..........
=============================== Coverage summary ===============================
Statements   : 100% ( 33/33 )
Branches     : 89.29% ( 25/28 )
Functions    : 100% ( 4/4 )
Lines        : 100% ( 30/30 )
================================================================================

10 specs in 0.062s.
>> 0 failures

Done, without errors.
</pre>
<p>Istanbul will also product html-output that can be browsed from <code>output/coverage/index.html</code>, and will look like this:<br />
<img src="http://www.zealake.com/wp-content/uploads/2013/04/istanbul-results.png" alt="" title="istanbul-results" width="1100" height="294" class="aligncenter size-full wp-image-830" /><br />
Some CI-servers, like Jenkins, can display coverage information and trend-lines, if the coverage information is in Cobertura-format. We also configured Istanbul to output in Cobertura-format so we can point our CI-server at the files in <code>output/coverage/cobertura</code>.</p>
<p>This post is part of a series on Continuous Integration for front-end JavaScript, read the other posts in the series:</p>
<ul>
<li><a href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">Automated build for your front-end JavaScript code</a></li>
<li><a href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">Continuous Integration of your front-end JavaScript code using Travis-CI</a></li>
<li><a href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/">Run all your JavaScript QUnit tests on every commit</a></li>
<li><a href="http://www.zealake.com/2012/12/25/check-your-javascript-code-on-every-save/">Check your JavaScript code on every save</a></li>
<li><a href="http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/">No line of your JavaScript code uncovered</a></li>
<li>Run all your JavaScript Jasmine tests on every commit</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>No line of your JavaScript code uncovered</title>
		<link>http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/</link>
		<comments>http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/#comments</comments>
		<pubDate>Wed, 26 Dec 2012 00:43:36 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Test]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=739</guid>
		<description><![CDATA[<p>Being able to get fast feedback from failing unit tests is useful. And if you could easily identify the parts of your code that you still need to write tests for, you would get more comprehensive feedback.</p> <p>There is a plugin for Grunt that can use the JSCoverage tool to produce a coverage report while <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/">No line of your JavaScript code uncovered</a></span>]]></description>
			<content:encoded><![CDATA[<p>Being able to <a title="Run all your JavaScript QUnit tests on every commit" href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/">get fast feedback from failing unit tests</a> is useful. And if you could easily identify the parts of your code that you still need to write tests for, you would get more comprehensive feedback.</p>
<p>There is a plugin for Grunt that can use the <a href="http://siliconforks.com/jscoverage/" target="_blank">JSCoverage</a> tool to produce a coverage report while running your unit tests.</p>
<p>First step is to install JSCoverage. Unfortunately I have not been able to find a working npm package for JSCoverage, so what you can do is download a <a href="http://siliconforks.com/jscoverage/download.html" target="_blank">binary</a> and put it somewhere in your path. The grunt-qunit-cov plugin also needs PhantomJS to be on the path, so you might need to install PhantomJS separately for that.</p>
<p>Grunt has a grunt-qunit-cov plugin that will combine the QUnit runner with JSCoverage. </p>
<p>You will need to configure the plugin, so change your Gruntfile.js to include:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">    grunt.<span style="color: #660066;">loadNpmTasks</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'grunt-qunit-cov'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gruntConfig<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'qunit-cov'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        test<span style="color: #339933;">:</span>
        <span style="color: #009900;">&#123;</span>
            minimum<span style="color: #339933;">:</span> <span style="color: #CC0000;">0.99</span><span style="color: #339933;">,</span>
            baseDir<span style="color: #339933;">:</span> <span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span>
            srcDir<span style="color: #339933;">:</span> <span style="color: #3366CC;">'src/js'</span><span style="color: #339933;">,</span>
            depDirs<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'src/lib'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'src/test'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
            outDir<span style="color: #339933;">:</span> <span style="color: #3366CC;">'output/coverage'</span><span style="color: #339933;">,</span>
            testFiles<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'src/test/index.html'</span><span style="color: #009900;">&#93;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    grunt.<span style="color: #660066;">registerTask</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'coverage'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'qunit-cov'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In this example the plugin will fail the build if the coverage is not 99% or more.</p>
<p>To demonstrate the capabilities of JSCoverage, create an application file src/js/password.js:</p>

<div class="wp_syntax"><table><caption>src/js/password.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*global window*/</span>
window.<span style="color: #660066;">passwordStrength</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>password<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">'use strict'</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>password.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        result <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>password.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        result <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Then add a test for it in src/test/index.html:</p>

<div class="wp_syntax"><table><caption>src/test/index.html</caption><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;jsdevenv tests&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;lib/qunit.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;script src=&quot;../js/password.js&quot;&gt;&lt;/script&gt;
    &lt;script async src=&quot;lib/qunit.js&quot; onload=&quot;runTests()&quot;&gt;&lt;/script&gt;
    &lt;div id=&quot;qunit&quot;&gt;&lt;/div&gt;
    &lt;script&gt;
        runTests = function() {
            module('password');
            test('strength is 0', function () {
                 equal(passwordStrength('abc'), 0, 'abc');
            });
        }
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Then invoke</p>
<pre>  grunt coverage</pre>
<p>This will fail with the following output:</p>
<pre>  Coverage in 75%
   Error: Coverage don't reach 99%</pre>
<p>A detailed report is saved in output/coverage/out/coverage.html<br />
<img class="aligncenter size-full wp-image-773" title="jsdevenv-coverage-failed" src="http://www.zealake.com/wp-content/uploads/2012/12/jsdevenv-coverage-failed.png" alt="" width="661" height="396" /><br />
And if you click on the link to password.js, you will see exactly which lines of password.js is not covered by any of the tests.<br />
<img class="aligncenter size-full wp-image-774" title="jsdevenv-coverage-password-failed" src="http://www.zealake.com/wp-content/uploads/2012/12/jsdevenv-coverage-password-failed.png" alt="" width="661" height="396" /><br />
To fix the broken build, add the following two tests in src/test/index.html:</p>

<div class="wp_syntax"><table><caption>src/test/index.html</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">   test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'strength is 1'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     equal<span style="color: #009900;">&#40;</span>passwordStrength<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lemon'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'abc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   test<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'strength is 2'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     equal<span style="color: #009900;">&#40;</span>passwordStrength<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lemonades'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'abc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now grunt coverage will succeed with this message:</p>
<pre>  Coverage in 100%</pre>
<p>And the coverage reports looks like this:<br />
<img class="aligncenter size-full wp-image-775" title="jsdevenv-coverage-passed" src="http://www.zealake.com/wp-content/uploads/2012/12/jsdevenv-coverage-passed.png" alt="" width="661" height="396" /><br />
<img class="aligncenter size-full wp-image-776" title="jsdevenv-coverage-password-passed" src="http://www.zealake.com/wp-content/uploads/2012/12/jsdevenv-coverage-password-passed.png" alt="" width="661" height="396" /><br />
Most continuous integration systems (like Jenkins, TeamCity and Bamboo, but not yet Travis-CI) allow you to configure a set of artifacts that are made accessible for every build. See this screenshot from TeamCity:<br />
<img class="aligncenter size-full wp-image-778" title="jsdevenv-coverage-teamcity" src="http://www.zealake.com/wp-content/uploads/2012/12/jsdevenv-coverage-teamcity.png" alt="" width="491" height="239" /><br />
Now you can make sure that you maintain a comprehensive test suite for your front-end JavaScript code. Enjoy!</p>
<p>This post is part of a series on Continuous Integration for front-end JavaScript, read the other posts in the series:</p>
<ul>
<li><a href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">Automated build for your front-end JavaScript code</a></li>
<li><a href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">Continuous Integration of your front-end JavaScript code using Travis-CI</a></li>
<li><a href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/">Run all your JavaScript QUnit tests on every commit</a></li>
<li><a href="http://www.zealake.com/2012/12/25/check-your-javascript-code-on-every-save/">Check your JavaScript code on every save</a></li>
<li>No line of your JavaScript code uncovered</li>
<li><a href="http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/">Run all your JavaScript Jasmine tests on every commit</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Check your JavaScript code on every save</title>
		<link>http://www.zealake.com/2012/12/25/check-your-javascript-code-on-every-save/</link>
		<comments>http://www.zealake.com/2012/12/25/check-your-javascript-code-on-every-save/#comments</comments>
		<pubDate>Wed, 26 Dec 2012 00:34:27 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=737</guid>
		<description><![CDATA[<p>Being able to check the quality of your code and run all unit tests in your project on every commit is useful. If you could do it on every save you would get even faster feedback.</p> <p>Grunt includes a watch facility that will run a task every time some file is changed. You only need <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2012/12/25/check-your-javascript-code-on-every-save/">Check your JavaScript code on every save</a></span>]]></description>
			<content:encoded><![CDATA[<p>Being able to <a title="Automated build for your front-end JavaScript code" href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">check the quality</a> of your code and <a title="Run all your JavaScript QUnit tests on every commit" href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/">run all unit tests</a> in your project <a title="Continuous Integration of your front-end JavaScript code using Travis-CI" href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">on every commit</a> is useful. If you could do it on every save you would get even faster feedback.</p>
<p>Grunt includes a <em>watch</em> facility that will run a task every time some file is changed. You only need to tell Grunt which files to watch and what tasks to run, so change your Gruntfile.js to include:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">    grunt.<span style="color: #660066;">loadNpmTasks</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'grunt-contrib-watch'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gruntConfig.<span style="color: #660066;">watch</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        scripts<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            files<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'src/**/*.*'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
            tasks<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'lint'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#93;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can now open a new terminal window and run</p>
<pre>  grunt watch</pre>
<p>Then use your editor and edit index.html to intentionally break the test. Save the file, and watch the immediate feedback in the adjacent terminal window. Fix the test, save the file, and see that everything is back in order. You can&#8217;t get feedback much faster than that. Enjoy!</p>
<p>This post is part of a series on Continuous Integration for front-end JavaScript, read the other posts in the series:</p>
<ul>
<li><a href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">Automated build for your front-end JavaScript code</a></li>
<li><a href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">Continuous Integration of your front-end JavaScript code using Travis-CI</a></li>
<li><a href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/">Run all your JavaScript QUnit tests on every commit</a></li>
<li>Check your JavaScript code on every save</li>
<li><a href="http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/">No line of your JavaScript code uncovered</a></li>
<li><a href="http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/">Run all your JavaScript Jasmine tests on every commit</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2012/12/25/check-your-javascript-code-on-every-save/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Run all your JavaScript QUnit tests on every commit</title>
		<link>http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/</link>
		<comments>http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/#comments</comments>
		<pubDate>Wed, 26 Dec 2012 00:11:26 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Test]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=735</guid>
		<description><![CDATA[<p>Being able to check the quality of your code on every commit is useful. But it would be even more useful if you could also run all your unit tests on every commit. Here I will show how you can extend the automated build setup to also run all your QUnit tests. (Switch to different <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/">Run all your JavaScript QUnit tests on every commit</a></span>]]></description>
			<content:encoded><![CDATA[<p>Being able to <a title="Automated build for your front-end JavaScript code" href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">check the quality</a> of your code <a title="Continuous Integration of your front-end JavaScript code using Travis-CI" href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">on every commit</a> is useful. But it would be even more useful if you could also run all your unit tests on every commit. Here I will show how you can extend the automated build setup to also run all your <a href="http://qunitjs.com/" target="_blank">QUnit</a> tests. (Switch to different blog post if you are using the <a href="http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/" title="Run all your JavaScript Jasmine tests on every commit">Jasmine framework</a>)</p>
<p>Assume that we have the following very simple QUnit test in src/test/index.html:</p>

<div class="wp_syntax"><table><caption>src/test/index.html</caption><tr><td class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;jsdevenv tests&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;lib/qunit.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;script async src=&quot;lib/qunit.js&quot; onload=&quot;runTests()&quot;&gt;&lt;/script&gt;
    &lt;div id=&quot;qunit&quot;&gt;&lt;/div&gt;
    &lt;script&gt;
        runTests = function () {
            module('my module');
            test('my test', function () {
                equal(2 + 2, 4, 'kindergarten');
            });
        }
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Make sure to put qunit.css and qunit.js in src/test/lib. You can then run the test by opening index.html in a browser.<br />
<img class="aligncenter size-full wp-image-763" title="jsdevenv-qunit" src="http://www.zealake.com/wp-content/uploads/2012/12/jsdevenv-qunit.png" alt="" width="661" height="396" /><br />
<a title="Automated build for your front-end JavaScript code" href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">Grunt</a> has built-in support for running QUnit tests using <a href="http://phantomjs.org/" target="_blank">PhantomJS</a> which is a headless browser (headless means that it runs from the command line without requiring a display to open a window on &#8212; very useful for a continuous integration server)</p>
<p>You will need to tell Grunt how to run your QUnit tests. Change your Gruntfile.js to include:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">    grunt.<span style="color: #660066;">loadNpmTasks</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'grunt-contrib-qunit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gruntConfig.<span style="color: #660066;">qunit</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        src<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'src/test/index.html'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    grunt.<span style="color: #660066;">registerTask</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'qunit:src'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can then run your unit tests from the command line with</p>
<pre>  grunt test</pre>
<p>To make sure that the tests will also be run by your continuous integration server, remember to adjust the travis alias you created in Gruntfile.js for that purpose:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">grunt.<span style="color: #660066;">registerTask</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'travis'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'lint'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Commit and push to GitHub and watch Travis-CI run your unit tests. Then try making one of the tests fail and commit the change to see how your continuous integration server reports errors.</p>
<p>Some continuous integration servers (like Jenkins, TeamCity, and Bamboo, but not yet Travis-CI) can show detailed reports and graphs based on test results. To make this work you will need to ensure that the automated build produces one or more XML-files in JUnit format containing all the test results. There is a Grunt plugin, called grunt-junit, that extends the Grunt QUnit runner to produce JUnit XML files.</p>
<p>First step is to install the grunt-junit plugin, so it needs to be included in the list of development dependencies in package.json.</p>
<p>You will also need to tell Grunt that it should load the grunt-junit plugin. Add these lines to Gruntfile.js:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">    grunt.<span style="color: #660066;">loadNpmTasks</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'grunt-qunit-junit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gruntConfig.<span style="color: #660066;">qunit_junit</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        options<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            dest<span style="color: #339933;">:</span> <span style="color: #3366CC;">'output/testresults'</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>and modify this line:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">grunt.<span style="color: #660066;">registerTask</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'qunit_junit'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'qunit:src'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The grunt-junit plugin will store the JUnit XML files in the specified destination directory.<br />
Now run</p>
<pre>  grunt test</pre>
<p>and verify the contents of the XML file that is produced in the output/testresults directory:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testsuite</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;undefined&quot;</span> <span style="color: #000066;">errors</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">failures</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">tests</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">time</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testcase</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;my module&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;my test&quot;</span> <span style="color: #000066;">time</span>=<span style="color: #ff0000;">&quot;0.012&quot;</span> <span style="color: #000066;">assertions</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testcase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testsuite<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Finally, you will need to configure your continuous integration server to parse any XML-files in the output/testresults directory. Then you will be able to view detailed test results and graphs, like this trend line from TeamCity:<br />
<img class="aligncenter size-full wp-image-765" title="pluto-qunit-teamcity" src="http://www.zealake.com/wp-content/uploads/2012/12/pluto-qunit-teamcity.png" alt="" width="697" height="173" /><br />
Now, whenever you commit changes to your code, you will get an updated test report telling you immediately of any regressions that might have been introduced.</p>
<p>This post is part of a series on Continuous Integration for front-end JavaScript, read the other posts in the series:</p>
<ul>
<li><a href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">Automated build for your front-end JavaScript code</a></li>
<li><a href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">Continuous Integration of your front-end JavaScript code using Travis-CI</a></li>
<li>Run all your JavaScript QUnit tests on every commit</li>
<li><a href="http://www.zealake.com/2012/12/25/check-your-javascript-code-on-every-save/">Check your JavaScript code on every save</a></li>
<li><a href="http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/">No line of your JavaScript code uncovered</a></li>
<li><a href="http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/">Run all your JavaScript Jasmine tests on every commit</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Continuous Integration of your front-end JavaScript code using Travis-CI</title>
		<link>http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/</link>
		<comments>http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/#comments</comments>
		<pubDate>Tue, 25 Dec 2012 23:00:55 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=733</guid>
		<description><![CDATA[<p>The previous post shows how to execute build steps from the command line. Usually this is all that is required for setting up a continuous integration server that will run those steps on every commit to your project. Since the sample code behind these blog posts is available on GitHub, you can easily set up <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">Continuous Integration of your front-end JavaScript code using Travis-CI</a></span>]]></description>
			<content:encoded><![CDATA[<p>The previous post shows how to <a title="Automated build for your front-end JavaScript code" href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">execute build steps from the command line</a>. Usually this is all that is required for setting up a continuous integration server that will run those steps on every commit to your project. Since the sample code behind these blog posts is available on <a href="http://github.com/larsthorup/jsdevenv" target="_blank">GitHub</a>, you can easily set up continuous integration for the code using the hosted <a href="http://travis-ci.org/" target="_blank">Travis-CI</a> system.</p>
<p>First you need to make Travis-CI aware of who you are and what GitHub repositories you want to continuously integrate. Follow <a href="http://about.travis-ci.org/docs/user/getting-started/" target="_blank">step 1 and 2</a> from the instructions.</p>
<p>Next step is to tell Travis-CI what platform your build system is running on. Here I use Node, which Travis-CI supports out of the box. You configure Travis-CI in a file in the root directory of your project, called <em>.travis.yml</em>. It should contain these instructions:</p>

<div class="wp_syntax"><table><caption>.travis.yml</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">language<span style="color: #339933;">:</span> node_js
node_js<span style="color: #339933;">:</span>
  <span style="color: #339933;">-</span> <span style="color: #CC0000;">0.9</span></pre></td></tr></table></div>

<p>0.9 will be the version of Node that Travis-CI uses to run your builds.</p>
<p>For build systems based on Node, Travis-CI will run the build with the command</p>
<pre>  npm test</pre>
<p>To make that command execute the grunt command you need to extend the package.json file to look like this with the scripts section added:</p>

<div class="wp_syntax"><table><caption>package.json</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jsdevenv&quot;</span><span style="color: #339933;">,</span>
  ...
  <span style="color: #3366CC;">&quot;scripts&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;grunt travis&quot;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Finally you can add the following line to Gruntfile.js to specify what grunt tasks you want Travis-CI to run:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;">grunt.<span style="color: #660066;">registerTask</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'travis'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'jshint'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Commit your 3 files (package.json, .travis.yml and Gruntfile.js) to GitHub and check the status on the project-specific status page on Travis-CI. For the sample code behind these blog posts, the status page is <a href="http://travis-ci.org/larsthorup/jsdevenv" target="_blank">http://travis-ci.org/larsthorup/jsdevenv</a>.</p>
<p>Finally you can add an icon directly to your GitHub project page by adding the following markup to <em>README.md</em> (adjusted to reference your own project):</p>

<div class="wp_syntax"><table><caption>README.md</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#91;</span>Build Status<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span>https<span style="color: #339933;">:</span><span style="color: #006600; font-style: italic;">//travis-ci.org/larsthorup/jsdevenv.png)](https://travis-ci.org/larsthorup/jsdevenv)</span></pre></td></tr></table></div>

<p>Now every time you push new code to GitHub, your project page (<a href="http://github.com/larsthorup/jsdevenv" target="_blank">http://github.com/larsthorup/jsdevenv</a>) will reflect if the automated build was successful or not, like this:<br />
<a href="http://travis-ci.org/larsthorup/jsdevenv" target="_blank"><img src="http://travis-ci.org/larsthorup/jsdevenv.png" alt="" /></a><br />
And you can click on the icon and see the detailed build output on Travis-CI. Pretty cool, ain&#8217;t it?</p>
<p>This post is part of a series on Continuous Integration for front-end JavaScript, read the other posts in the series:</p>
<ul>
<li><a href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">Automated build for your front-end JavaScript code</a></li>
<li>Continuous Integration of your front-end JavaScript code using Travis-CI</li>
<li><a href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/">Run all your JavaScript QUnit tests on every commit</a></li>
<li><a href="http://www.zealake.com/2012/12/25/check-your-javascript-code-on-every-save/">Check your JavaScript code on every save</a></li>
<li><a href="http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/">No line of your JavaScript code uncovered</a></li>
<li><a href="http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/">Run all your JavaScript Jasmine tests on every commit</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automated build for your front-end JavaScript code</title>
		<link>http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/</link>
		<comments>http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/#comments</comments>
		<pubDate>Tue, 25 Dec 2012 22:27:13 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=731</guid>
		<description><![CDATA[<p>Update as of April 7, 2013: This blog series has now been extended with Jasmine and Istanbul. Update as of March 28, 2013: This blog series has now been upgraded to use Grunt-0.4.</p> <p>A lot of projects use continuous integration these days. It is still far more common for back-end projects using C#, Java or <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/">Automated build for your front-end JavaScript code</a></span>]]></description>
			<content:encoded><![CDATA[<p><em>Update as of April 7, 2013: This blog series has now been extended with Jasmine and Istanbul.</em><br />
<em>Update as of March 28, 2013: This blog series has now been upgraded to use Grunt-0.4.</em></p>
<p>A lot of projects use continuous integration these days. It is still far more common for back-end projects using C#, Java or Ruby than it is for JavaScript projects. But front-end projects using JavaScript can also benefit from continuous integration, and this series of blog posts will show you how easy it is to set up using available open source tools.</p>
<p>I will show how to do fully automated code quality analysis with JSHint, <a title="Run all your JavaScript QUnit tests on every commit" href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/">unit testing</a> with QUnit, <a href="http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/" title="No line of your JavaScript code uncovered">code coverage</a> with JSCoverage and <a title="Continuous Integration of your front-end JavaScript code using Travis-CI" href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">continuous integration</a> with Travis-CI.</p>
<p>Continuous integration means that your code will be checked automatically on every commit. If you commit often, you will get very precise information about what change introduced an error. Frequent and precise feedback is the cheapest way to keep bugs at bay.</p>
<p>All continuous integration systems rely on the ability to do the checking of the code from the command line. This blog post is about front-end JavaScript. While it is meant to be run in a browser, nonetheless there are great tools available for checking this front-end JavaScript code from the command line. Here I will focus on Node and Grunt. You can access the <a href="http://github.com/larsthorup/jsdevenv" target="_blank">sample code</a> in these blog posts at GitHub.</p>
<p>You will need only a single tool being installed globally on your system, namely <a href="http://nodejs.org/" target="_blank">Node</a>. All other tools will be added locally to your project directory.</p>
<p>First step is to install Node on your system. Follow the <a href="http://nodejs.org/" target="_blank">instructions</a>.</p>
<p>Your first goal will be to check your JavaScript code quality using <a href="http://www.jshint.com/" target="_blank">JSHint</a>, a tool that detects errors and potential problems in JavaScript code and enforces coding conventions. To be able to check the code from the command line we will use <a href="http://gruntjs.com/" target="_blank">Grunt</a>, a command line build tool for JavaScript. Grunt has built-in support for JSHint.</p>
<p>You can use the Node Package Manager, npm, to install any other tools you will be using. Npm is configured using a file called <em>package.json</em> located in the root directory of your project. To let Node know that you want to use Grunt, you will need to add Grunt as a development dependency to package.json. I have included some standard Grunt plugins, that we will need later on:</p>

<div class="wp_syntax"><table><caption>package.json</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jsdevenv&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;version&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0.1.1-1&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;A sample build environment for JavaScript&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;author&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Lars Thorup &lt;lars@zealake.com&gt;&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;devDependencies&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;grunt-cli&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0.1.6&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;grunt&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0.4.0&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;grunt-contrib-jshint&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;~0.1.1&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;grunt-contrib-qunit&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;~0.1.1&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;grunt-qunit-junit&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;~0.1.0&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;grunt-contrib-watch&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;~0.2.0&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;grunt-qunit-cov&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;~0.3.2&quot;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You can then install Grunt locally by running the following command in the root directory of your project (the directory with package.json):</p>
<pre>  npm install</pre>
<p>Next thing to do is to configure Grunt to check your code quality. Grunt needs a configuration file, and we will use <em>Gruntfile.js</em>. A basic Gruntfile.js that will check your code quality looks like this:</p>

<div class="wp_syntax"><table><caption>Gruntfile.js</caption><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*global module*/</span>
module.<span style="color: #660066;">exports</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>grunt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'use strict'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">var</span> gruntConfig <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  grunt.<span style="color: #660066;">loadNpmTasks</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'grunt-contrib-jshint'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  gruntConfig.<span style="color: #660066;">jshint</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
      options<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> bitwise<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> camelcase<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> curly<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> eqeqeq<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> forin<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> immed<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
          indent<span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> latedef<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> newcap<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> noarg<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> noempty<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> nonew<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> plusplus<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
          quotmark<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> regexp<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> undef<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> unused<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> strict<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> trailing<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
          maxparams<span style="color: #339933;">:</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> maxdepth<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> maxstatements<span style="color: #339933;">:</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      all<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
          <span style="color: #3366CC;">'Gruntfile.js'</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">'src/js/**/*.js'</span>
      <span style="color: #009900;">&#93;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  grunt.<span style="color: #660066;">initConfig</span><span style="color: #009900;">&#40;</span>gruntConfig<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Based on your style preferences you might want to <a href="http://www.jshint.com/docs/" target="_blank">configure JSHint</a> differently than the example used here.</p>
<p>Note that Gruntfile.js is a JavaScript file, so it makes sense to include this file in the list of files to be checked. You may also need to tweak the second path of the array <em>gruntConfig.jshint.all</em> if your JavaScript files are located somewhere else in the project.</p>
<p>Now you can check the quality of your JavaScript code from the command line by running:</p>
<pre>  grunt jshint</pre>
<p>Next step would be to configure your continuous integration server to execute the following two steps on every commit to your project:</p>
<pre>  npm install
  grunt lint</pre>
<p>How to do this will depend on what continuous integration software you are using. In the next blog post I will show <a title="Continuous Integration of your front-end JavaScript code using Travis-CI" href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">how to continuously integrate your JavaScript code</a> with Travis-CI.</p>
<p>This post is part of a series on Continuous Integration for front-end JavaScript, read the other posts in the series:</p>
<ul>
<li>Automated build for your front-end JavaScript code</li>
<li><a href="http://www.zealake.com/2012/12/25/continuous-integration-of-your-front-end-javascript-code-using-travis-ci/">Continuous Integration of your front-end JavaScript code using Travis-CI</a></li>
<li><a href="http://www.zealake.com/2012/12/25/run-all-your-javascript-qunit-tests-on-every-commit/">Run all your JavaScript QUnit tests on every commit</a></li>
<li><a href="http://www.zealake.com/2012/12/25/check-your-javascript-code-on-every-save/">Check your JavaScript code on every save</a></li>
<li><a href="http://www.zealake.com/2012/12/25/no-line-of-your-javascript-code-uncovered/">No line of your JavaScript code uncovered</a></li>
<li><a href="http://www.zealake.com/2013/04/07/run-all-your-javascript-jasmine-tests-on-every-commit/">Run all your JavaScript Jasmine tests on every commit</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2012/12/25/automated-build-for-your-front-end-javascript-code/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tool Improvement Spikes</title>
		<link>http://www.zealake.com/2012/09/11/tool-improvement-spikes/</link>
		<comments>http://www.zealake.com/2012/09/11/tool-improvement-spikes/#comments</comments>
		<pubDate>Wed, 12 Sep 2012 00:24:04 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=687</guid>
		<description><![CDATA[<p>A client of mine asked me this question today:</p> <p>One of our developers wants to write her BDD features using SpecFlow instead of our existing Cucumber JVM approach. Isn&#8217;t it a bad idea having two separate approaches? What are your thoughts on this.</p> <p>I think it is important that you constantly ask youself how you <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2012/09/11/tool-improvement-spikes/">Tool Improvement Spikes</a></span>]]></description>
			<content:encoded><![CDATA[<p>A client of mine asked me this question today:</p>
<blockquote><p>One of our developers wants to write her BDD features using SpecFlow instead of our existing Cucumber JVM approach. Isn&#8217;t it a bad idea having two separate approaches? What are your thoughts on this.</p></blockquote>
<p>I think it is important that you constantly ask youself how you can improve on existing practices. Conducting experiments with a new tool that seems to have benefits over an existing tool is a great way to spike a potential improvement. You are not going to keep the same tool chain over the next 10 years anyway, so better do spikes early than wait until you are forced to change by outside forces.</p>
<p>I am not sure what the benefits might be in this particular case, but from a quick look, it seems like SpecFlow might be better integrated with the rest of the tool chain (.NET, Visual Studio). However, there might be drawbacks as well, such as integration with Jenkins reporting.</p>
<p>I don&#8217;t think you should accept that one of the developers (let&#8217;s call her Sandra) uses another tool just because she wants to. I also don&#8217;t think you should encourage having two separate approaches to solving the same problem.  But you can leverage Sandra&#8217;s desire to improve by asking her to run a few experiements to demonstrate answers to some actual questions you can pose in advance, like</p>
<ul>
<li>how well does it integrate with Visual Studio in day-to-day work?</li>
<li>how easy is it to integrate with the existing build scripts and Jenkins reporting?</li>
<li>how easy is it to write and debug compared to Cucumber?</li>
</ul>
<p>Then Sandra can do a demo for the team and you can all discuss pros and cons on the basis of her results.</p>
<p>If the team agrees that Cucumber is best, Sandra should throw out her SpecFlow experiments and use Cucumber like the rest of  the team. But when the majority of the team agrees that some new tool seems to be better than the existing one, you can gradually discourage the use of the old tool and educate and encourage the use of the new one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2012/09/11/tool-improvement-spikes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automated Performance Testing</title>
		<link>http://www.zealake.com/2012/05/11/automated-performance-testing/</link>
		<comments>http://www.zealake.com/2012/05/11/automated-performance-testing/#comments</comments>
		<pubDate>Fri, 11 May 2012 15:36:41 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=661</guid>
		<description><![CDATA[<p>If you are like most test driven developers, you write automated tests for your software to get fast feedback about potential problems. Most of the tests you write will verify the functional behaviour of the software: When we call this function or press this button, the expected result is that value or that message.</p> <p>But <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2012/05/11/automated-performance-testing/">Automated Performance Testing</a></span>]]></description>
			<content:encoded><![CDATA[<p>If you are like most test driven developers, you write automated tests for your software to get fast feedback about potential problems. Most of the tests you write will verify the functional behaviour of the software: When we call this function or press this button, the expected result is that value or that message.</p>
<p>But what about the non-functional behaviour, such as performance: When we perform this query the expected speed of getting results should be no more than that many milliseconds. It is important to be able to write automated performance tests as well, because they can give us early feedback about potential performance problems. But expected performance is not as clear-cut as expected results. Expected results are either correct or wrong. Expected performance is more like a threshold: If the performance is worse than this, we want the test to fail.</p>
<p>At yesterdays <a href="http://www.meetup.com/Test-Driven-Developers-Bay-Area/events/59331662/">meetup</a> I gave the following presentation.</p>
<div style="width:425px" id="__ss_12895809"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/larsthorup/automated-performance-testing" title="Automated Performance Testing" target="_blank">Automated Performance Testing</a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/12895809" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/" target="_blank">presentations</a> from <a href="http://www.slideshare.net/larsthorup" target="_blank">Lars Thorup</a> </div>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2012/05/11/automated-performance-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CommentReader &#8211; Place your test data next to the test code</title>
		<link>http://www.zealake.com/2012/04/15/commentreader-place-your-test-data-next-to-the-test-code/</link>
		<comments>http://www.zealake.com/2012/04/15/commentreader-place-your-test-data-next-to-the-test-code/#comments</comments>
		<pubDate>Sun, 15 Apr 2012 19:58:23 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=637</guid>
		<description><![CDATA[<p>Have you ever faced the problem of writing unit tests which relies on textual test data? This is a classic issue where you usually end up putting the test data in a string variable or in an external file, depending on the amount of text. Neither of these options are particularly elegant. In this article <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2012/04/15/commentreader-place-your-test-data-next-to-the-test-code/">CommentReader &#8211; Place your test data next to the test code</a></span>]]></description>
			<content:encoded><![CDATA[<p>Have you ever faced the problem of writing unit tests which relies on textual test data? This is a classic issue where you usually end up putting the test data in a string variable or in an external file, depending on the amount of text. Neither of these options are particularly elegant. In this article however, we will show you how we came up with a more developer-friendly approach.</p>
<p><img class="size-full wp-image-641 alignnone" title="CommentReaderTest" src="http://www.zealake.com/wp-content/uploads/2012/04/CommentReaderTest.png" alt="" width="670" height="258" /></p>
<p>In our current project, we are writing code that manipulates source code. Of course we like to write unit tests, and these unit tests often needs to use some sample source code as parameters to the method under test. Consider a function that extracts all string constants from a source file. The test case should take an input source file like the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Sample
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;some value&quot;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Method<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Here is &quot;</span> <span style="color: #008000;">+</span> s <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The result of calling our function should be this list:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;some value&quot;</span>, <span style="color: #666666;">&quot;Here is&quot;</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>To write this unit test we will need a way to represent the input source file. Two options comes to mind immediately:</p>
<ol>
<li>Use an external file for the source and load this file from the unit test</li>
<li>Encode the file a string constant inside the unit test</li>
</ol>
<p>Using an external file looks like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ExtractCommentTest1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">string</span> fileContent <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;..\..\Data\ExtractCommentTest.cs&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> comments <span style="color: #008000;">=</span> ExtractComments<span style="color: #008000;">&#40;</span>fileContent<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span>, comments<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The problem with using an external file is that it makes it harder to read and modify the test case, because you need to look in two different files, both the unit test itself and the external file, not to say that you have to remember where the external file is located.</p>
<p>Using a string constant inside the unit test to hold the file looks like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ExtractCommentTest2<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">string</span> fileContent <span style="color: #008000;">=</span>
    <span style="color: #666666;">&quot;class Sample<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;{<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;	private string s = <span style="color: #008080; font-weight: bold;">\&quot;</span>some value<span style="color: #008080; font-weight: bold;">\&quot;</span>;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;	public string Method()<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;	{<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;		return <span style="color: #008080; font-weight: bold;">\&quot;</span>Here is <span style="color: #008080; font-weight: bold;">\&quot;</span> + s + <span style="color: #008080; font-weight: bold;">\&quot;</span><span style="color: #008080; font-weight: bold;">\\</span>r<span style="color: #008080; font-weight: bold;">\\</span>n<span style="color: #008080; font-weight: bold;">\&quot;</span>;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;	}<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span>
    <span style="color: #666666;">&quot;}<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> comments <span style="color: #008000;">=</span> ExtractComments<span style="color: #008000;">&#40;</span>fileContent<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span>, comments<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The problem with using an embedded string literal is that most programming languages lack adequate support for file-like string literals (Pythons tripple quoted strings being an exception). In C&#35; you need to add explicit carriage-return-line-feed and &#8211; worse &#8211; you have to escape all double quotes and backslashes within the file.</p>
<p>But here is a third option:</p>
<ol start="3">
<li>Encode the file as a comment inside the unit test</li>
</ol>
<p>Using a comment could look like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;code&gt; </span>
<span style="color: #008080; font-style: italic;">/// class Sample </span>
<span style="color: #008080; font-style: italic;">/// { </span>
<span style="color: #008080; font-style: italic;">///   private string s = &quot;some value&quot;;</span>
<span style="color: #008080; font-style: italic;">///   public string Method()</span>
<span style="color: #008080; font-style: italic;">///   { </span>
<span style="color: #008080; font-style: italic;">///     return &quot;Here is &quot; + s + &quot;\r\n&quot;; </span>
<span style="color: #008080; font-style: italic;">///   } </span>
<span style="color: #008080; font-style: italic;">///   } </span>
<span style="color: #008080; font-style: italic;">/// &lt;/code&gt;</span>
<span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ExtractCommentTest3<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">string</span> fileContent <span style="color: #008000;">=</span> CommentReader<span style="color: #008000;">.</span><span style="color: #0000FF;">GetElement</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;code&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> comments <span style="color: #008000;">=</span> ExtractComments<span style="color: #008000;">&#40;</span>fileContent<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span>, comments<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The CommentReader class is coded to take advantage of the XML-file that the C&#35; compiler can generate from the comments when compiling the source code. To support the ability to have several different embedded files in a single unit test you surround each file in an XML-element, and use the element name to lookup the right embedded file.</p>
<p>Triple comment markers are quite easy to use for this purpose, because Visual Studio will automatically insert the markers whenever you press [Enter] on your keyboard, making it easy to write the embedded file. It is also fairly easy to paste code from elsewhere; you would then need to add two sets of normal comments (Ctrl-K, Ctrl-C, Ctrl-K, Ctrl-C) and then removing the first column (Shift+Alt+arrows, Delete).</p>
<p>If you employ continuous integration you will need to make sure that the generated XML-file is available on the integration machine; however this will normally be the case, since the integration machine has just compiled the source code before running the unit tests.</p>
<p>Embedding the file in comments within the source file avoids the problems associated with the other two options: Now the file is located right beside the unit test referencing the file making it easy to read and manipulate. And there is no need to escape anything (except minimal XML escaping), because all types of code can be used verbatim.</p>
<p>The need to reference complete files as input or expected output in unit tests is not restricted to implementations of programming tools. Any program that works with data files, configuration files or other types of files will need unit tests that test the parsing and generation of these files.</p>
<p>An implementation can be found here: <a href="https://gist.github.com/2394566" target="_blank">CommentReader.cs</a></p>
<p>Getting Started</p>
<ol>
<li>Setup Visual Studio to output an XML documentation file (Project -&gt; Properties -&gt; Build -&gt; &#8220;XML documentation file&#8221;)</li>
<li>Suppress warning &#8217;1591&#8242; to stop the compiler from warning you that every method should have an XML summary</li>
<li>Start writing unit tests!</li>
</ol>
<p>This blog post was originally written in June 2006 in collaboration with Sune Gynthersen, <a href="http://www.bestbrains.dk" target="_blank">BestBrains</a>, and now rehosted here at zealake.com.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2012/04/15/commentreader-place-your-test-data-next-to-the-test-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test and Behavior Driven Development (TDD/BDD)</title>
		<link>http://www.zealake.com/2012/03/25/test-and-behavior-driven-development-tddbdd/</link>
		<comments>http://www.zealake.com/2012/03/25/test-and-behavior-driven-development-tddbdd/#comments</comments>
		<pubDate>Sun, 25 Mar 2012 18:54:15 +0000</pubDate>
		<dc:creator>Lars</dc:creator>
				<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://www.zealake.com/?p=628</guid>
		<description><![CDATA[<p>This week I gave a presentation for a client, starting on Test and Behavior Driven Development.</p> <p>In this introduction we give a high level description of what it is and why it is useful for developers. Then we go into some details on stubs and mocks, test data, UI testing, SQL testing, JavaScript testing, web <span style="color:#777"> . . . &#8594; Read More: <a href="http://www.zealake.com/2012/03/25/test-and-behavior-driven-development-tddbdd/">Test and Behavior Driven Development (TDD/BDD)</a></span>]]></description>
			<content:encoded><![CDATA[<p>This week I gave a presentation for a client, starting on Test and Behavior Driven Development.</p>
<p>In this introduction we give a high level description of what it is and why it is useful for developers. Then we go into some details on stubs and mocks, test data, UI testing, SQL testing, JavaScript testing, web services testing and how to start doing TDD/BDD on an existing code base.</p>
<div id="__ss_12150552" style="width: 425px;"><strong style="display: block; margin: 12px 0 4px;"><a title="Test and Behaviour Driven Development (TDD/BDD)" href="http://www.slideshare.net/larsthorup/test-and-behaviour-driven-development-tddbdd">Test and Behaviour Driven Development (TDD/BDD)</a></strong><object id="__sse12150552" width="425" height="355" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="wmode" value="transparent" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=tdd-and-bdd-by-lars-thorup-120325134921-phpapp02&amp;stripped_title=test-and-behaviour-driven-development-tddbdd&amp;userName=larsthorup" /><param name="allowscriptaccess" value="always" /><param name="allowfullscreen" value="true" /><embed id="__sse12150552" width="425" height="355" type="application/x-shockwave-flash" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=tdd-and-bdd-by-lars-thorup-120325134921-phpapp02&amp;stripped_title=test-and-behaviour-driven-development-tddbdd&amp;userName=larsthorup" allowFullScreen="true" allowScriptAccess="always" wmode="transparent" allowscriptaccess="always" allowfullscreen="true" /></object></p>
<div style="padding: 5px 0 12px;">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/larsthorup">Lars Thorup</a>.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.zealake.com/2012/03/25/test-and-behavior-driven-development-tddbdd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
