<?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>Mike&#039;s Blabberings &#187; unit testing</title>
	<atom:link href="http://www.mike-griffith.com/blog/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mike-griffith.com/blog</link>
	<description>on software, testing, and the web.</description>
	<lastBuildDate>Tue, 29 Jun 2010 14:37:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Python decorators, SRP, and testability</title>
		<link>http://www.mike-griffith.com/blog/2010/06/python-decorators-srp-and-testability/</link>
		<comments>http://www.mike-griffith.com/blog/2010/06/python-decorators-srp-and-testability/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 06:30:02 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.mike-griffith.com/blog/?p=504</guid>
		<description><![CDATA[On the SRP:
For those unfamiliar with the Single Responsibility Principle (SRP), it states that there should never be more than one reason for a class to change.
That is to say: do one thing, do it well.
Decorators (not to be confused with the decorator pattern) can add behaviors or side-effects to a method, and this can [...]]]></description>
			<content:encoded><![CDATA[<p><strong>On the SRP:</strong><br />
For those unfamiliar with the <a href="http://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod" target="_blank">Single Responsibility Principle</a> (SRP), it states that <em>there should never be more than one reason for a class to change</em>.</p>
<p>That is to say: do one thing, do it well.</p>
<p><a href="http://www.python.org/dev/peps/pep-0318/" target="_blank">Decorators</a> (not to be confused with the <a href="http://en.wikipedia.org/wiki/Decorator_pattern" target="_blank">decorator pattern</a>) can add behaviors or side-effects to a method, and this can be dangerous.  It seems harmless, because by adding a decorator, you&#8217;re likely taking boilerplate code and shuffling it elsewhere.  However, they often encourage badness because of how easy it is to add these behaviors.</p>
<p><strong>On testing decorated methods:</strong></p>
<p>Adding an @decorator to a python object unarguably makes the undecorated code difficult to test in isolation.  Decorators are applied at compile-time, and cannot be mocked or made to not-execute without some pain.</p>
<p>There are certainly a few common tricks that can help test a decorated method with minimal side-effects, but they require changes to the decorator itself.  There&#8217;s just plain and simple no way to un-decorate a method for isolated testing.</p>
<p><strong>Let&#8217;s look at a few common examples:</strong></p>
<p><strong><em>@expose</em></strong>: register a URL for a view function in a web framework</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> BlogPostController<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    @expose<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/blog/{post_id}&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request, post_id=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; show a blog post &quot;&quot;&quot;</span>
        post = adapter.<span style="color: black;">get_post</span><span style="color: black;">&#40;</span>post_id<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> render<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;show_entry.html&quot;</span>, <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>post=post<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Without the `@expose`, your `show_entry` knows how to get a given post and render it in the proper template.  With the decorator, it also now knows which URL corresponds to that.  You now have multiple reasons for this block of code to change, including pointing to a different URL or using a different template.  It&#8217;s preferred to have a separate module for managing which urls point to which views.</p>
<p>Harm factor: low.  Ick factor: medium &#8211; high.</p>
<p><strong><em>@cache</em></strong>: try to get the result from memcache, otherwise, execute the function and stick it in cache for next time</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> PostAdapter<span style="color: black;">&#40;</span>DataAdapter<span style="color: black;">&#41;</span>:
    @cache
    <span style="color: #ff7700;font-weight:bold;">def</span> get_post<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, post_id<span style="color: black;">&#41;</span>
        <span style="color: #483d8b;">&quot;&quot;&quot; grab a blog post from the database &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Post<span style="color: black;">&#41;</span>.<span style="color: #008000;">filter</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span>=post_id<span style="color: black;">&#41;</span>.<span style="color: black;">one</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># SQLAlchemy folks need to talk to Mr Demeter...</span></pre></div></div>

<p>OK this seems cool right?  You only hit the database when you have to, otherwise we get it even quicker by looking it up in the cache.</p>
<p>What happens if you want to disable caching?  A separate cache abstraction layer would reduce volatility in your data adapter.</p>
<p>And what does the unit test look like?</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> TestGettingAPost<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> setup<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">query</span> = Mock<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#don't hit the production database!</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">post_adapter</span> = PostAdapter<span style="color: black;">&#40;</span>query=<span style="color: #008000;">self</span>.<span style="color: black;">query</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> test_getting_a_post<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">assert</span> <span style="color: #008000;">self</span>.<span style="color: black;">post_adapter</span>.<span style="color: black;">get_post</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">123</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Damn, there&#8217;s no way to mock out the @cache decorator so it doesn&#8217;t run.  Try to, I dare you.  You&#8217;re likely going to actually get post 123 from your production memcache.  Crappy.  The only thing you can do is make the @cache grab the cache implementation from the PostAdapter instance (and mock that out in you test), or find some other sneaky way of disabling caching for test runs.  But the @cache decorator isn&#8217;t all self-contained and fun anymore.</p>
<p>Harm factor: medium &#8211; high.</p>
<p><strong><em>@validate</em></strong>: Make sure the request matches the specified schema, otherwise hand-off to error handler</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> EditPostController<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> _save_error<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request, errors<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; @validate decorator kicked flow here, redisplay edit page with errors &quot;&quot;&quot;</span>
        ...
    @validate<span style="color: black;">&#40;</span>schema, error_handler=_save_error<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> save<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>
        post = <span style="color: #008000;">self</span>.<span style="color: black;">schema</span>.<span style="color: black;">to_python</span><span style="color: black;">&#40;</span>request.<span style="color: black;">params</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">post_adapter</span>.<span style="color: black;">save_post</span><span style="color: black;">&#40;</span>post<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> redirect<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/blog/{0}&quot;</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>post_id<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Without this @validate, there would be a lot of boilerplate code inside the `save` method.  With it, any unit test for the save method will be likely linked to your schema.  You&#8217;d have to make an actual valid request in order to test this method.  That&#8217;s outside of any tests for your schema directly. That means double-coverage but 2 tests to update when requirements shift.</p>
<p>Harm factor: low-medium</p>
<p><strong>Conclusions (or tl;dr):</strong></p>
<p>As easy as it is to become infatuated with Python decorators, they definitely encourage you to violate the SRP.  This can create a myriad of problems:</p>
<ul>
<li>Difficultly in isolating system under test</li>
<li>Added complexity to enable testing</li>
<li>Redundant redundant unit tests</li>
<li>Making a code module more volatile than it ought to</li>
</ul>
<p>They still have some valid use cases and can lead to cleaner code, however,  as Master Yoda once said, &#8220;when you look at the dark side, careful you must be&#8230;&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2010/06/python-decorators-srp-and-testability/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Including external data with your unit tests</title>
		<link>http://www.mike-griffith.com/blog/2009/01/including-external-data-with-your-unit-tests/</link>
		<comments>http://www.mike-griffith.com/blog/2009/01/including-external-data-with-your-unit-tests/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 20:06:21 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.mike-griffith.com/blog/?p=191</guid>
		<description><![CDATA[We generally preach that you should not use real data in your unit tests.  You should instead mock said data out in an effort to lesson potential side-effects, including degrading test performance and adversely affecting live data.
Sometimes, you&#8217;d like to test actual file handling in a unit test.  If you do, you should [...]]]></description>
			<content:encoded><![CDATA[<p>We generally preach that you should not use real data in your unit tests.  You should instead mock said data out in an effort to lesson potential side-effects, including degrading test performance and adversely affecting live data.</p>
<p>Sometimes, you&#8217;d like to test actual file handling in a unit test.  If you do, you should provide a sample data set packaged with your code in a data directory within your test directory.</p>
<p>Your package would then resemble the following structure:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/package
    /tests
        /data
            data.txt
        __init__.py
        test_module.py
    __init__.py
    module.py</pre></div></div>

<p>In your test, you&#8217;ll need to provide a relative path to the data file.  To do this, use the special <code>__file__</code> attribute of the package to provide a starting point.  This will let you know where the module lives on disk regardless of where the package is installed.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">from</span> package.<span style="color: black;">tests</span> <span style="color: #ff7700;font-weight:bold;">import</span> __file__ <span style="color: #ff7700;font-weight:bold;">as</span> test_directory
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _data_dir<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>test_directory<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'data'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> test_sample_data<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    file_path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>_data_dir<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'data.txt'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>file_path<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> f:
        package.<span style="color: black;">module</span>.<span style="color: black;">do_something</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span></pre></div></div>

<p>There you have it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2009/01/including-external-data-with-your-unit-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Readying Javascript for the Enterprise with Automated Testing</title>
		<link>http://www.mike-griffith.com/blog/2009/01/readying-javascript-for-the-enterprise-with-automated-testing/</link>
		<comments>http://www.mike-griffith.com/blog/2009/01/readying-javascript-for-the-enterprise-with-automated-testing/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 04:51:46 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.mike-griffith.com/blog/?p=152</guid>
		<description><![CDATA[&#60;flame&#62;
Javascript seems to gets left in the dust when we consider code quality.  It&#8217;s been rigged, scattered, and smeared every which way from Sunday.  Most sites of significant complexity have a severe amount of legacy, rotting code buried deep in a countless number of files.  There is little separation of concerns in [...]]]></description>
			<content:encoded><![CDATA[<p>&lt;flame&gt;<br />
Javascript seems to gets left in the dust when we consider code quality.  It&#8217;s been rigged, scattered, and smeared every which way from Sunday.  Most sites of significant complexity have a severe amount of legacy, rotting code buried deep in a countless number of files.  There is little separation of concerns in places, where event handlers written alongside presentation markup are validating data and controlling flow simultaneously.  At times, the code gets hacked up at the end of a project to deal with browser flaws and edge cases, and quickly mutates from something not-so-bad to an ugly beast.<br />
&lt;/flame&gt;</p>
<p>Lo and behold, there&#8217;s a way out!  There&#8217;s very few valid excuses for not writing cleaner, prettier, more maintainable Javascript. Many of the same <a href="http://www.python.org/dev/peps/pep-0020/">principles preached in Python</a> hold true.</p>
<p>There are some prevalent concepts that can help us write better code.  Understanding and applying <strong>unobtrusive Javascript</strong> can help alleviate everything from cluttered namespaces to browser differences and performance issues  &#8212; I suggest reading <a href="http://icant.co.uk/articles/seven-rules-of-unobtrusive-javascript/">the 7 rules</a>.  Following directly from that, <strong>progressive enhancement</strong> principles are fantastic to allow for graceful degradation and accessibility.  Do with Javascript what we know we should do with CSS.  Here&#8217;s some <a href="http://www.alistapart.com/articles/progressiveenhancementwithjavascript">food for thought about progressive enhancement</a>.  Additionally, writing <strong>loosely coupled</strong> code (i.e. methods not tied directly to the DOM unless necessary) provides a separation of concerns that helps with debugging and testability.  Finally, automating the testing of your code to ensure it&#8217;s doing what you think it should do (prior to sending it to QA for them to check) is powerful in and of itself.</p>
<p>If you&#8217;ve been bitten by the testing bug and find yourself wanting to test your Javascript code, you&#8217;ve come to the right place.  The first question you should ask yourself is, &#8220;what should I be testing?&#8221;.  I will <strong>focus on unit testing</strong> in this article.  You should also be aware that there are powerful tools (e.g. Selenium) for integration and browser testing.  But first things first.</p>
<p>So, what are common things done in Javascript that might need to be unit-tested?  Here are a few examples:</p>
<ul>
<li>Client-side validation.  Let&#8217;s ensure all bases are covered with validators that are written to check email addresses, birthdays, maximum lengths, etc.</li>
<li>Methods that compute window sizes or element positions.  There&#8217;s often lots of math, and edge cases can be killer.</li>
<li>Basic things ingrained in the Dojo framework, such as class inheritance.  You&#8217;ll want to ensure that your class behaves as you expect.</li>
</ul>
<p>Let&#8217;s take the example of a email address validator and see how we might write it and test it.  Here&#8217;s one method, which happens to validate a string expected to be a valid email address, with a length less than the specified maximum.  This would be located at <code>$JS/dojo1.2/src/app/validators.js</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'app.validators'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>email<span style="color: #339933;">,</span> maxLength<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>email.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> maxLength<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pattern <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> pattern.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There are several things we might want to test.  For example, we should test that the maximum length functionality works.  Additionally, we should test that it doesn&#8217;t allow emails without a domain to pass through.  And we don&#8217;t want to leave out testing that a valid email can pass through.  In an ideal world, we would test every reasonable branch through the function, and through the regular expression itself.</p>
<p>Here are 3 test cases, presented in a format acceptable for the D.O.H. framework</p>
<p><strong>Case 1: Too long of an email is invalid</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    doh.<span style="color: #660066;">assertFalse</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'123456'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Case 2: Email without a domain is invalid</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    doh.<span style="color: #660066;">assertFalse</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo@.'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Case 3: A valid email should validate as such</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    doh.<span style="color: #660066;">assertTrue</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo@bar.baz'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We can then insert these into a D.O.H.-compatible test package.  Open an editor to <code>$JS/dojo1.2/src/app/tests/test_validation.js</code>, and register the tests from above as shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'app.validators'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
doh.<span style="color: #660066;">register</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;app.tests.test_validation&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'test_validate_email_too_long'</span><span style="color: #339933;">,</span>
        runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            doh.<span style="color: #660066;">assertFalse</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'123456'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'test_validate_email_nodomain'</span><span style="color: #339933;">,</span>
        runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            doh.<span style="color: #660066;">assertFalse</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo@.'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'test_validate_email_ok'</span><span style="color: #339933;">,</span>
        runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            doh.<span style="color: #660066;">assertTrue</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo@bar.baz'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The last steps are to plug it into a test &#8220;module&#8221; (so it can be discovered by the D.O.H. runner), and run it to ensure things are working as desired.  Point an editor to <code>$JS/dojo1.2/src/app/tests/module.js</code>, and add the following line:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;app.tests.test_validation&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>D.O.H. provides a browser-based test runner for executing tests.  Assuming you have Dojo 1.2 available on your server, modify the following path to suite your installation: <code>http://<em>servername</em>/js/dojo1.2/src/util/doh/runner.html?testModule=app.tests.module</code>.</p>
<p>Hold tight for the next installment, which will include mocking in Javascript and introduce the Rhino runner.  Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2009/01/readying-javascript-for-the-enterprise-with-automated-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On my mind today</title>
		<link>http://www.mike-griffith.com/blog/2008/09/on-my-mind-today/</link>
		<comments>http://www.mike-griffith.com/blog/2008/09/on-my-mind-today/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:36:26 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[web security]]></category>

		<guid isPermaLink="false">http://blog.rideshootlive.com/?p=68</guid>
		<description><![CDATA[I&#8217;ve got some ideas that I need to find time to work on.  In no particular order:

Port MockMe to the Dojo javascript framework
Utilize browser history sniffing to build a generalized user segmentation framework
Write a Ubiquity plugin for spreeder (to easily speed read a page in Firefox)
Port OWASP&#8217;s AntiSamy project to Python, to provide comprehensive [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got some ideas that I need to find time to work on.  In no particular order:</p>
<ul>
<li>Port <a href="http://johanneslink.net/projects/mockme.html">MockMe</a> to the Dojo javascript framework</li>
<li>Utilize <a href="http://www.niallkennedy.com/blog/2008/02/browser-history-sniff.html">browser history sniffing</a> to build a generalized user segmentation framework</li>
<li>Write a <a href="http://labs.mozilla.com/projects/ubiquity/">Ubiquity</a> plugin for <a href="http://www.spreeder.com/">spreeder</a> (to easily speed read a page in Firefox)</li>
<li>Port <a href="http://code.google.com/p/owaspantisamy/">OWASP&#8217;s AntiSamy project</a> to Python, to provide comprehensive XSS protection</li>
</ul>
<p>None of these are terribly difficult to get started, and with the summer winding down, I might have a few minutes of free time to start working on some.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2008/09/on-my-mind-today/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
