<?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>Robert Horvick &#187; eunit</title>
	<atom:link href="http://www.roberthorvick.com/tag/eunit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.roberthorvick.com</link>
	<description>Things my wife doesn&#039;t want on the family blog...</description>
	<lastBuildDate>Mon, 01 Aug 2011 14:38:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Startup and Cleanup functions in Erlang EUnit tests</title>
		<link>http://www.roberthorvick.com/2009/07/06/startup-and-cleanup-functions-in-erlang-eunit-tests/</link>
		<comments>http://www.roberthorvick.com/2009/07/06/startup-and-cleanup-functions-in-erlang-eunit-tests/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 04:22:16 +0000</pubDate>
		<dc:creator>robert</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[eunit]]></category>

		<guid isPermaLink="false">http://www.roberthorvick.com/?p=62</guid>
		<description><![CDATA[I&#8217;ve got the basic grasp on Erlang but before I can do anything meaningful I need to learn how to test my code  EUnit is the de-facto test framework.  There are a ton of great sites that talk about testing with EUnit &#8211; I have linked to several at the end of this post.  I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got the basic grasp on Erlang but before I can do anything meaningful I need to learn how to test my code  EUnit is the de-facto test framework.  There are a ton of great sites that talk about testing with EUnit &#8211; I have linked to several at the end of this post.  I&#8217;m going to focus on the problem I ran into &#8211; needing to run some startup and cleanup code around the test suite.</p>
<p>I started by writing a very simple dictionary that lived in it&#8217;s own process.  The interface is:</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">ok</span>
<span style="color: #ff3c00;">stop</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">ok</span>
<span style="color: #ff3c00;">write</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Key</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Element</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">ok</span>
<span style="color: #ff3c00;">delete</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Key</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">ok</span>
<span style="color: #ff3c00;">read</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Key</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Value</span><span style="color: #109ab8;">&#125;</span> | not_found</pre></div></div>

<p>Calling start() spawns and registers the process the dictionary lives in.  Calling stop ends the process.  The rest do what you expect.</p>
<p>I started by writing some basic tests for each function but I didn&#8217;t really think it through.  The start test ran somewhere in the middle so all the tests prior to start either failed or timed out (because I was waiting on a receive that would never come since the process wasn&#8217;t started).  Very quickly I smacked my forehead and said &#8220;I need the test suite to have a pre and post function to start and stop the process&#8221;.</p>
<p>It took about 15 minutes of reading to realize that I needed to create a test generator with a setup tuple that defined the startup, cleanup and list of tests to run.</p>
<p>Conceptually it was easy to understand but getting the syntax right was a royal pain.  Basically you do this (I&#8217;m not sure if giving the function arity is the preferred method but it saved some keystrokes so I went with it):</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #ff3c00;">my_db_test_</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #109ab8;">&#123;</span>spawn<span style="color: #6bb810;">,</span>
    <span style="color: #109ab8;">&#123;</span>setup<span style="color: #6bb810;">,</span>
      <span style="color: #186895;">fun</span> start<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span>
      <span style="color: #186895;">fun</span> stop<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span>
      <span style="color: #109ab8;">&#91;</span>
        <span style="color: #186895;">fun</span> write_new<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span>
        <span style="color: #186895;">fun</span> write_existing<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span>
        <span style="color: #186895;">fun</span> read_existing<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span>
        <span style="color: #186895;">fun</span> read_missing<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span>
        <span style="color: #186895;">fun</span> delete_existing<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span>
        <span style="color: #186895;">fun</span> delete_missing<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span>
      <span style="color: #109ab8;">&#93;</span>
    <span style="color: #109ab8;">&#125;</span>
  <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">.</span></pre></div></div>

<p>The test methods are exactly what you&#8217;d expect so I won&#8217;t repeat them.  The syntax feels a little foreign and uncomfortable but it&#8217;s very easy to understand and I totally get why it&#8217;s written this way.  I&#8217;m just so used to <a href="http://en.wikipedia.org/wiki/MSTest">MSTest</a> attributes and <a href="http://rspec.info/">RSpec</a> that this new way is going to take a night or two to feel like an old friend.</p>
<h2>Some Eunit resources</h2>
<p><a href="http://pragdave.pragprog.com/pragdave/2007/04/testfirst_word_.html">Prag Dave &#8211; Test-First Word Wrap in Erlang</a></p>
<p><a href="http://svn.process-one.net/contribs/trunk/eunit/doc/overview-summary.html">EUnit &#8211; a Lightweight Unit Testing Framework for Erlang</a></p>
<p><a href="http://erlang.org/doc/apps/eunit/index.html">EUnit Reference Manual</a></p>
<p><a href="http://salientblue.com/codenotes/?name=erl_start">Getting Started: No Consoles!</a></p>



Share and Enjoy:


	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.roberthorvick.com%2F2009%2F07%2F06%2Fstartup-and-cleanup-functions-in-erlang-eunit-tests%2F&amp;partner=sociable" title="Print"><img src="http://www.roberthorvick.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Startup%20and%20Cleanup%20functions%20in%20Erlang%20EUnit%20tests%20-%20http%3A%2F%2Fwww.roberthorvick.com%2F2009%2F07%2F06%2Fstartup-and-cleanup-functions-in-erlang-eunit-tests%2F" title="Twitter"><img src="http://www.roberthorvick.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.roberthorvick.com%2F2009%2F07%2F06%2Fstartup-and-cleanup-functions-in-erlang-eunit-tests%2F&amp;title=Startup%20and%20Cleanup%20functions%20in%20Erlang%20EUnit%20tests&amp;bodytext=I%27ve%20got%20the%20basic%20grasp%20on%20Erlang%20but%20before%20I%20can%20do%20anything%20meaningful%20I%20need%20to%20learn%20how%20to%20test%20my%20code%C2%A0%20EUnit%20is%20the%20de-facto%20test%20framework.%C2%A0%20There%20are%20a%20ton%20of%20great%20sites%20that%20talk%20about%20testing%20with%20EUnit%20-%20I%20have%20linked%20to%20several%20at%20t" title="Digg"><img src="http://www.roberthorvick.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fwww.roberthorvick.com%2F2009%2F07%2F06%2Fstartup-and-cleanup-functions-in-erlang-eunit-tests%2F&amp;title=Startup%20and%20Cleanup%20functions%20in%20Erlang%20EUnit%20tests" title="Reddit"><img src="http://www.roberthorvick.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fwww.roberthorvick.com%2F2009%2F07%2F06%2Fstartup-and-cleanup-functions-in-erlang-eunit-tests%2F&amp;title=Startup%20and%20Cleanup%20functions%20in%20Erlang%20EUnit%20tests&amp;notes=I%27ve%20got%20the%20basic%20grasp%20on%20Erlang%20but%20before%20I%20can%20do%20anything%20meaningful%20I%20need%20to%20learn%20how%20to%20test%20my%20code%C2%A0%20EUnit%20is%20the%20de-facto%20test%20framework.%C2%A0%20There%20are%20a%20ton%20of%20great%20sites%20that%20talk%20about%20testing%20with%20EUnit%20-%20I%20have%20linked%20to%20several%20at%20t" title="del.icio.us"><img src="http://www.roberthorvick.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.roberthorvick.com%2F2009%2F07%2F06%2Fstartup-and-cleanup-functions-in-erlang-eunit-tests%2F&amp;t=Startup%20and%20Cleanup%20functions%20in%20Erlang%20EUnit%20tests" title="Facebook"><img src="http://www.roberthorvick.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.roberthorvick.com%2F2009%2F07%2F06%2Fstartup-and-cleanup-functions-in-erlang-eunit-tests%2F&amp;title=Startup%20and%20Cleanup%20functions%20in%20Erlang%20EUnit%20tests&amp;annotation=I%27ve%20got%20the%20basic%20grasp%20on%20Erlang%20but%20before%20I%20can%20do%20anything%20meaningful%20I%20need%20to%20learn%20how%20to%20test%20my%20code%C2%A0%20EUnit%20is%20the%20de-facto%20test%20framework.%C2%A0%20There%20are%20a%20ton%20of%20great%20sites%20that%20talk%20about%20testing%20with%20EUnit%20-%20I%20have%20linked%20to%20several%20at%20t" title="Google Bookmarks"><img src="http://www.roberthorvick.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.roberthorvick.com%2F2009%2F07%2F06%2Fstartup-and-cleanup-functions-in-erlang-eunit-tests%2F&amp;title=Startup%20and%20Cleanup%20functions%20in%20Erlang%20EUnit%20tests" title="StumbleUpon"><img src="http://www.roberthorvick.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.roberthorvick.com/2009/07/06/startup-and-cleanup-functions-in-erlang-eunit-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

