<?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>Sat, 08 May 2010 23:27:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.roberthorvick.com%252F2009%252F07%252F06%252Fstartup-and-cleanup-functions-in-erlang-eunit-tests%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.roberthorvick.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a>
	<a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DStartup%2520and%2520Cleanup%2520functions%2520in%2520Erlang%2520EUnit%2520tests%2520-%2520http%253A%252F%252Fwww.roberthorvick.com%252F2009%252F07%252F06%252Fstartup-and-cleanup-functions-in-erlang-eunit-tests%252F';" 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" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.roberthorvick.com%252F2009%252F07%252F06%252Fstartup-and-cleanup-functions-in-erlang-eunit-tests%252F%26amp%3Btitle%3DStartup%2520and%2520Cleanup%2520functions%2520in%2520Erlang%2520EUnit%2520tests%26amp%3Bbodytext%3DI%2527ve%2520got%2520the%2520basic%2520grasp%2520on%2520Erlang%2520but%2520before%2520I%2520can%2520do%2520anything%2520meaningful%2520I%2520need%2520to%2520learn%2520how%2520to%2520test%2520my%2520code%25C2%25A0%2520EUnit%2520is%2520the%2520de-facto%2520test%2520framework.%25C2%25A0%2520There%2520are%2520a%2520ton%2520of%2520great%2520sites%2520that%2520talk%2520about%2520testing%2520with%2520EUnit%2520-%2520I%2520have%2520linked%2520to%2520several%2520at%2520t';" 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" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.roberthorvick.com%252F2009%252F07%252F06%252Fstartup-and-cleanup-functions-in-erlang-eunit-tests%252F%26amp%3Btitle%3DStartup%2520and%2520Cleanup%2520functions%2520in%2520Erlang%2520EUnit%2520tests';" 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" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.roberthorvick.com%252F2009%252F07%252F06%252Fstartup-and-cleanup-functions-in-erlang-eunit-tests%252F%26amp%3Btitle%3DStartup%2520and%2520Cleanup%2520functions%2520in%2520Erlang%2520EUnit%2520tests%26amp%3Bnotes%3DI%2527ve%2520got%2520the%2520basic%2520grasp%2520on%2520Erlang%2520but%2520before%2520I%2520can%2520do%2520anything%2520meaningful%2520I%2520need%2520to%2520learn%2520how%2520to%2520test%2520my%2520code%25C2%25A0%2520EUnit%2520is%2520the%2520de-facto%2520test%2520framework.%25C2%25A0%2520There%2520are%2520a%2520ton%2520of%2520great%2520sites%2520that%2520talk%2520about%2520testing%2520with%2520EUnit%2520-%2520I%2520have%2520linked%2520to%2520several%2520at%2520t';" 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" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.roberthorvick.com%252F2009%252F07%252F06%252Fstartup-and-cleanup-functions-in-erlang-eunit-tests%252F%26amp%3Bt%3DStartup%2520and%2520Cleanup%2520functions%2520in%2520Erlang%2520EUnit%2520tests';" 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" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.roberthorvick.com%252F2009%252F07%252F06%252Fstartup-and-cleanup-functions-in-erlang-eunit-tests%252F%26amp%3Btitle%3DStartup%2520and%2520Cleanup%2520functions%2520in%2520Erlang%2520EUnit%2520tests%26amp%3Bannotation%3DI%2527ve%2520got%2520the%2520basic%2520grasp%2520on%2520Erlang%2520but%2520before%2520I%2520can%2520do%2520anything%2520meaningful%2520I%2520need%2520to%2520learn%2520how%2520to%2520test%2520my%2520code%25C2%25A0%2520EUnit%2520is%2520the%2520de-facto%2520test%2520framework.%25C2%25A0%2520There%2520are%2520a%2520ton%2520of%2520great%2520sites%2520that%2520talk%2520about%2520testing%2520with%2520EUnit%2520-%2520I%2520have%2520linked%2520to%2520several%2520at%2520t';" 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" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.roberthorvick.com%252F2009%252F07%252F06%252Fstartup-and-cleanup-functions-in-erlang-eunit-tests%252F%26amp%3Btitle%3DStartup%2520and%2520Cleanup%2520functions%2520in%2520Erlang%2520EUnit%2520tests';" 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>
