<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Making the Complex Simple &#187; Design</title>
	<atom:link href="http://simpleprogrammer.com/category/design/feed/" rel="self" type="application/rss+xml" />
	<link>http://simpleprogrammer.com</link>
	<description>Software Development from John Sonmez&#039;s Perspective</description>
	<lastBuildDate>Tue, 07 Feb 2012 17:47:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='simpleprogrammer.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Making the Complex Simple &#187; Design</title>
		<link>http://simpleprogrammer.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://simpleprogrammer.com/osd.xml" title="Making the Complex Simple" />
	<atom:link rel='hub' href='http://simpleprogrammer.com/?pushpress=hub'/>
		<item>
		<title>Getting up to BAT: Building a True DSL</title>
		<link>http://simpleprogrammer.com/2011/05/14/getting-up-to-bat-building-a-true-dsl/</link>
		<comments>http://simpleprogrammer.com/2011/05/14/getting-up-to-bat-building-a-true-dsl/#comments</comments>
		<pubDate>Sun, 15 May 2011 03:24:52 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[BAT]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Language]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/2011/05/14/getting-up-to-bat-building-a-true-dsl/</guid>
		<description><![CDATA[If you’ve made it this far with your BAT implementation, you have finally arrived. Not to say that you’ll ever be done expanding your automation framework and building and improving your BATs, but you are at the point of having a mature usable BAT framework and you can be proud of that accomplishment. If you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1329&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you’ve made it this far with your BAT implementation, you have finally arrived.</p>
<p>Not to say that you’ll ever be done expanding your automation framework and building and improving your BATs, but you are at the point of having a mature usable BAT framework and you can be proud of that accomplishment.</p>
<p>If you truly want to see where the rabbit hole goes though… Read on</p>
<h2>Taking it to 11</h2>
<p><a href="http://complextosimple.files.wordpress.com/2011/05/spinal_tap_but_it_goes_to_eleven.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="spinal_tap_but_it_goes_to_eleven" border="0" alt="spinal_tap_but_it_goes_to_eleven" src="http://complextosimple.files.wordpress.com/2011/05/spinal_tap_but_it_goes_to_eleven_thumb.jpg?w=485&#038;h=273" width="485" height="273" /></a></p>
<p>If you have designed a good automation framework for writing your BATs, you will most likely have created an internal domain specific language (DSL).</p>
<p>What is an internal DSL?</p>
<p>Basically it is a set of APIs and rules built in another language that function as their own language.</p>
<p>You might have some code that looks like this:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:08f381ea-bda8-4f72-ab40-6ac923941fc7" class="wlWriterEditableSmartContent">
<pre style="white-space:normal;">
<pre class="brush: csharp; pad-line-numbers: true;">
Pages.Login.Goto();
Pages.Login.LoginWithDefaultUser();
Pages.Customers.Goto();
Pages.Customers.AddNewCustomer();
</pre>
</pre>
</div>
<p>&#160;</p>
<p>You can see how this code is really looking like a language itself.&#160; It is hard to tell if this code is even C# or Java.</p>
<p>This is a good example of an internal DSL.</p>
<p>If we were to take this same section of a BAT test and write it as an external DSL, it might look something like this:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:521b3b0f-ee9e-4256-8083-4b36a0085593" class="wlWriterEditableSmartContent">
<pre style="white-space:normal;">
<pre class="brush: plain;">
LOGIN WITH DEFAULT-USER
ADD-CUSTOMER WITH DEFAULT-CUSTOMER

</pre>
</pre>
</div>
<p>&#160;</p>
<p>With a true DSL we can simplify even further to get right down to the bare minimum set of commands we need to execute what we want in our application.</p>
<h2>How to implement a DSL?</h2>
<p>There are a number of tools and techniques that can be used to create a DSL.&#160; I’ll hit on some of those a bit later on, but first we need to design the DSL itself.</p>
<p>Designing a DSL can be challenging, but I have found the best approach is to use a similar approach to what we used to <a href="http://simpleprogrammer.com/2011/03/16/getting-up-to-bat-designing-an-automation-framework/">design our automation framework</a>.</p>
<p>It is best to work backwards from the syntax that would make your BAT the easiest to write and the most clear.&#160; Once you have done that you can worry later about implementing the DSL to support the syntax you have come up with.</p>
<p>You should focus on creating a limited vocabulary for your DSL so that it will be easy for someone to learn and know what their options are.</p>
<p>The more restrictions we put into a DSL the easier that DSL becomes for doing a specific purpose.</p>
<p><strong>We want to give the user of a DSL no more options than required for what they want to do and no less.</strong></p>
<p>This isn’t always easy to do.&#160; It often requires some careful thought and refinement.</p>
<p>Your DSL should also use terminology and phrases that are familiar to the business domain of your application.&#160; By doing this you will make it so someone understanding your application will understand BATs written in that DSL.</p>
<p>I also always try to make a BAT DSL read as much as plain English as possible.</p>
<p>Once you have designed the DSL, you will need a way to implement it.&#160; </p>
<p>I’ve written about <a href="http://simpleprogrammer.com/2010/03/10/internal-dsl-becomes-external-dsl/">using ANTLR to create an external DSL</a>.&#160; Also I have written a review on a <a href="http://simpleprogrammer.com/2010/04/16/book-review-the-definitive-antlr-reference-building-domain-specific-languages/">book that I recommend for learning ANTLR</a>.</p>
<p>Microsoft also has a <a href="http://archive.msdn.microsoft.com/vsvmsdk">DSL SDK you can use in VS2010</a>, although I haven’t used it myself.</p>
<p>The basic idea behind implementing a DSL is that you have to take some sort of grammar you design for your DSL, parse it into a syntax tree, then use that tree to generate code that will actually perform the actions.</p>
<p>It is by no means a trivial task, but using a tool like ANTLR, makes it much simpler than trying to completely generate a parser and execution engine yourself.</p>
<h2>Why even bother?</h2>
<p>Creating a DSL for your BATs is completely optional, but there are some significant benefits to doing so.</p>
<p>The biggest one I have found is that you are much more likely to get business people to write BATs if you have a simple DSL they can use to create them.&#160; Once you get everyone using BATs to define behavior and requirements, communication becomes much more simple.</p>
<p>Another great thing about creating a DSL is that you can create a runner that can interpret the DSL BAT scripts which can be run on any machine.&#160; Typically, to run BATs created in Java or C#, you would need a C# or Java compiler and a test runner, etc.&#160; By creating a DSL and a stand alone executor, you can very easily allow anyone to run BATs with little to no setup.</p>
<p>Writing your BATs in a DSL will also make writing the BATs and understanding them much easier.&#160; A good DSL can get rid of all the little language and API nuances that don’t really add information to the specific domain you are working in.&#160; A good DSL strips information down to the minimum amount needed and is able to do so, because it assumes the context is your application’s business domain.</p>
<p>You will also find that you are better able to separate framework from tests when you have a DSL in place.&#160; One issue that many teams face when creating a BAT framework is that some people come along and write lower level code in the higher level BATs.&#160; This creates a problem because it is hard to force someone to use the higher level API when the framework and the BAT code are the same language.</p>
<h2>Wrapping it up</h2>
<p>So that completes my series on Getting up to BAT.</p>
<p>Hopefully you have learned enough to be able to implement your own BAT framework and get BATs running for your application.</p>
<p>I tried to cover most of the important steps here without getting too deep into the details.</p>
<p>If you have any questions or are interested in getting more in depth training or consulting on getting BATs setup for your environment, send me an email or leave a comment.</p>
<p>It can be a little difficult to get started and get a BAT framework up and running, but the rewards are well worth the effort!</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.&#160; Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.&#160; Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1329/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1329&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2011/05/14/getting-up-to-bat-building-a-true-dsl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/05/spinal_tap_but_it_goes_to_eleven_thumb.jpg" medium="image">
			<media:title type="html">spinal_tap_but_it_goes_to_eleven</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting up to BAT: Scaling Out</title>
		<link>http://simpleprogrammer.com/2011/05/05/getting-up-to-bat-scaling-out/</link>
		<comments>http://simpleprogrammer.com/2011/05/05/getting-up-to-bat-scaling-out/#comments</comments>
		<pubDate>Fri, 06 May 2011 04:03:25 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[BAT]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/2011/05/05/getting-up-to-bat-scaling-out/</guid>
		<description><![CDATA[If you haven’t been following, it has been a while since my last post on this topic.&#160; I had a bit of distraction the last few weeks, but I am back and ready to go! When we last left off we had just gotten our BATs as part of the acceptance criteria for any new [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1326&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you haven’t been following, it has been a while since <a href="http://simpleprogrammer.com/2011/04/12/getting-up-to-bat-adding-bats-to-your-acceptance-criteria/">my last post on this topic</a>.&#160; I had a <a href="http://simpleprogrammer.com/2011/04/25/the-most-complex-program-of-all-time/">bit of distraction</a> the last few weeks, but I am back and ready to go!</p>
<p>When we last left off we had just gotten our BATs as part of the acceptance criteria for any new backlogs that are worked on.&#160; This puts us at a point where we could really say that we have successfully implemented BAT testing.</p>
<p>You don’t want to get too comfortable just yet though, because the next hurdle you will most likely face will be the problem of not having enough time to execute all your tests.</p>
<h2>You want to think about this ahead of time</h2>
<p>Nothing worse than getting everything going and then not being able to execute the entire test suite, because you didn’t plan ahead.</p>
<p>You don’t want to get to the point where confidence in your BAT suite it lost because you are not able to get all the tests executed in a reasonable amount of time.</p>
<p><strong>The more frequently your tests are run the more value they have.</strong></p>
<p>By reducing cycle time from the time a breaking change is made in the system and the time it is discovered, you greatly reduce the risk it imposes on your software and you decrease the scope of the code changes in which it could have occurred.</p>
<p>To put it simply, the faster you can find out you broke something, the more likely you can fix it before it does damage, and the more likely you will be to know what thing you did caused the breakage.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/05/clarkson_face.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="clarkson_face" border="0" alt="clarkson_face" src="http://complextosimple.files.wordpress.com/2011/05/clarkson_face_thumb.jpg?w=485&#038;h=361" width="485" height="361" /></a></p>
<h2>How can we reduce cycle time?</h2>
<p>There are a few different strategies we can employ and we can mix and match some of these strategies.</p>
<p><strong>Straight forward parallelization</strong></p>
<p>The best and most effective thing you can do is to take a given test run, split up the execution of those tests on multiple machines and execute them in parallel.</p>
<p>This approach is going to give you the best bang for your buck.&#160; You should really try to get some amount of parallelization going before attempting any other solution, since it is going to make a huge impact on the total execution time for your tests without making any sacrifices.</p>
<p>There are even many ways you can mix and match to do parallelization:</p>
<ul>
<li>Use multiple physical machines</li>
<li>Run many virtual machines on a single host</li>
<li>Put an executor program on every team member’s machines that will execute tests when that machine is idle or put into test execution mode (perhaps at night)</li>
<li>Use a cloud based computing platform to execute your tests</li>
<li>Run multiple browser instances on a single machine</li>
</ul>
<p><strong>Data preloading</strong></p>
<p>With this approach, you would preload some of the test data that your tests might generate by manually clicking through screens.</p>
<p>This is fairly hard to explain, so let me give you an example:</p>
<p>Suppose you had a set of tests that all involved creating customers, but each customer you create in the system takes about 3 minutes to create by clicking through the screens to get them into the system.</p>
<p>We don’t need to have 500 tests all executing the same exact logic in the system 500 times for 3 minutes just to generate all the customer data that will be used in the tests.</p>
<p>Instead, we can leave a few tests that are exercising the customer creation functionality, and we can execute a SQL script to push all the other customer data into the test database for the other tests to use.</p>
<p>Using this technique we might be able to reduce our total execution time by 3 minutes * each test, or about 25 hours for 500 tests.</p>
<p>This can be a huge savings in time, and it doesn’t come at that high of a cost.&#160; The sanctity of our tests is slightly compromised, but we are taking a calculated risk here knowing that we already have covered the area of execution which we are preloading data for.</p>
<p>Consider this technique when you notice certain things in the system taking a very long time to do.</p>
<p><strong>Test runs by area</strong></p>
<p>With this technique, we can reduce the total execution time in a given period by splitting up test areas and running them either at different times or in response to changes in certain areas.</p>
<p>You have to be very careful with this approach, because if you don’t do it correctly, you can start to erode some of the safety your BAT tests are providing you.</p>
<p>I would only do something like this as a last resort, because it is so easy to virtualize today, and hardware is so cheap.</p>
<p>I’d much rather run too many tests than too few.</p>
<p><strong>Test randomization</strong></p>
<p>With test randomization, we are going to take our total desired execution time, and divide it by the average time for running a test.&#160; We then can use that number of tests to run to randomize the execution of our tests each time we run them and only run the number or tests that will fit in the desired execution time.</p>
<p>This choice is also a compromise that I typically don’t like to take.</p>
<p>It can be very useful though, combined with other techniques when you still don’t have enough time to execute your entire test suite.</p>
<p>The basic idea here is that you are going to randomly run tests each time to fill up the time you have to run tests.</p>
<p><strong>Test reduction</strong></p>
<p>This one seems fairly obvious, but can be very helpful.</p>
<p>Often I will see teams starting out with automation, trying to write way too many BAT tests for a given feature.&#160; Sure, with automated tests it is possible for run a test for every single possible combination of values in your 5 drop downs, but will it really benefit you?</p>
<p>In many cases you have to think about what you are trying to protect against with your BATs.&#160; Sometimes running every combination of data selection choices is going to be important, but other times you are only going to need to write tests to test a few of the happy path scenarios.</p>
<p>It is important to find a balance between test coverage and test volume and not just for execution time.&#160; There is a logistical overhead to having a large volume of mostly redundant tests.</p>
<p>So even though this technique might seem dangerous and counter-productive, I will almost always employ it to some degree.</p>
<h2>Common pitfalls</h2>
<p>Here are some of the things you might want to watch out for as you are scaling out and streamlining your execution of BATs:</p>
<ul>
<li><strong>Parallelization issues.&#160; </strong>If you are using shared state, you can run into big trouble when your tests are executing in parallel.&#160; There are many manifestations of this.&#160; You could have issues at the database level or at the local machine memory level.&#160; The best way to avoid this kind of problem is to use separate virtual machines for each test execution, and not reuse data setup between test cases.</li>
<li><strong>Ineffective error reporting.&#160; </strong>If you run a huge volume of tests in parallel, you better have a good way to sort through the results.&#160; It is much harder to figure out why things failed when they are run across multiple machines.</li>
<li><strong>Test order dependencies.&#160; </strong>Make sure tests don’t rely on being run in a certain order or you will have lots of pain when you disrupt that order.</li>
<li><strong>Environment setup.&#160; </strong>Make sure all your test execution environments are exactly the same unless you are specifically testing different environments for execution.&#160; You don’t want tests failing on one machine but passing on another.</li>
</ul>
<p><strong>As always, you can subscribe to this </strong><a href="http://feeds.feedburner.com/MakingTheComplexSimple"><strong>RSS feed</strong></a><strong> to follow my posts on Making the Complex Simple.&#160; Feel free to check out </strong><a href="http://elegantcode.com/"><strong>ElegantCode.com</strong></a><strong> where I post about the topic of writing elegant code about once a week.&#160; Also, you can follow me on twitter </strong><a href="http://twitter.com/jsonmez"><strong>here</strong></a><strong>.</strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1326/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1326/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1326/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1326&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2011/05/05/getting-up-to-bat-scaling-out/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/05/clarkson_face_thumb.jpg" medium="image">
			<media:title type="html">clarkson_face</media:title>
		</media:content>
	</item>
		<item>
		<title>The Most Complex Program of All Time</title>
		<link>http://simpleprogrammer.com/2011/04/25/the-most-complex-program-of-all-time/</link>
		<comments>http://simpleprogrammer.com/2011/04/25/the-most-complex-program-of-all-time/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 23:40:09 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Cool Stuff]]></category>
		<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=1322</guid>
		<description><![CDATA[What if I told you that there is a program so complicated that is the source responsible for all other programs ever created? Give me your most complex program and I will trump it by presenting you with the program that was responsible for creating the human who created that program. If you haven’t figured [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1322&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What if I told you that there is a program so complicated that is the source responsible for all other programs ever created?</p>
<p>Give me your most complex program and I will trump it by presenting you with the program that was responsible for creating the human who created that program.</p>
<p>If you haven’t figured it out by now that program is human DNA and that masterful programmer is the Creator Himself.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/04/dna_500.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="dna_500" border="0" alt="dna_500" src="http://complextosimple.files.wordpress.com/2011/04/dna_500_thumb.jpg?w=474&#038;h=308" width="474" height="308" /></a></p>
<h2>Doesn’t matter what religion you are</h2>
<p>It is pretty difficult to fail to recognize that a program as complex as a human being’s genetic code was created, not randomly assimilated.</p>
<p>As programmers we have a unique perspective in life.&#160; We are able to truly understand God’s masterwork in the creation of human life.&#160; We are able to grasp loosely the ungraspable magnificence of a program so complicated, yet executed so simply.</p>
<p>We also have the unique position of fundamentally understanding that complex programs could never be created by random chance, regardless of time.&#160; We can look at a piece of code or functioning computer program and we can understand that its creation required an intelligent creator.&#160; Other people and professionals do not have this insight that so many of us are fortunate to have.</p>
<h2></h2>
<h2>Qualities of the code</h2>
<p>We’ve been treating data like code recently in C# with the introduction of Lambda expressions and other languages have been doing it for much longer, but the ultimate example of this practice is right there in our own DNA.</p>
<p>DNA is data, a message, and code all together at once.&#160; Just like a data structure in the memory of a computer executing a program, it lacks meaning without the context of the cell that it is in.&#160; Amazingly though, this data is recombined in different ways that are dictated by two separate and different instances of itself to produce a third separate instance of itself that still makes sense.</p>
<p>Just to give you an idea of how insanely complex that idea is (despite the simple execution of it), it would be like if you wrote a webserver that could spawn new instances of itself by contacting another, but different copy of your webserver and getting 1/2 the source code payload from itself and the contacted server, then actually combining that code to create a new copy that is different, yet still passes all of your unit and integration tests.</p>
<p>One of the qualities we often look at for our code is the size of the code in relation to what it can accomplish.</p>
<p><a href="http://www.utheguru.com/fun-science-how-many-megabytes-in-the-human-body">A decent estimate</a> is that the human genome is about .35 gigabytes of data.&#160; That data is stored on such a small space that we can’t even see it with the naked eye.&#160; There is no more compact form of data in the universe that we know of.</p>
<p>Not only is this data compact, but consider how much functionality is built into this small .35 gigabyte package?&#160; If you had to write acceptance tests for the human body, you might spend several lifetimes doing it.&#160; If you had to write acceptance tests for just a single cell in your body, that feat itself could take years and still not be complete.&#160; Imagine trying to write a program that took up .35 gigabytes of data, yet contained millions of acceptance tests worth of functionality.</p>
<p>We certainly could go on and on about the qualities of the human code, but I want to point out what perhaps is one of the most unique and amazing… the ability for the code to create its own hardware.</p>
<p>In actuality the code and that hardware are much the same.&#160; DNA replicates by a chemical reaction that causes the entire molecule to unwind and split.&#160; RNA inside a cell is created from specific portions of the DNA in order to create different protein chains which are used to construct little machines that will replicate the DNA and cells and in turn create machines capable of doing the same.</p>
<p>The process is very complex and not wholly understood, but in effect the end result is that DNA contains the instructions inside of a cell to create the machinery to host another copy of itself and execute that code.</p>
<h2>Easter eggs</h2>
<p>Let’s not forget the little Easter eggs that have been thrown in by the designer of our codebase.</p>
<p>How about a hiccup or a tear?</p>
<p>Sadness or laughter?</p>
<p>There are some “evolutionary theories” as to why these things exist, but in reality they serve no specific purpose for either our survival or well being.</p>
<p>In my mind these are clearly Easter eggs left by the designer to show His handiwork.</p>
<h2>Why am I writing this post now?</h2>
<p>I didn’t want to go completely off subject and boast about my wonderful daughter that was born on April 17th 2011 at 2:33 PM, since this is a professional blog.&#160; </p>
<p><a href="http://complextosimple.files.wordpress.com/2011/04/0417sophiasonmez-favs-16.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="0417sophiasonmez-favs-16" border="0" alt="0417sophiasonmez-favs-16" src="http://complextosimple.files.wordpress.com/2011/04/0417sophiasonmez-favs-16_thumb.jpg?w=489&#038;h=732" width="489" height="732" /></a></p>
<p>But, there you have it.</p>
<p>The amazing birth of my first child reminded my of the incredibly complex code that keeps us all breathing, procreating and living life that we seem to take for granted each and every day.</p>
<p>Welcome to the world Sophia Grace!</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.&#160; Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.&#160; Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1322/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1322&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2011/04/25/the-most-complex-program-of-all-time/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/04/dna_500_thumb.jpg" medium="image">
			<media:title type="html">dna_500</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/04/0417sophiasonmez-favs-16_thumb.jpg" medium="image">
			<media:title type="html">0417sophiasonmez-favs-16</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting Up to BAT: Designing an Automation Framework</title>
		<link>http://simpleprogrammer.com/2011/03/16/getting-up-to-bat-designing-an-automation-framework/</link>
		<comments>http://simpleprogrammer.com/2011/03/16/getting-up-to-bat-designing-an-automation-framework/#comments</comments>
		<pubDate>Thu, 17 Mar 2011 00:01:36 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[BAT]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Functional]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/2011/03/16/getting-up-to-bat-designing-an-automation-framework/</guid>
		<description><![CDATA[Now that you’ve gotten an automation lead and decided on the browser automation tool you are going to use, the next step is to design an actual automation framework. This is one of the most critical components of the overall success of your automation strategy, so you will want to make sure you invest properly [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1275&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Now that you’ve gotten an automation lead and decided on the browser automation tool you are going to use, the next step is to design an actual automation framework.</p>
<p>This is one of the most critical components of the overall success of your automation strategy, so you will want to make sure you invest properly in this area.</p>
<p>I have seen a large number of automation projects go forth, but each time the critical component determining their success or failure was having a good automation framework.</p>
<p>Solid design and excellent technical resources are absolutely critical to success in this area!</p>
<h2>What is an automation framework?</h2>
<p>An automation framework is essentially an API that all of your BATs (Blackbox Automated Tests) are written against.</p>
<p>This API can be as simple as the API that is exposed by your browser automation tool (WatiN, Selenium, etc.), but I would highly recommend building your own layer on top of the browser automation tool’s API that will act as a DSL (Domain Specific Language) for automating your application.</p>
<p>Let me break this down a bit further by using an example.</p>
<p>Think about the task of making coffee.</p>
<p>There are several “API” levels we can interact with.</p>
<ol>
<li>We can go with a very low level API where we take whole coffee beans and grind them down.&#160; Then we take some water, get it hot.&#160; We take our filter put the ground beans in it, put it over a cup and pour the water into the filter.</li>
<li>We can go with a higher level API where we use a traditional coffee maker.&#160; In this case we load the coffee maker with a filter, ground coffee beans and water and push a “brew” button.&#160; We could also set it to start at a certain time.</li>
<li>We can go with a very high level API where we use a <a href="http://www.amazon.com/gp/product/B000AQSMPO/ref=as_li_ss_tl?ie=UTF8&amp;tag=makithecompsi-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B000AQSMPO">Keurig</a> machine or similar device.&#160; In this case we only make sure the machine has water in it, and we just insert a little pod and press brew.&#160; We can make different kinds of coffee, cider or hot cocoa just by changing what pod we use.</li>
</ol>
<p><a href="http://complextosimple.files.wordpress.com/2011/03/unnamed.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="unnamed" border="0" alt="unnamed" src="http://complextosimple.files.wordpress.com/2011/03/unnamed_thumb.jpg?w=220&#038;h=220" width="220" height="220" /></a></p>
<p>Using the API provided by the browser driver is like making the coffee by hand.&#160; It’s going to take a large amount of effort each time you do it.</p>
<p>We want our automation framework to be more like the Keurig machine.&#160; We want to be able to compartmentalize our tests in little pods that are small and don’t require many hooks into the automation framework.</p>
<p>To rehash this one more time, basically our automation framework will be a framework we build on top of the browser driver framework, which is designed to make it easy to write tests which automate our application.</p>
<h2>What makes a good automation framework design?</h2>
<p><strong>The true measure of an automation framework is the size of the tests that are written against them. The less lines of code in each test, the better the automation framework captures the domain of the application being automated.</strong></p>
<p>In my <a href="http://simpleprogrammer.com/2010/01/05/automated-ui-testing-framework-a-real-example/">earlier post about an example of an automation framework</a>, I talked a bit about the strategy I used in a real implementation of an automation framework.</p>
<p>Here is a diagram showing the different layers.&#160; You can see the framework layer is in green here.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/03/automated_testing_framework-2.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="automated_testing_framework-2" border="0" alt="automated_testing_framework-2" src="http://complextosimple.files.wordpress.com/2011/03/automated_testing_framework-2_thumb.png?w=482&#038;h=577" width="482" height="577" /></a></p>
<p>You can also see in this diagram that on the right hand side, I have screens, workflows and navigation.</p>
<p>This is one of the common design patterns you can use to build your automation framework, since it closely models that of most web applications.</p>
<p>A good way to design your framework is to use a pattern like this and create classes for each page in your application, create classes which might represent workflows in your application, and create some classes for navigating around the application.</p>
<p>The basic idea you are shooting for with your automation framework is to make it so the tests do not have to know anything about the browser.&#160; Only your automation framework itself should be dealing with the DOM and HTML and CSS.&#160; Your tests should be dealing with concepts which any user would be familiar with.&#160; That is why I commonly use pages and workflows.</p>
<p>Let me give you an example.</p>
<p>Let’s say we ignored the advice of creating an automation framework and decided to program our tests directly against the browser driver layer.&#160; In this case, let’s say that we want to automate a login page.&#160; Our test might look something like this pseudo-code.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:af0bdb44-e0ca-4c0b-94d6-200830b1ed71" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp; pad-line-numbers: true;">
browser.goto(&quot;http://mywebsite/login.aspx&quot;);
Element userName = browser.getElementById(&quot;login_userName&quot;);
userNameTextField.Type(&quot;Joe&quot;);

Element userPassword = browser.getElementById(&quot;login_userPassword&quot;);
userPassword.TextField.Type(&quot;mypassword&quot;);

Element loginButton = browser.
    getElementByXPath(&quot;//button[class=&quot;loginButton&quot;]&quot;);
loginButton.Click();
</pre>
</pre>
</div>
<p>&#160;</p>
<p>This is not very readable.&#160; It is not something an average tester will be able to pick up and start writing, and it is extremely fragile.&#160; If you have 500 tests like this and the id changes for one of the elements on the page, many tests will break.</p>
<p>Now, contrast it to this pseudo-code:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:a727af81-9910-4280-bab2-a5da7a354e0d" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp;">
Pages.Login.Goto();
Pages.Login.LoginWithDefaultUser();
</pre>
</pre>
</div>
<p>&#160;</p>
<p>Where did all the code go?&#160; You don’t need it!&#160; Actually it went into the framework, where it can be reused.&#160; Now your test code is simple, can be understood by just about anyone, and won’t break with changes to the UI.&#160; (You will just have to change the framework method instead of 500 tests.)</p>
<p>If you want some more detailed examples take a look at my Boise Code Camp slides on the subject <a href="http://www.slideshare.net/jsonmez/internal-ds-ls-for-automated-functional-testing">here</a>.</p>
<p>Let me offer up some general guidelines for creating a good design for an automation framework.</p>
<ol>
<li>NEVER require the tests to declare variables.</li>
<li>NEVER require the tests to use the <em>new</em> keyword or create new objects.</li>
<li>NEVER require the tests to manage state on their own.</li>
<li>ALWAYS reduce the number of parameters for API calls when possible.</li>
<li>ALWAYS use default values instead of requiring a parameter when possible.</li>
<li>PREFER to make the API easier to use over making the internals of the API less complex.</li>
<li>PREFER using enumerations and constants to requiring the test to pass in primitive types.&#160; (Make the input bounded when possible.)</li>
<li>NEVER expose the browser or DOM to the tests or let them manipulate it directly.</li>
</ol>
<p>You can see that these guidelines will almost force your API to be entirely static methods!&#160; Don’t freak out.&#160; It is perfectly fine.&#160; There is nothing wrong with using static methods in this case.</p>
<p>Stop… take a breath, think about why you think static methods are bad, because <a href="http://simpleprogrammer.com/2010/01/29/static-methods-will-shock-you/">I probably agree with you.</a></p>
<p>But, in this case we are HIGHLY valuing making the tests simple and easy to write over anything else, and because of this emphasis, static methods are going to be the way to go.</p>
<h2>Some advice on getting started</h2>
<p>Start with the tests in plain human English.</p>
<p>Take those tests and make them into code preserving as much English as possible.&#160; Then work down from there implementing the methods needed to make those tests work.&#160; Only add things as you need them.&#160; Do not overdesign!</p>
<p>In my example above, we might have started with something like this:</p>
<p>1. Goto the login page.</p>
<p>2. Login with the default user.</p>
<p>The actual code looks just like that.&#160; It should always be a 1 to 1 mapping.</p>
<p>This is where having someone that has some experience doing this is going to come in handy.</p>
<p>Just keep in mind the whole time your 2 big goals:</p>
<ol>
<li>Easy to write tests</li>
<li>Tests are concise</li>
</ol>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.&#160; Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.&#160; Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1275/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1275&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2011/03/16/getting-up-to-bat-designing-an-automation-framework/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/03/unnamed_thumb.jpg" medium="image">
			<media:title type="html">unnamed</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/03/automated_testing_framework-2_thumb.png" medium="image">
			<media:title type="html">automated_testing_framework-2</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting Up to BAT: Hiring an Automation Lead</title>
		<link>http://simpleprogrammer.com/2011/02/23/getting-up-to-bat-hiring-an-automation-lead/</link>
		<comments>http://simpleprogrammer.com/2011/02/23/getting-up-to-bat-hiring-an-automation-lead/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 05:03:39 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[BAT]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Interview]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/2011/02/23/getting-up-to-bat-hiring-an-automation-lead/</guid>
		<description><![CDATA[This is my first in a series of posts about getting to fully automated blackbox testing, or BAT, as I like to call it. In my last post in the Back to Basics series, I mentioned that I would begin this series with the intent of showing you step by step how to go from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1264&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is my first in a series of posts about getting to fully automated blackbox testing, or BAT, as I like to call it.</p>
<p>In my <a href="http://simpleprogrammer.com/2011/02/05/back-to-basics-becoming-bat-man/">last post in the Back to Basics series</a>, I mentioned that I would begin this series with the intent of showing you step by step how to go from no automation to a quality BAT infrastructure.</p>
<p>By the end of this series you should have the information you need to actually get automated testing going in your organization instead of just talking about it.</p>
<p>First stop on this train is hiring an automation lead.</p>
<h2>Don’t doom yourself before you start</h2>
<p>If you think you are going to create BATs, but you are not going to have at least 1 dedicated resource to doing the job of getting this all going, you are probably bound for failure.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/02/doom4.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="DOOM4" border="0" alt="DOOM4" src="http://complextosimple.files.wordpress.com/2011/02/doom4_thumb.jpg?w=474&#038;h=452" width="474" height="452" /></a></p>
<p><strong>Listen up, because this is the single biggest mistake that most organizations make when trying to get BATs up.</strong></p>
<p>This mistake is thinking that you can get your QA team to get this going and you can part time some developers onto the project, but not having a dedicated resource.</p>
<p>Why is this absolutely absurd and you should expect it to fail?&#160; Because, in order to get an infrastructure to support your BATs, you are going to need to basically design and create a testing framework that is tailored to the business domain of your application.</p>
<p>Blah, what?&#160; Ok, in simple terms.&#160; Someone is going to need to design and build an automation framework.</p>
<p>If you think you are just going to buy some tool and use it to record test scripts, you are wrong.&#160; That approach simply is not going to work.&#160; I won’t get into why here, but I have <a href="http://simpleprogrammer.com/2009/12/18/automated-functional-testing-record-or-program/">written on it before in the past</a>, and if you are interested, let me know and I’ll go much more in depth.</p>
<p>Think about this, do you want your QA team, as good as they may be at QA work, designing and building an automation framework?&#160; Give them a few C# or Java classes and have their first real coding work be to design a complex system?</p>
<p>How about a developer who has “real” work and can only devote part time to designing what may end up becoming one of the most important parts of your infrastructure?</p>
<p>It is fine to pull a developer off of the main product and put them in this role until it is up and running, but make no mistake, you need a dedicated resource.</p>
<p>You are going to want to a well designed maintainable automation framework, because the ease of use of that framework will determine how easy or not it is to write automated tests that use that framework.</p>
<h2></h2>
<h2>I recommend getting someone who has done it before</h2>
<p>Why?&#160; Because creating an automation framework is much different than developing your software and it is much different than writing tests.&#160; It really requires a special skill set that is not so easily acquired without the experience of having done this before.</p>
<p>I also think it is fine to use a developer resource, but if you do, you want to try to get a more senior developer to take this role.</p>
<p>Here is a list of some of the skills that I think are important which may also make it much more clear why I am recommending a specialist in this case:</p>
<p><strong>Development of APIs</strong> (In the end the automation framework will be an API that is used to write automated tests.&#160; The better the API is designed, the easier it will be to write tests against it.)</p>
<p><strong>Design of reusable base components</strong> (Much of the framework consists of repeated concepts with different variations for different screens in your application.&#160; As a result, the core of the framework will be a basic design that allows for variation without too much repetition… reuse)</p>
<p><strong>Ability to quickly acquire domain knowledge of the software</strong> (Someone writing an automation framework needs to really understand how the system under test works, so that they can model the framework after it.)</p>
<p><strong>Pragmatism</strong> (Many “accepted” good software designs are actually bad automation framework designs.&#160; Static methods can make an automation framework’s API much more simple to use, especially for non-programmers, but would not be a good design choice in your main product.&#160; A person who designs an automation framework should be pragmatic enough to “break the rules” in order to make the framework easiest to use and maintain.)</p>
<p><strong>Experience or understanding of QA</strong> (It is important to understand the users of the framework, the QA team.&#160; Understanding what kind of tests they will likely write or how they will think will go a long way in coming up with a good framework design.)</p>
<p><strong>Communication skills</strong> (Good BATs read like English.&#160; It is critical to make sure that the person designing the framework facilitates this to make the BATs as useful and understandable as possible.)</p>
<p><strong>HTML / XPath </strong>(This specific skill set is very important for web based products.&#160; Creating an automation framework built on top of one of the web browser drivers will require intimate knowledge of HTML and probably XPath to be able to access attributes and elements on pages in your application.)</p>
<p>I’ve written a few automation frameworks and contributed to the design of several others, and I can tell you that my version 6.0 of an automation framework was much much better than version 1.0.</p>
<p>There are a huge number of patterns that emerge that are unique to this problem domain.&#160; So, I do believe having experience in this area specially is a huge benefit.</p>
<h2></h2>
<h2></h2>
<h2>What does this automation person do?</h2>
<p>We’ll get to more specific in my later posts in this series, but the basic responsibility should be:</p>
<ul>
<li>Figure out the technology and tools to be used</li>
<li>Create the initial automation framework</li>
<li>Create smoke tests for the application under test</li>
<li>Get those smoke tests into some kind of automated build</li>
<li>Train developers and QA in the use of the framework</li>
<li>Drive the adoption of the framework and automation for all features</li>
<li>Set up rules for consistency in writing automated tests</li>
<li>Design of hierarchy and structure of automated tests</li>
</ul>
<p>You want your automation person to be responsible for pushing and driving forward the goal of getting to the point of being able to automate each feature being created with minimal effort.</p>
<p>This is one of those jobs that can actually be “done.”&#160; What I mean by that is you may have the automation lead end up creating the framework, developing it enough to push it out to your development teams and train them to write automated tests along with QA, and you really don’t need an automation lead anymore.</p>
<p>This is great though, because now you have a developer that really knows your product and is a good bridge to QA, or you can keep them improving the framework and overseeing it if you want to go that route.</p>
<p>Bottom line is:</p>
<ol>
<li>Make sure you get a dedicated resource</li>
<li>Make sure that resource can write code and design a framework</li>
<li>Make sure they train everyone else, one person cannot automate your entire application</li>
</ol>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.&#160; Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.&#160; Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1264/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1264/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1264/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1264&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2011/02/23/getting-up-to-bat-hiring-an-automation-lead/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/02/doom4_thumb.jpg" medium="image">
			<media:title type="html">DOOM4</media:title>
		</media:content>
	</item>
		<item>
		<title>Professionals Have Professional Looking Resumes</title>
		<link>http://simpleprogrammer.com/2011/02/15/professionals-have-professional-looking-resumes/</link>
		<comments>http://simpleprogrammer.com/2011/02/15/professionals-have-professional-looking-resumes/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 03:20:18 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Career]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Self Improvement]]></category>

		<guid isPermaLink="false">http://simpleprogrammer.com/?p=1254</guid>
		<description><![CDATA[I&#8217;ve avoided talking about this subject a bit, because I didn&#8217;t want anyone to think I was looking for a job.&#160; I&#8217;m not. But, I think it is worth taking a moment to talk about one of the biggest blunders professional programmers make in their job search, careers. The mistake is not having a resume [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1254&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve avoided talking about this subject a bit, because I didn&#8217;t want anyone to think I was looking for a job.&#160; I&#8217;m not.</p>
<p>But, I think it is worth taking a moment to talk about one of the biggest blunders professional programmers make in their <strike>job search</strike>, careers.</p>
<p>The mistake is not having a resume that is of the same caliber in both presentation and content as the set of skills you are trying to sell.</p>
<h2>I&#8217;m no marketing whiz</h2>
<p>But, over the years I have come to understand the value of marketing.&#160; As a professional, <a href="http://simpleprogrammer.com/2010/01/12/selling-yourself/">you should always be trying to sell yourself</a>.</p>
<p>I&#8217;ll be honest, part of the reason I write this blog is because I know it is a valuable tool for marketing myself.</p>
<p>For most professional programmers your resume is your only real marketing method. When it looks worse than a 3rd grade book report and I see it, I immediately think you suck.</p>
<p>Sorry for the boldness, but no need to mince words here.&#160; </p>
<p><strong>Unless you have some awesome open source project you built, or a blog, or some other kind of flashy marketing tool, your resume IS your ONLY marketing and if that sucks, people are going to assume you do also.</strong>&#160;</p>
<p>To be completely honest again, in some part you have to admit that you do suck. Perhaps you are a great programmer, but you suck at common sense.&#160; I mean going through all the hard work of becoming a great programmer and not having the common sense to do the easy thing that will make it all worth it…</p>
<p><b><a href="http://www.imdb.com/name/nm0000681/">Trent</a></b>: You know what you are? You&#8217;re like a big bear with claws and with fangs&#8230;    <br /><b><a href="http://www.imdb.com/name/nm0887187/">Sue</a></b>: &#8230;big &amp;!%* teeth, man.    <br /><b><a href="http://www.imdb.com/name/nm0000681/">Trent</a></b>: Yeah&#8230; big &amp;!%*teeth on ya&#8217;. And she&#8217;s just like this little bunny, who&#8217;s just kinda cowering in the corner.    <br /><b><a href="http://www.imdb.com/name/nm0887187/">Sue</a></b>: Shivering.    <br /><b><a href="http://www.imdb.com/name/nm0000681/">Trent</a></b>: Yeah, man just kinda&#8230; you know, you got these claws and you&#8217;re staring at these claws and you’re thinking to yourself, and with these claws you&#8217;re thinking, &quot;How am I supposed to kill this bunny, how am I supposed to kill this bunny?&quot;    <br /><b><a href="http://www.imdb.com/name/nm0887187/">Sue</a></b>: And you&#8217;re poking at it, you&#8217;re poking at it&#8230;    <br /><b><a href="http://www.imdb.com/name/nm0000681/">Trent</a></b>: Yeah, you&#8217;re not hurting it. You&#8217;re just kinda gently batting the bunny around, you know what I mean? And the bunny&#8217;s scared Mike, the bunny&#8217;s scared of you, shivering.    <br /><b><a href="http://www.imdb.com/name/nm0887187/">Sue</a></b>: And you got these &amp;!%* claws and these fangs&#8230;    <br /><b><a href="http://www.imdb.com/name/nm0000681/">Trent</a></b>: And you got these &amp;!%* claws and these fangs, man! And you&#8217;re looking at your claws and you&#8217;re looking at your fangs. And you&#8217;re thinking to yourself, you don&#8217;t know what to do, man. &quot;I don&#8217;t know how to kill the bunny.&quot; With *this* you don&#8217;t know how to kill the bunny, do you know what I mean?    <br /><b><a href="http://www.imdb.com/name/nm0887187/">Sue</a></b>: You&#8217;re like a big bear, man.    <br /><b><a href="http://www.imdb.com/name/nm0269463/">Mike</a></b>: So you&#8217;re not just like &amp;!%* with me?    <br /><b><a href="http://www.imdb.com/name/nm0000681/">Trent</a></b>: No I&#8217;m not &amp;!%* with you.    <br /><b><a href="http://www.imdb.com/name/nm0887187/">Sue</a></b>: Honestly, man.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/02/swingers443551.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="swingers443551" border="0" alt="swingers443551" src="http://complextosimple.files.wordpress.com/2011/02/swingers443551_thumb.jpg?w=464&#038;h=576" width="464" height="576" /></a></p>
<p>I know you might be thinking I am being harsh here, but really it is for your own good.</p>
<p>Let me give you an example.</p>
<p>Lets pretend that Pepsi let me design their new logo and ad copy for a magazine ad.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/02/pepsi-yum.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="pepsi yum" border="0" alt="pepsi yum" src="http://complextosimple.files.wordpress.com/2011/02/pepsi-yum_thumb.jpg?w=473&#038;h=463" width="473" height="463" /></a></p>
<p align="center"><u><em><strong>PEPSI!&#160; It eez so freakin good it will nock you&#8217;re socks off!</strong></em></u></p>
<p align="center"><u><em><strong>I drink it all the time.&#160; I have spearheaded drinking it at parties with 100-1000 people.</strong></em></u></p>
<p align="center"><u><em><strong>PEPSI! YES!&#160; TOTALLY AWESOME AND QUITE DRINKABLE!</strong></em></u></p>
<p>Next time you are at the store and you see a 12 pack of Pepsi with my hand drawn logo on it, and a big white board with no color that says &quot;DRINK PEPSI! TOTALLY AWESOME AND QUITE DRINKABLE&quot; are you going to buy it or are you going to think whoever made that piece of crap is a doofus?</p>
<p>The situation is far worse than what I described.&#160; Pepsi has many marketing venues and advertising campaigns.&#160; <strong>If all you have is your resume, then you just have one.</strong> There is nothing wrong with that, but it better be damn good!</p>
<p>Your resume is like a Superbowl ad for you and your programming skills.</p>
<h2>I would love to make a nice resume, but I do suck at writing and design</h2>
<p>It is good that you admit that, because if you are a programmer and your name isn&#8217;t <a href="http://www.hugoware.net/">Hugo</a>, you probably do suck at writing and design and that is Ok.&#160; </p>
<p>You should not be the person writing your resume or designing it.&#160; You should provide the information, you should review the design, you should even proof-read and edit it, but you should be letting someone else do most of the work.</p>
<p>Why?&#160; Because there are professionals that know how to write and design a resume. Perhaps with many hours of work and research you could create a resume as good or better than a professional resume writer, but chances are you can&#8217;t.</p>
<p>Just like a professional resume writer shouldn&#8217;t try and write their own software, they should hire you to do it, you shouldn&#8217;t try and write your own resume.</p>
<p>This might seem like strange advice.&#160; Perhaps it seems like a cop-out or uber-lame. You are welcome to think that if you want.</p>
<p>Perhaps you think, well if everyone did that then all resumes would be nicely formatted, not contain spelling errors and typos and be full of marketing hype.&#160; </p>
<p>That is true, but the good news for you is that HARDLY ANYONE IS DOING IT and by being one of the few who do, your resume will stand out and make the others look like something your cat puked up.</p>
<p>If you can get an edge, take the edge!</p>
<h2>People should judge my skills not my design and formatting on my resume</h2>
<p>I completely disagree.&#160; Having an ugly resume tells me a large amount about you&#8230; shall I list a few prejudice things I assume about you when I see your double spaced 6 page monstrosity?</p>
<ol>
<li>Doesn&#8217;t care about appearances &#8211; hmm too bad my customers do. </li>
<li>Doesn&#8217;t have self-respect &#8211; those who don&#8217;t respect themselves don&#8217;t respect others usually. </li>
<li>No sense of pride &#8211; if you can&#8217;t take pride in your own work, why do I expect you to take pride in the software you are building for me? </li>
<li>Doesn&#8217;t value the opinion of others. </li>
<li>Is limited by own abilities &#8211; if you can&#8217;t reach out to others for help in your area of weakness, I expect when you can&#8217;t figure something out, you&#8217;ll just flounder. </li>
<li>Doesn&#8217;t care about quality. </li>
<li>Is not detail oriented. </li>
<li>Lacks ambition, will settle for less. </li>
<li>Your code reflects all the above statements. </li>
</ol>
<p>Unfair?&#160; Perhaps.&#160; Untrue?&#160; Perhaps, but given no other information and a stack of 100 more resumes to go through, I&#8217;m going to go ahead and be prejudice.</p>
<h2>But it costs too much money</h2>
<p>It is called an investment. <strong> If you are not willing to put some money into your career, then why should someone else be willing to take a chance and make an investment in you?</strong></p>
<p>A good professional resume writer might charge around $500.&#160; That is chump change compared to the potential earnings increase of having a professionally written and designed resume.&#160; You may increase your negotiating power by a huge margin by appearing to be more professional and you will certainly increase the number of possible opportunities that are available to you.</p>
<p>Let&#8217;s use some real fake numbers like I always like to do.</p>
<p>Suppose that you have skills that on average would be worth about 60k a year in the market and your resume is getting around a 10% response rate from jobs you submit to.</p>
<p>Now of the jobs you submit to, most of the 10% responses you are getting are for jobs that pay right at your market rate, some are above, very few are way above, and some are below.</p>
<p>If you invest $500 in getting a really well done professional resume, you could reasonably expect to increase the base value of your market rate to say 65k and improve your response rate to 20%.</p>
<p>That is a pretty modest improvement, which only requires an investment of money to achieve.&#160; The immediate return on that investment is HUGE, and the lifelong return on that investment could be literally 10s of 1000s of percent.</p>
<p>Think about it.&#160; If you increase your starting wage at a company by just 5k, for a $500 investment, and you work for that company for 10 years, if you got no raises at all, you would make 50k return on your $500 investment.</p>
<p>Raises, by the way, are usually based on percentages of base pay, so it is realistically going to be even more.</p>
<p>Two more points, then I promise to stop beating this horse.</p>
<ul>
<li>It is likely that someone who had a very bad resume and expected to find jobs making 60k, could get a very good resume and find that one company willing to pay them 80k for that same set of skills. </li>
<li>It is very much more likely that the dream job you are after will have a much wider pool of candidates to choose from, even if they are paying the same market rate.&#160; So by having the better resume, you are more likely to get seen and get that dream job. </li>
</ul>
<h2>Final advice</h2>
<ul>
<li>Don’t hire some cheap resume writing service. You get what you pay for.&#160; Spend the money to get it done by a real professional who will do it right.&#160; This is the only hard part you have to do, you need to do a large amount of research here.&#160; I have hired several resume writers for myself and my wife, and I have had to fire a few, because they were basically just rip-offs.&#160; If you try to be cheap and you end up getting ripped off, don’t come crying to me. <img style="border-style:none;" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://complextosimple.files.wordpress.com/2011/02/wlemoticon-smile.png?w=500" /></li>
<li>Review the resume.&#160; You will need to go through more than 1 revision, probably at least 5.&#160; If you want something changed, ask for it.&#160; They will do it, you are paying for it.</li>
<li>You are still going to do most of the work of gathering the information.&#160; Figure out all the things you want to have on your resume and put it all in a mind-map to give to the resume writer.&#160; Garbage in, garbage out.</li>
<li>Whether you take this advice or not, before you ever send your resume to an employer, send it to your mom, your girlfriend, your English major buddy, anyone who will look at it and tell you why it is ugly and what you need to do to fix it.</li>
</ul>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.&#160; Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.&#160; Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1254/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1254&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2011/02/15/professionals-have-professional-looking-resumes/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/02/swingers443551_thumb.jpg" medium="image">
			<media:title type="html">swingers443551</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/02/pepsi-yum_thumb.jpg" medium="image">
			<media:title type="html">pepsi yum</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/02/wlemoticon-smile.png" medium="image">
			<media:title type="html">Smile</media:title>
		</media:content>
	</item>
		<item>
		<title>Back to Basics: Mock Eliminating Patterns</title>
		<link>http://simpleprogrammer.com/2011/01/26/back-to-basics-mock-eliminating-patterns/</link>
		<comments>http://simpleprogrammer.com/2011/01/26/back-to-basics-mock-eliminating-patterns/#comments</comments>
		<pubDate>Wed, 26 Jan 2011 14:09:32 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/2011/01/26/back-to-basics-mock-eliminating-patterns/</guid>
		<description><![CDATA[In my previous post I talked about unit testing without mocks.  I gave some examples of how I had done this in some of my real code from PaceMaker. This time I want to take a look at some of the common patterns we can use to extract parts of our code into dependency-lite or [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1236&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://simpleprogrammer.com/2011/01/23/back-to-basics-unit-testing-without-mocks/">previous post</a> I talked about unit testing without mocks.  I gave some examples of how I had done this in some of my real code from <a href="http://www.appbrain.com/app/pacemaker-(running)/com.pacemaker.android">PaceMaker</a>.</p>
<p>This time I want to take a look at some of the common patterns we can use to extract parts of our code into dependency-lite or dependency-less classes that we can unit test without needing mocks.</p>
<p>I’ll have to admit, I haven’t really be practicing this approach for a long time, so my list of patterns is probably lacking, but these are some of the ones I kept seeing as I was working through the code.</p>
<p>If you have some ideas for other patterns or know of some already or have better names than what I have chosen, please let me know.</p>
<h2>Pattern 1: Pull out state</h2>
<p>In many cases we can have complex objects that contain their own state often split across member variables.</p>
<p>If you find that a class has many methods that check some member variable and do one thing or another based on what value that member variable is set to, you might be able to benefit by extracting all the state changing logic into a separate class and creating events to notify the original class when the state changes.</p>
<p>We can then keep the new class as the single source of state for the class it came from and easily write dependency free (level 2) unit tests for the state class.</p>
<p>From the perspective of the class you are pulling the state out of, we might turn:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:14face58-4695-432f-ab79-a9eadf095d0d" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp; pad-line-numbers: true;">
isSpecial = false;
timeToLive = 50;
timeToLove = 1
penguinsHaveAttacked = true;

public ApocalypseScenario CreateNew()
{
    if(isSpecial &amp;&amp; timeToLive &lt; timeToLove || penguinsHaveAttacked)
        return new ApocalypseScenario(&quot;Penguin Apocalypse, Code Blue&quot;);
}
</pre></p>
</div>
<p>Into:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:0a54b67e-f20c-4b91-a14f-0684e7a52cd7" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp;">
private apocalypseStateMachine = new ApocalypseStateMachine();

public ApocalypseScenario CreateNew()
{
    if(apocalypseStateMachine.State == States.PENGUIN_SLAUGHTER)
        return new ApocalypseScenario(&quot;Penguin Apocalypse, Code Blue&quot;);
}
</pre></p>
</div>
<p>Obviously in this case there would be many ways the states get changed.  I haven’t included those here, but you can imagine how those would become part of the new state class.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/01/attack-penguin.jpg"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="attack-penguin" src="http://complextosimple.files.wordpress.com/2011/01/attack-penguin_thumb.jpg?w=490&#038;h=324" border="0" alt="attack-penguin" width="490" height="324" /></a></p>
<h2>Pattern 2: Method to class</h2>
<p>Often I have found that I can’t find a way to extract all of the logic out of a class into one cohesive class, because of multiple interactions inside the class.</p>
<p>When I encounter this problem, I have found that I can usually identify a single large method that is using data from dependencies in the class to perform its logic.</p>
<p>These methods can usually be extracted to be their own new class.  We can pass in the data from the dependencies the method was originally using instead of using the dependencies directly.</p>
<p>Good candidates for this kind of pattern are methods which use multiple dependencies to get data in order to compute a single result.</p>
<p>When applying this pattern we may end up with a very small class, containing only 1 or 2 methods, but we are able to easily unit test this class with state and dependency free unit tests (level 1.)</p>
<h2>Pattern 3: Pull data sources up</h2>
<p>It is often the case that we have dependencies in a class that exist only to provide some sort of data to the actual logic in the class.</p>
<p>In these situations it is often possible to pull the source of the data up and out of the methods that use the data and instead pass in just the used data.</p>
<p>Performing this refactoring allows us to have methods in our class that do not use the dependency in the class directly, but instead have the data that dependency class provided passed in.</p>
<p>When we do this, it opens up the ability to write clean and simple unit tests without mocks for those methods in the class or to apply the “Method to class” pattern to extract the method into its own testable class.</p>
<p>As a first step to refactoring my code to make it easily unit testable, I will often scan the methods in the class to find methods that are only using data from dependencies, but not manipulating the state or sending commands to the dependencies.  As I am able to identify these methods, I can apply this pattern to remove the dependencies from the methods.</p>
<h2>Pattern 4: Gather then use</h2>
<p>Often we find that we cannot pull a dependency out of a method because that dependency is being manipulated inside of a loop or in the middle of some process.</p>
<p>In cases like these, we can often gather all the data that will be used to manipulate the dependency in some form of a collection and then reiterate through that collection to perform the manipulation on the dependency.</p>
<p>A good way to envision this is to think about two people mailing letters.  In one scenario we might have the first guy stuffing the envelope and the 2nd guy sealing the envelope.</p>
<p>We could carry on this process of 1st guy stuffs, 2nd guy seals for each letter we want to mail, but the process is dependent on both guys being present and working.</p>
<p>If we change it so that the first guy stuffs all the envelopes first and gives that stack of stuffed envelopes to the 2nd guy, they can do their jobs independently.</p>
<p>By applying the same idea to algorithms in our code, we can take a single method and break off the parts that don’t rely on dependencies to test independently or even move to their own classes.</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.  Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1236/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1236/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1236&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2011/01/26/back-to-basics-mock-eliminating-patterns/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/01/attack-penguin_thumb.jpg" medium="image">
			<media:title type="html">attack-penguin</media:title>
		</media:content>
	</item>
		<item>
		<title>Back To Basics: Unit Testing Without Mocks</title>
		<link>http://simpleprogrammer.com/2011/01/23/back-to-basics-unit-testing-without-mocks/</link>
		<comments>http://simpleprogrammer.com/2011/01/23/back-to-basics-unit-testing-without-mocks/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 22:43:46 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/?p=1230</guid>
		<description><![CDATA[In my last post, I revealed my conclusions regarding what to do instead of misusing IoC containers and interfaces all over your code mostly for the purpose of unit testing. One of the approaches I suggested was to focus on writing level 1 or level 2 unit tests and occasionally level 3 unit tests with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1230&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://simpleprogrammer.com/2011/01/14/back-to-basics-unit-testing-automated-blackbox-testing-and-conclusions/">my last post</a>, I revealed my conclusions regarding what to do instead of misusing IoC containers and interfaces all over your code mostly for the purpose of unit testing.</p>
<p>One of the approaches I suggested was to focus on writing level 1 or level 2 unit tests and occasionally level 3 unit tests with 1 or possibly 2 dependencies at most.</p>
<p>I want to focus on how this is possible, using real code where I can.</p>
<h2>First let’s talk about the approach</h2>
<p>When I first talk about writing mainly level 1 or level 2 unit tests, most people assume that I mean to cherry pick the few classes in your code base that already qualify and only write unit tests for those classes.</p>
<p>That is not at all what I am advocating.</p>
<p>Instead, the approach I am suggesting is to find ways to make most of the actual logic in your code become encapsulated into classes that depend mostly on data.</p>
<p>What I mean by this is that our goal should be to refactor or write our code in such a way that logic is grouped into classes that only depend on primitive types and data classes, not other classes that contain logic.</p>
<p>This of course is not fully achievable, because something will have to tie all of these logic containing classes together.  We we need these tie-together classes, but if we can make their job to simply execute commands and tie other classes together, we can feel pretty confident in not unit testing them, and we make our job a whole lot easier.</p>
<p>So to summarize, the basic strategy we are going to employ here is to eliminate the need for mocks by designing our classes to be much smaller, having much tighter roles and responsibilities, and operating on data that is passed in rather than manipulating methods on other classes.</p>
<p>There are many patterns we can use to achieve this goal.  I’ll show you an example, then I’ll try to cover some of the major patterns I have discovered so far.</p>
<h2>A real world example</h2>
<p>I recently released a Java based Android application called <a href="http://simpleprogrammer.com/2011/01/04/introducing-pacemaker-for-android/">PaceMaker</a>.  When I had started out building this application, I set out with the high and mighty goal of using Google Guice framework for dependency injection and BDD style unit tests using JMock to mock passed in dependencies.  It wasn’t a horrible approach, I wrote about it <a href="http://simpleprogrammer.com/2010/07/27/the-best-way-to-unit-test-in-android/">here</a>.</p>
<p>What I found with this approach though was that I was spending a large amount of time creating mocks, and I wasn’t getting much benefit from it.  So, I had abandoned writing unit tests for the code all together, and I pulled out the now almost useless Guice.</p>
<p>The past couple of nights, I decided to use this project as a test project to demonstrate some of the ideas I have been talking about and thinking about.</p>
<p>I wanted to take my real code, refactor the logic out of it into small classes with little or no dependencies, and write BDD style unit tests that would be simple and easy to understand.</p>
<p>The big challenge here was trying to find a small enough piece of code to use an as example.  For this example, I am going to use a bit of code that I was using to generate the name of a file that I write to disk for saving the data from a run.</p>
<p>This code was originally inside of a presenter class that handled generating a file name to pass to a serializer class in order to serialize the run.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/01/history_detail1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border:0;" title="history_detail" src="http://complextosimple.files.wordpress.com/2011/01/history_detail_thumb1.png?w=303&#038;h=536" border="0" alt="history_detail" width="303" height="536" /></a></p>
<p>Here is the original private method that existed in the presenter.</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:dc356276-d88f-4de6-a6f3-e0001ba6c033" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: java; pad-line-numbers: true;">
private String getRunFileName()
{
     String completeFileName = StorageManager.getDataStorageDirectory();
     Location firstLocation = locationTracker
          .getLocations().iterator().next();
     Date firstPointTime = new Date(firstLocation.getTime());

     SimpleDateFormat dateFormat = new
         SimpleDateFormat(&quot;MMddyyyy_HHmmss&quot;);
     dateFormat.setTimeZone(TimeZone.getTimeZone(&quot;GMT&quot;));
     String fileName = dateFormat.format(firstPointTime) + &quot;.gpx&quot;;

     completeFileName = completeFileName + fileName;
     return completeFileName;
}
</pre></p>
</div>
<p>&nbsp;</p>
<p>This method was a perfect candidate for some easily testable logic that could be put into its own class, but there are a few problems we should notice here.</p>
<ul>
<li>We are dependent on <em>StorageManager</em> to get the data storage directory used as the base directory.</li>
<li>We are dependent on the <em>locationTracker</em> object to get the time of the first location.</li>
<li>There is some real logic here in the form of a transformation.  (It is important to note that we are dealing with logic, not just commands, because testing execution of commands is not as important as testing logic.)</li>
</ul>
<p>My approach to this refactor is actually pretty simple.  The first thing we need to do is see the dependencies for what they are.  It looks like our logic is dependent on <em>StorageManager</em> and <em>locationTracker</em>, but in reality the logic is dependent on the string which is the base directory for the file and the time to use for the file name.</p>
<p>We can change this code to reflect that pretty easily.</p>
<p>&nbsp;</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:07e80d6b-00eb-492e-ad00-eac88b93cf95" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: java;">
private String getRunFileName(String baseDirectory, Calendar time)
{
     String completeFileName = baseDirectory;
     SimpleDateFormat dateFormat = new
         SimpleDateFormat(&quot;MMddyyyy_HHmmss&quot;);
     dateFormat.setTimeZone(TimeZone.getTimeZone(&quot;GMT&quot;));
     String fileName = dateFormat.format(time.getTime()) + &quot;.gpx&quot;;

     completeFileName = completeFileName + fileName;
     return completeFileName;
}
</pre></p>
</div>
<p>What we have done here is small, but it is critical.  We have eliminated dependencies that would otherwise have to be mocked to test this logic.  Sure, we will still need to use those dependencies to get the data to pass into this method, but we can leave that code in the presenter class and move this code into its own class.  The class will be small for now, but it will be easily testable with a level 1 unit test (our favorite kind.)</p>
<h2>More examples</h2>
<p>I didn’t cherry pick this example from the source code in PaceMaker, but I did cherry pick it for this blog post, because it was one of the shorter examples I could use.</p>
<p>I have several other areas of code in my presenter class in PaceMaker where I used a similar approach to extract out the logic, pull it into its own class with little or no dependencies and write unit tests for.</p>
<p>Here are two other examples:</p>
<ul>
<li>Pulled the state logic for starting, stopping, pausing, resuming and calculating length of time paused during a run into its own class.  I elected to add one dependency (the presenter itself as an observer) to the refactored class in order to allow the class to notify the presenter when the state changed.  In C# I would have just used an event to do this, but in Java we use the observer pattern.</li>
<li>Pulled out the logic that created the GPX data files into a GPXDataModelBuilder which instead of depending on the LocationTracker class depended only on the data from that class.</li>
</ul>
<p>The result ended up being very clear, easy to write, level 1 and level 2 unit tests with no mocking.  In addition, my class structure is now much more tightly cohesive, with a much tighter and clearer responsibility.  Before my presenter was doing several things, but now many of those things are broken up into very small testable classes.</p>
<p>In my next post, I’ll go into the patterns you can use to create classes that are able to be tested by level 1 and level 2 unit tests.</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.  Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1230/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1230&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2011/01/23/back-to-basics-unit-testing-without-mocks/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/01/history_detail_thumb1.png" medium="image">
			<media:title type="html">history_detail</media:title>
		</media:content>
	</item>
		<item>
		<title>Back to Basics: Unit Testing, Automated Blackbox Testing, and Conclusions!</title>
		<link>http://simpleprogrammer.com/2011/01/14/back-to-basics-unit-testing-automated-blackbox-testing-and-conclusions/</link>
		<comments>http://simpleprogrammer.com/2011/01/14/back-to-basics-unit-testing-automated-blackbox-testing-and-conclusions/#comments</comments>
		<pubDate>Fri, 14 Jan 2011 14:24:50 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Functional]]></category>
		<category><![CDATA[Process Improvement]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[User]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/2011/01/14/back-to-basics-unit-testing-automated-blackbox-testing-and-conclusions/</guid>
		<description><![CDATA[If you’ve been following me from the beginning of the Back to Basics series, you’ll know that I set out to reevaluate some of the commonly held truths of what best practices are, especially in regards to unit testing, dependency injection and inversion of control containers. We’ve talked about what an interface is, cohesion and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1225&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you’ve been following me from the <a href="http://simpleprogrammer.com/2010/10/30/getting-back-to-basics-introduction-and-why/">beginning</a> of the <a href="http://simpleprogrammer.com/back-to-basics-series/">Back to Basics series</a>, you’ll know that I set out to reevaluate some of the commonly held truths of what best practices are, especially in regards to unit testing, dependency injection and inversion of control containers.</p>
<p>We’ve talked about <a href="http://simpleprogrammer.com/2010/11/02/back-to-basics-what-is-an-interface/">what an interface</a> is, <a href="http://simpleprogrammer.com/2010/11/04/back-to-basics-cohesion-and-coupling-part-1/">cohesion and coupling</a>, and even went a little bit off track to talk about <a href="http://simpleprogrammer.com/2010/12/07/back-to-basics-sorting/">sorting</a> for a bit.</p>
<p>One of the reoccurring themes that kept showing up in most of the posts was unit testing.&#160; I talked about <a href="http://simpleprogrammer.com/2010/12/12/back-to-basics-why-unit-testing-is-hard/">why unit testing is hard</a>, and I defined three levels of unit testing.</p>
<ul>
<li><strong>Level 1</strong> &#8211; we have a single class with no external dependencies and no state.&#160; We are just testing an algorithm.</li>
<li><strong>Level 2</strong> &#8211; we have a single class with no external dependencies but it does have state.&#160; We are setting up an object and testing it as a whole.</li>
<li><strong>Level 3</strong> &#8211; we have a single class with at least one external dependency, but it does not depend on its own internal state.</li>
<li><strong>Level 4</strong> &#8211; we have a single class with at least one external dependency and depends on its own internal state.</li>
</ul>
<p>Throughout this series I ended up tearing down using interfaces with only single non-unit test implementation.&#160; I criticized the overuse of dependency injection for the sole purpose of unit testing.&#160; I attacked a large portion of best practices that I felt were only really being used in order to be able to unit test classes in isolation.</p>
<p>But, I never offered a solution.&#160; I told you what was bad, but I never told you what was good.</p>
<p>I said don’t create all these extra interfaces, use IoC containers all over your app, and mocks everywhere just for the purpose of being able to isolate a class you want to unit test, but when you asked me what to do instead, I said “I don’t know, I just know what we are doing is wrong and we need to stop.”</p>
<p>Well, that is no answer, but I intend to give one now.&#160; I’ve been thinking about this for months, researching the topic and experimenting on my own.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/01/cool-experiment.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="cool experiment" border="0" alt="cool experiment" src="http://complextosimple.files.wordpress.com/2011/01/cool-experiment_thumb.jpg?w=488&#038;h=390" width="488" height="390" /></a></p>
<h2>I finally have an answer</h2>
<p>But, before I give you it, I want to give you a little background on my position on the subject matter.</p>
<p>I come from a pretty solid background of unit testing and test driven development.&#160; I have been preaching both for at least the last 7 years.</p>
<p>I was on board from the beginning with dependency injection and IoC containers.&#160; I had even rolled my own as a way to facilitate isolating dependencies for true unit tests.</p>
<p>I think unit testing and TDD are very good skills to have.&#160; I think everyone should learn them.&#160; TDD truly helps you write object oriented code with small concentrated areas of responsibility.</p>
<p>But, after all this time I have finally concluded, for the most part, that unit tests and practicing TDD in general do more good for the coder than the software.</p>
<p>What?&#160; How can I speak such blasphemy?</p>
<p>The truth of the matter is that I have personally grown as a developer by learning and practicing TDD, which has lead me to build better software, but not because the unit tests themselves did much.&#160; </p>
<p>What happened is that while I was feeling all that pain of creating mocks for dependencies and trying to unit test code after I had written it, I was learning to reduce dependencies and how to create proper abstractions.&#160; </p>
<p>I feel like I learned the most when the IoC frameworks were the weakest, because I was forced to minimize dependencies for the pain of trying to create so many mocks or not being able to unit test a class in isolation at all.</p>
<p>I’ve gotten to the point now where two things have happened:</p>
<ol>
<li>I don’t need the TDD training wheels anymore.&#160; I don’t pretend to be a coding god or demi-god of some sort, but in general the code I write that is done in a TDD or BDD style is almost exactly the same as the code I write without it.</li>
<li>The IoC containers have made it so easy to pass 50 dependencies into my constructor that I am no longer feeling the pain that caused my unit tests to cause me to write better code.</li>
</ol>
<p>What I find myself ending up with now when I write unit tests is 70% mocking code that verifies that my code calls certain methods in a certain order.</p>
<p>Many times I can’t even be sure if my unit test is actually testing what I think it is, because it is so complex.</p>
<h2>Umm, did you say you had an answer, dude?</h2>
<p>Yes, I do have an answer.&#160; I just wanted to make sure you understand where I am coming from before I throw out all these years of practical knowledge and good practices.</p>
<p><strong>I am not the enemy.</strong></p>
<p>My answer to the problem of what to do if you shouldn’t be using IoC containers and interfaces all over your code base just for the purpose of unit testing, is to take a two pronged approach.</p>
<p><a href="http://complextosimple.files.wordpress.com/2011/01/2prong.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="2prong" border="0" alt="2prong" src="http://complextosimple.files.wordpress.com/2011/01/2prong_thumb.jpg?w=480&#038;h=480" width="480" height="480" /></a></p>
<ol>
<li>Mostly only write level 1 or level 2 unit tests.&#160; Occasionally write level 3 unit tests if you only have 1 or possibly 2 dependencies.&#160; (I’ll talk about more how to do this in my next post)</li>
<li>Spend a majority of your effort, all the time you would have spent writing unit tests, instead writing what I will call blackbox automated tests or BATs.&#160; (I used to call this automated functional tests, but I think that name is too ambiguous.)</li>
</ol>
<p>I intend to drill really deep into these approaches in some upcoming posts, but I want to briefly talk about why I am suggesting these two things in place of traditional BDD or TDD approaches.</p>
<h2>What are the benefits?</h2>
<p><strong>The first obvious benefit is that you won’t be complicating your production code with complex frameworks for injecting dependencies and other clever things that really amount to making unit testing easier.</strong></p>
<p>Again, I am not saying you shouldn’t ever use dependency injection, interfaces or IoC containers.&#160; I am just saying you should use them when they provide a real tangible value (which most of the time is going to require alternate non-unit test implementations of an interface.)</p>
<p>Think about how much simpler your code would be if you just went ahead and new’d up a concrete class when you needed it.&#160; If you didn’t create an extra interface for it, and then pass it in the constructor.&#160; You just used it where you needed it and that was that.</p>
<p><strong>The second benefit is that you won’t spend so much time writing hard unit tests.</strong>&#160; I know that when I am writing code for a feature I usually spend at least half the amount of time writing unit tests.&#160; This is mostly because I am writing level 3 and level 4 unit tests, which require a large number of mocks.</p>
<p>Mocks kill us.&#160; Mocking has a negative ROI.&#160; Not only is creating them expensive in terms of time, but it also strongly couples our test classes to the system and makes them very fragile.&#160; Plus, mocking adds huge amounts of complexity to unit tests.&#160; Mocking usually ends up causing our unit test code to become unreadable, which makes it almost worthless.</p>
<p>I’ve been writing mocks for years.&#160; I know just about every trick in the book.&#160; I can show you how to do it in Java, in C#, even in C++.&#160; It is always painful, even with auto-mocking libraries.</p>
<p>By skipping the hard unit tests and finding smart ways to make more classes only require level 1 and level 2 unit tests, you are making your job a whole lot easier and maximizing on the activities that give you a high ROI.&#160; Level 1 and level 2 unit tests, in my estimation, give very high ROIs.</p>
<p><strong>The thirds benefit is that blackbox automated tests are the most valuable tests in your entire system and now you’ll be writing more of them.&#160; </strong>There are many names for these tests, I am calling them BATs now, but basically this is what most companies call automation.&#160; Unfortunately, most companies leave this job to a QA automation engineer instead of the development teams.&#160; Don’t get me wrong, QA automation engineers are great, but there aren’t many of them, good ones are very expensive, and the responsibility shouldn’t lie squarely on their shoulders.</p>
<p>BATs test the whole system working together.&#160; BATs are your automated regression tests for the entire system.&#160; BATs are automated customer acceptance tests and <strong>the ROI for each line for code in a BAT can be much higher than the ROI of each line of production code.</strong></p>
<p>Why?&#160; How is this even possible?&#160; It’s all about leverage baby.&#160; Each line of code in a BAT may be exercising anywhere from 5 to 500 lines of production code, which is quite the opposite case of a unit test where each line of unit test code might only be testing a 1/8th or 1/16th a line of production code on average (depending on code coverage numbers being reached.)</p>
<p>I’ll save the detail for later posts, but it is my strong opinion that a majority of a development teams effort should be put in BATs, because BATs</p>
<ul>
<li>Have high value to the customer</li>
<li>Regression test the entire system</li>
<li>Have a huge ROI per line of code (if you create a proper BAT framework)</li>
</ul>
<p>Imagine how much higher quality your software would be if you had a BAT for each backlog item in your system which you could run every single iteration of your development process.&#160; Imagine how confident you would be in making changes to the system, knowing that you have an automated set of tests that will catch almost any break in functionality.</p>
<p>Don’t you think that is worth giving up writing level 3 and level 4 unit tests, which are already painful and not very fun to begin with to achieve?</p>
<p>In my future posts on the Back to Basics series, I will cover in-depth how to push more of your code into level 1 and level 2 unit tests by extracting logic out to separate classes that have no dependencies, and I will talk more about BATs, and how to get started and be successful using them.&#160; (Hint: you need a good BAT framework.)</p>
<h6>As always, you can subscribe to this <a href="http://feeds.feedburner.com/MakingTheComplexSimple">RSS feed</a> to follow my posts on Making the Complex Simple.&#160; Feel free to check out <a href="http://elegantcode.com/">ElegantCode.com</a> where I post about the topic of writing elegant code about once a week.&#160; Also, you can follow me on twitter <a href="http://twitter.com/jsonmez">here</a>.</h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1225/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1225&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2011/01/14/back-to-basics-unit-testing-automated-blackbox-testing-and-conclusions/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/01/cool-experiment_thumb.jpg" medium="image">
			<media:title type="html">cool experiment</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2011/01/2prong_thumb.jpg" medium="image">
			<media:title type="html">2prong</media:title>
		</media:content>
	</item>
		<item>
		<title>Back to Basics: Why Unit Testing is Hard</title>
		<link>http://simpleprogrammer.com/2010/12/12/back-to-basics-why-unit-testing-is-hard/</link>
		<comments>http://simpleprogrammer.com/2010/12/12/back-to-basics-why-unit-testing-is-hard/#comments</comments>
		<pubDate>Sun, 12 Dec 2010 21:47:08 +0000</pubDate>
		<dc:creator>jsonmez</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">https://complextosimple.wordpress.com/2010/12/12/back-to-basics-why-unit-testing-is-hard/</guid>
		<description><![CDATA[More and more lately, I’ve been beginning to question the value of unit testing.&#160; I’ve really been starting to wonder if all the work we put into being able to actually test at the unit level and the extra scaffolding we put into our applications to support it is worth the cost. I’m not going [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1195&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>More and more lately, I’ve been beginning to question the value of unit testing.&#160; I’ve really been starting to wonder if all the work we put into being able to actually test at the unit level and the extra scaffolding we put into our applications to support it is worth the cost.</p>
<p>I’m not going to talk about that subject yet.&#160; Instead, I want to look at some of the costs of unit testing and ask the question “why is unit testing hard?”</p>
<p>After all, if unit testing weren’t hard, we wouldn’t have to question whether or not it was worth it.&#160; It makes sense then to look at first why it is hard and what makes it hard.</p>
<p><a href="http://complextosimple.files.wordpress.com/2010/12/ar119675077156567.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="ar119675077156567" border="0" alt="ar119675077156567" src="http://complextosimple.files.wordpress.com/2010/12/ar119675077156567_thumb.jpg?w=485&#038;h=364" width="485" height="364" /></a></p>
<h2></h2>
<h2>The ideal scenario</h2>
<p>Unit testing itself is rather easy once you understand how to do it.&#160; Even test driven or behavior driven development is easy one mastered… at least for the ideal scenario.</p>
<p>What is the ideal scenario then?</p>
<p><strong>It is a unit test where the class under test has no external dependencies.</strong></p>
<p>When a class we are writing unit tests for doesn’t have any external dependencies, we don’t need mocks or stubs or anything else.&#160; We can just write code that tests our code.</p>
<p>Let’s look at an example of this.&#160; Suppose, I had a class called <em>Calculator.&#160; </em>This <em>Calculator</em> class has some very simple methods.&#160; Specifically, let us talk about testing a method <em>Add.&#160; Add </em>takes two single digit integers and returns the result.&#160; If either integer passed in is more than a single digit, it throws an exception.</p>
<p>It is a pretty stupid method, with little use, but it will serve the point well here.</p>
<p>We can TDD or BDD this baby with minimal effort.</p>
<p>Let’s start thinking of test cases:</p>
<ul>
<li>When I add 0 and a single digit number it should return the single digit number </li>
<li>When I add 0 and 0 it should return 0 </li>
<li>When I add two single digit numbers it should return the sum of those numbers </li>
<li>When I add one two digit number it should throw an exception </li>
</ul>
<p>Pretty easy to come up with test cases, just as easy to implement them:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:76f84a40-5e84-4388-adcf-805db05ac012" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp; pad-line-numbers: true;">
[Test]
public void ZeroAndANumber_IsANumber()
{
   var calculator = new Calculator();
   var result =  calculator.Add(0, 5);
   
   Assert.AreEqual(5, result);
}
</pre>
</pre>
</div>
<p>&#160;</p>
<p>We can then implement the code that will make this test pass pretty easily.&#160; I won’t show it here since it is so trivial.</p>
<p>This is the ideal scenario, or what I will call <strong>Level 1 Unit Testing</strong>.</p>
<p><strong>Level 1 Unit Testing</strong> is where we have a single class with no external dependencies and no state.&#160; We are just testing an algorithm.</p>
<h2>Taking it up a notch</h2>
<p>The next level of unit testing is reached if we add state to the class under test.</p>
<p><strong>Level 2 Unit Testing</strong> is where we have a single class with no external dependencies but it does have state.&#160; We are setting up an object and testing it as a whole.</p>
<p>If we take our existing example, and now we want to add a new method called <em>GetHistory, </em>it is still not difficult to implement the tests, but it gets harder, because we have to make sure we are setting up some state for our object as part of the test.</p>
<p>Let’s look at one of the test cases we might implement for this functionality:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:80309f0a-86ba-4b8a-a695-8124833de562" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp;">
[Test]
public void When3AddOperationsThenGetHistory_ShouldReturnThose3Results()
{
   var calculator = new Calculator();

   // Arrange   
   calculator.Add(1, 3);
   calculator.Add(2, 5);
   calculator.Add(3, 6);

   // Act
   var result = calculator.GetHistory();

   // Assert
   Assert.Equal(4, result[0]);
   Assert.Equal(7, resut[1]);
   Assert.Equal(9, result[2]);
   Assert.Equal(3, result.Count);
}
</pre>
</pre>
</div>
<p>&#160;</p>
<p>Again, not too hard.&#160; But, state does make this a bit more difficult.&#160; Here the value of a Behavior Driven Development (BDD) style of unit testing can be seen as it helps us to clearly divide the test up into the different parts we now have.</p>
<p>The real complexity we have added here is that we now have to deal with a setup step before we can execute our test.&#160; BDD deals with this by having a special step for defining the context in which the actual test takes place.&#160; It is called a few different things in different BDD circles, but let’s stick with AAA for this post, since it is easy to remember.</p>
<p>The major difference between<strong> Level 1 Unit Testing</strong> and <strong>Level 2 Unit Testing</strong> is that in Level 1, we were really testing only one method.&#160; In Level 2 we are testing at the class level.&#160; Really we could call <strong>Level 1 Unit Testing</strong> method testing, since the unit we are testing is the method.&#160; The class that method existed in didn’t matter.</p>
<h2>Enter dependencies</h2>
<p>Let’s see what happens when we throw dependencies into the <em>Calculator</em> class.</p>
<p>Imagine that our <em>Calculator</em> class has to keep an audit trail of our calculations.&#160; We have a service that we can use to put calculations from the <em>Add</em> method into a storage location, like a database and our <em>GetHistory</em> method can query the storage location for the history.</p>
<p>As I was thinking about this, an important point occurred to me.&#160; Were this an integration test, our example test method above wouldn’t change at all.</p>
<p>But, as it turns out we are talking about unit tests here, we need to isolate the testing down to the class level.</p>
<p>So let’s think about what our test should do now.&#160; Here are some possible tests we might have.</p>
<ul>
<li>When I add two number the result is returned and the <em>Store</em> method is called on the <em>StorageService</em> with that result. </li>
<li>When I get the history, the <em>RetrieveHistory</em> method is called on the <em>StorageService </em>and it’s results are returned back. </li>
</ul>
<p>Let’s see what one of these tests might look like:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:fde6810a-6429-43fe-895e-ef0728aa9cea" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp;">
[Test]
public void WhenAdding2NumbersAndServiceOnline_SumIsReturnedAndStored()
{
   // Arrange
   IStorageService storageServiceMock = Mocker.Mock&lt;IStorageService&gt;();
   storageServiceMock.Stub(service =&gt; service.IsServiceOnline())
          .Return(true);

    var calculator = new Calculator(storageServiceMock);   

   // Act
   var result = calculator.Add(3, 4);

   // Assert
   storageServiceMock.AssertWasCalled(service =&gt; service.Store(7);
   Assert.Equals(7, result);
}
</pre>
</pre>
</div>
<p>I call this <strong>Level 3 Unit Testing.</strong></p>
<p><strong>Level 3 Unit Testing </strong>is when we have a single class with at least one external dependency, but it does not depend on its own internal state.</p>
<p>Things really start to get complicated here, because we have to start thinking not just about inputs and outputs and sequences, but now have to think about interactions.</p>
<p>It really starts to get blurry here about what the expectations of our unit tests should be.&#160; In the example code above, do we need to check to make sure <em>IsServiceOnline</em> is called on the <em>StorageService </em>or do we only check that <em>Store</em> was called?</p>
<p>You’ll also notice here that we had to use a mock and pass our dependency into our class so that we could change its behavior.&#160; Along with that came the burden of creating an interface, so that we could have a mock implementation.</p>
<p>If you’re paying attention right now, you may be thinking to yourself that the example is bad.&#160; You may be thinking that the <em>Calculator</em> class now has two responsibilities.</p>
<ul>
<li>It calculates things and return the result </li>
<li>It stores calculation results </li>
</ul>
<p>Right you are, but we can’t wish away this problem.&#160; Let’s suppose we refactor and move the StorageService dependency out of the <em>Calculator</em> class.&#160; We have several options.&#160; We could make a decorator and use it like this:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:b5bac265-9e13-4b85-b93b-e2c7cd42ed3f" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp;">
var calculator = new Calculator();
var StoringCalculator = new StoringCalculator(calculator);
</pre>
</pre>
</div>
<p>&#160;</p>
<p>Or we could do something more like a mediator pattern like so:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:f4ab745d-91cf-4bbe-a1de-f0dc1fb88cb5" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp;">
var calcMediator = new CalculatorMediator(calculator, storageService);
</pre>
</pre>
</div>
<p>However we attempt to solve this problem, we are still going to have to have some class that will have to have a mock in its unit test.&#160; </p>
<p>There is a simple fact that we cannot get around. <strong>If we are going to use the <em>StorageService </em>to store calculations, either <em>Calculator</em> will depend on it, or something else will depend on calculator and it.&#160; </strong>There is no alternative to those two options.</p>
<p>There is another simple fact we can’t get around also.&#160; <strong>If we are going to depend on another class in our unit test, we either need an interface that we can use for the mock class, or we need a mocking framework that will support mocking concrete classes.</strong></p>
<p>So with <strong>Level 3 Unit Testing </strong>we are stuck with needing to mock at least one dependency and either creating a bogus interface, or using a mocking library that will let us mock concrete classes.</p>
<h2></h2>
<h2><strong>It gets worse</strong></h2>
<p>It only gets worse from here.&#160; At Level 3 we didn’t worry about state inside our calculator class, we worried about an external dependency that pretty much handled state for us.&#160; In many cases though we will have to worry about state and dependencies.</p>
<p><strong>Level 4 Unit Testing</strong> is when we have a single class with at least one external dependency and depends on its own internal state.</p>
<p>In our calculator example, we can simply add the requirement that we only want to get the history for a particular session of calculations.&#160; We need to keep track of the calculations so that we can ask the <em>StorageService</em> for the history for our session.</p>
<p>Let’s look at a possible test for that scenario:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:ea71e8d8-ccff-4b83-af1b-21f20fc59421" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp; pad-line-numbers: true;">
[Test]
public void When3AddsThenGetHistory_ShouldReturnOnlyThose3Results()
{
   // Arrange   
   IStorageService storageServiceMock = Mocker.Mock&lt;IStorageService&gt;();
   storageServiceMock.Stub(service =&gt; service.IsServiceOnline())
          .Return(true);
   storageServiceMock.Stub(service =&gt; service.GetHistorySession(1))
          .Return(new List&lt;int&gt;{4, 7, 9});
   var calculator = new Calculator(storageServiceMock);


   calculator.Add(1, 3);
   calculator.Add(2, 5);
   calculator.Add(3, 6);

   // Act
   var result = calculator.GetHistory();

   // Assert
   storageServiceMock.AssertWasCalled(service =&gt; 
              service.GetHistorySession(1);
   Assert.Equal(4, result[0]);
   Assert.Equal(7, resut[1]);
   Assert.Equal(9, result[2]);
   Assert.Equal(3, result.Count);
}
</pre>
</pre>
</div>
<p>&#160;</p>
<p>Consider for a moment how fragile and complex this unit testing code is.&#160; Consider how simple the functionality of our class is.</p>
<p>We have a major problem here.&#160; Our unit testing code is more complex than the code it is testing!&#160; It’s ok, if the unit testing code is more lines of code than the code it is testing, that is usually the case.&#160; But, I consider it a big problem when our unit testing code is more complex, because you have to ask yourself the very real question.</p>
<p>Where is there more likely to be a bug?</p>
<h2>I’m not saying anything yet</h2>
<p>My point is not to make a point, at least not yet.&#160; My real goal here is to help us to change the way we think about unit testing.</p>
<p>We need to stop asking the general question of whether not unit testing is worth the cost and instead ask the more specific question of what level of unit testing is worth the cost.</p>
<p>Level 3+ has a very steep cost as mocking is unavoidable and adds considerable complexity to even the most trivial of implementations.</p>
<p>From that we can draw a bit of wisdom.&#160; If we are going to unit test we should strive to encapsulate as much of our pure logic into classes without dependencies and if possible without state.</p>
<p>The other thing to consider is that as the difficulty and complexity of the unit tests are increasing each level, the goal of the test and value starts to become lost also.</p>
<p>What I mean by this, is that when we start testing that our class properly calls another class with certain parameters, we are crossing over into testing the implementation details of the class.</p>
<p>If I say a class should be able to add 2 numbers and return the result.&#160; I am not talking about how it has to do it.&#160; As long as the result is correct, how doesn’t matter.</p>
<p>When I add a mock and say a class needs to add 2 numbers and store the result using a <em>StorageService</em> by calling the method <em>Store</em> on it, I have now tied how into the test.&#160; Changing how breaks the test.</p>
<p>That’s all we are going to look at for now.&#160; If you’ve read some of my <a href="http://simpleprogrammer.com/back-to-basics-series/">other back to basics posts</a>, you can see the progression up to this point.&#160; I’ve been discounting using interfaces and dependency injection for the sake of unit testing, but I have yet to offer an alternative.&#160; I still don’t.&#160; To be honest, I don’t have one yet.&#160; But, I do believe by breaking down this problem to its roots we can evaluate what we are doing and determine what our true problems are.</p>
<p>By the end of this series I hope to have a solution and a recommendation for tackling these kinds of problems.</p>
<h6><strong>As always, you can subscribe to this </strong><a href="http://feeds.feedburner.com/MakingTheComplexSimple"><strong>RSS feed</strong></a><strong> to follow my posts on Making the Complex Simple.&#160; Feel free to check out </strong><a href="http://elegantcode.com/"><strong>ElegantCode.com</strong></a><strong> where I post about the topic of writing elegant code about once a week.&#160; Also, you can follow me on twitter </strong><a href="http://twitter.com/jsonmez"><strong>here</strong></a><strong>.</strong></h6>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/complextosimple.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/complextosimple.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/complextosimple.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/complextosimple.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/complextosimple.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/complextosimple.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/complextosimple.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/complextosimple.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/complextosimple.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/complextosimple.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/complextosimple.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/complextosimple.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/complextosimple.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/complextosimple.wordpress.com/1195/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=simpleprogrammer.com&amp;blog=10597120&amp;post=1195&amp;subd=complextosimple&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://simpleprogrammer.com/2010/12/12/back-to-basics-why-unit-testing-is-hard/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/695e2a956b2dcb5ac45a7095b6ee338a?s=96&#38;d=retro&#38;r=PG" medium="image">
			<media:title type="html">jsonmez</media:title>
		</media:content>

		<media:content url="http://complextosimple.files.wordpress.com/2010/12/ar119675077156567_thumb.jpg" medium="image">
			<media:title type="html">ar119675077156567</media:title>
		</media:content>
	</item>
	</channel>
</rss>
