<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Game Dev Without a Cause &#187; TDD</title>
	<atom:link href="https://gamedevwithoutacause.com/?cat=15&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>https://gamedevwithoutacause.com</link>
	<description>Rob&#039;s Thoughts on Games, Game Dev, and Design - Temporary home of Skyboy Games</description>
	<lastBuildDate>Wed, 23 Jul 2014 09:18:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>TDD Tips for XNA: Isolating XNA</title>
		<link>https://gamedevwithoutacause.com/?p=1218</link>
		<comments>https://gamedevwithoutacause.com/?p=1218#comments</comments>
		<pubDate>Tue, 17 Jul 2012 14:05:33 +0000</pubDate>
		<dc:creator><![CDATA[Rob]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://gamedevwithoutacause.com/?p=1218</guid>
		<description><![CDATA[TDD (Test-Driven Development) can be great. It encourages you to address edge cases earlier when you have a better grasp of the code you just implemented. It forces you to de-couple classes in ways that allow you to later combine &#8230; <a href="https://gamedevwithoutacause.com/?p=1218">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><a href="http://gamedevwithoutacause.com/wp-content/uploads/2012/07/Isolation.png"><img src="http://gamedevwithoutacause.com/wp-content/uploads/2012/07/Isolation.png" alt="" title="Isolation" width="650" height="520" class="aligncenter size-full wp-image-1239" /></a><br />
TDD (Test-Driven Development) can be great.  It encourages you to address edge cases earlier when you have a better grasp of the code you just implemented.  It forces you to de-couple classes in ways that allow you to later combine and re-use them in ways that might not have been possible had you developed them using other methodologies.</p>
<p>TDD can also be a big pain-in-the-ass.  Isolating logic for testing can be difficult, especially when you need to rely on code that requires complicated external resources: legacy codebases and hardware being the prime examples.  The XNA Framework code, with its substantial functionality abstracting the details of hardware on Windows, the Xbox 360, and Windows Phones, falls squarely into both of those categories.</p>
<p>So, does this mean that it&#8217;s impossible to use TDD when working with XNA?  Hardly.  It can be tough, but there are ways to make it easier.  In this post, I&#8217;ll talk about how I&#8217;ve managed to work with XNA while still giving my code the TDD-loving it deserves / needs.<br />
<span id="more-1218"></span></p>
<p>A lot of the power of the XNA Framework comes from several large, core classes: Game, ContentManager, and GraphicsDevice being the major triumvirate.  These classes provide the core update, file management and rendering facilities you need to run a game using XNA.  They are also fairly intertwined and take a bit of work to instantiate individually.  Even if you can manage to include them in your test framework, their instantiation can be quite resource-intensive.  This resource-intensiveness can either make your tests unsuitable for rapid iteration (if you instantiate XNA for each test) or harder to keep independent of each other (if you keep XNA instantiated across all tests, thereby allowing the possibility of tests polluting each other with shared state.)</p>
<p>Of course, the real kicker here is that you don&#8217;t even need XNA for a lot of your tests.  The XNA Framework itself is relatively mature and tested and you couldn&#8217;t fix an XNA bug even if you found one because you won&#8217;t have source access.  The vast majority of code you would want to develop with TDD will have nothing to do with the large XNA classes.  So, my big piece of advice for using TDD with XNA is: don&#8217;t apply TDD to XNA!</p>
<p>Not touching XNA using TDD is a pragmatic solution although it may not be the methodologically-correct according to the usual tenets of TDD. Theoretically, you could create a mock object to emulate all the functionality of XNA&#8217;s GraphicsDevice.  However, that would involve a LOT of effort and probably have a lot of bugs in and of itself.  For the vast (VAST!) majority of cases, you can entirely ignore GraphicsDevice&#8217;s existence and still get the benefits of TDD, even for code related to graphics.  In most cases, what you need to test is not the behavior of GraphicsDevice itself, but the data and commands you send to GraphicsDevice.</p>
<p>What this means is that you can apply TDD to code that needs access to complicated XNA classes, like GraphicsDevice and ContentManager, by designing your code to function (in a minimal fashion) given a null instance of the XNA class they require.  You&#8217;d be surprised at the amount of code you can test even if the class that does all the &#8220;real work&#8221; is null.</p>
<p>An example from my own code is my TextureLibrary class.  It uses ContentManager to load Texture2Ds given a suitable path, but that is only a small (although crucial) part of what TextureLibrary does.  Its main mission is to organize collections of Texture2Ds and Rectangles representing UV coordinates for easy access by the rest of the game.  The real trick to implementing this class using TDD was realizing that the part of it&#8217;s functionality that actually used ContentManager (the loading of textures) was small and trivially simple to implement.  By designing TextureLibrary to function (fully except for texture loading proper) with a null ContentManager passed to it, I could concentrate on implementing and testing all the functionality related to adding and removing texture library entries as well as dealing with edge-cases like repeated adds of the same data, etc.  When it came to actually using TextureLibrary in a game, I just had to pass in a valid ContentManager and implement the (trivial) code for loading Texture2Ds.  Then TextureLibrary just worked there, pretty much exactly as expected.</p>
<p>So there it is, my big tip for how to use TDD with XNA: don&#8217;t use TDD with XNA.  Or rather, don&#8217;t bother trying to get big, complicated XNA classes to work with TDD.  You won&#8217;t get that much benefit from doing the &#8220;right&#8221; thing and using mock objects because most of the code you&#8217;ll write won&#8217;t actually be all that dependent on those big XNA classes.  The way I see it, it&#8217;s better to test 80% of your code and release a game then to test 100% of your code but never finish a project.</p>
]]></content:encoded>
			<wfw:commentRss>https://gamedevwithoutacause.com/?feed=rss2&#038;p=1218</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Pages from my TDD Diary</title>
		<link>https://gamedevwithoutacause.com/?p=1212</link>
		<comments>https://gamedevwithoutacause.com/?p=1212#comments</comments>
		<pubDate>Tue, 10 Jul 2012 14:20:07 +0000</pubDate>
		<dc:creator><![CDATA[Rob]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://gamedevwithoutacause.com/?p=1212</guid>
		<description><![CDATA[TDD (Test-Driven Development) can be a hard-sell in many work environments. I said as-much in a previous post. The main problem lies in figuring out how to calculate the concrete time costs of TDD versus the abstract (but real) benefits. &#8230; <a href="https://gamedevwithoutacause.com/?p=1212">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><a href="http://gamedevwithoutacause.com/wp-content/uploads/2012/07/pages.jpg"><img src="http://gamedevwithoutacause.com/wp-content/uploads/2012/07/pages.jpg" alt="" title="pages" width="360" height="270" class="aligncenter size-full wp-image-1214" /></a><br />
TDD (Test-Driven Development) can be a hard-sell in many work environments.  I said as-much in a <a href="http://gamedevwithoutacause.com/?p=987" title="Getting TDD in my XNA with NUnit">previous post</a>.  The main problem lies in figuring out how to calculate the concrete time costs of TDD versus the abstract (but real) benefits.</p>
<p>While it&#8217;s easy to track the costs of TDD with a timesheet recording the amount of time spent writing tests versus production code, measuring the benefits in a measurable way takes a bit more creativity.  My approach to measuring the benefits of TDD in my own code is to keep a journal of moments of when I think I have reaped benefits due to TDD.  This is, of course, a very anecdotal approach to measuring the benefits, but it does provide a basis for tracking effects of TDD that would normally go unmeasured.<br />
<span id="more-1212"></span></p>
<p>To give an idea of how this documentation goes, I&#8217;ll present a few entries from my TDD diary, a log of events in which I felt the benefits of having produced my code using TDD:  </p>
<blockquote><p>
6/18/2012 Implementing Preallocate on Pool<><br />
   Created trivial test of item counts, expected it to pass<br />
   Implemented Preallocate but it didn&#8217;t pass<br />
   I had made a dumb error by calling the Item allocator directly instead of through the get item function<br />
   Caught my dumb mistake, yay!</p>
<p>6/18/2012 Implemented updating list of actors, but found problem when updating actors added other updating actors (like bullets)<br />
   Thought: &#8220;Great, I probablly need to change the interface for my updating actors list&#8221;<br />
   Had test for 3 cases related to said list<br />
      &#8212; Insert updateable increases count<br />
      &#8212; Inserted updateable updated on list update<br />
      &#8212; Changing updateability of object in list moves it to update list<br />
   With the tests in place, I found that I was able to implement temporary holding lists without changing my interface at all</p>
<p>6/24/2012 I think I can attribute this to TDD: using TDD, my actor update logic ended up being decoupled in its own class instead of being just part of a larger &#8220;engine update&#8221;<br />
   This decoupling was motivated by testing ease, but it made implementing a pause system for my game trivially easy because I could instantiate an actor updater just for<br />
   in-game (pause-affected) actors while allowing other actors to be unaffected by pause.  Had I not been using TDD, the oversight of not providing a convenient way to tick certain<br />
   groups of actors might have meant a lot of re-work.</p>
<p>6/29/2012 I found out that my scenes wouldn&#8217;t wait for the previous scene finish transitioning out before activating.  I found the bug and fixed it, but had a slight logic error.<br />
   My test of standard scene functionality caught it.  This way, I fixed it before even having to run the code in game to test.
</p></blockquote>
<p>Given events of TDD benefit like the ones, I can estimate the the amount of that would have been lost had I not been using TDD.  This gives me a number that I can compare against the actual time I spend writing tests and help me figure out how well TDD is working out for me.</p>
]]></content:encoded>
			<wfw:commentRss>https://gamedevwithoutacause.com/?feed=rss2&#038;p=1212</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting TDD in my XNA with NUnit</title>
		<link>https://gamedevwithoutacause.com/?p=987</link>
		<comments>https://gamedevwithoutacause.com/?p=987#comments</comments>
		<pubDate>Tue, 08 May 2012 13:37:44 +0000</pubDate>
		<dc:creator><![CDATA[Rob]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://gamedevwithoutacause.com/?p=987</guid>
		<description><![CDATA[I&#8217;ve had an infatuation with test-driven development (TDD) for years but I&#8217;ve never had a chance to use it professionally to any major extent. While the potential benefits of TDD, like reduced bug regression and greater code decoupling, are substantial, &#8230; <a href="https://gamedevwithoutacause.com/?p=987">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><a href="http://gamedevwithoutacause.com/wp-content/uploads/2012/04/tdd.png"><img src="http://gamedevwithoutacause.com/wp-content/uploads/2012/04/tdd.png" alt="" title="tdd" class="aligncenter size-full wp-image-1013" /></a><br />
I&#8217;ve had an infatuation with <a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank">test-driven development</a> (TDD) for years but I&#8217;ve never had a chance to use it professionally to any major extent.  While the potential benefits of TDD, like reduced bug regression and greater code decoupling, are substantial, the considerable cost involved in learning how to write effective tests and testable code make it a hard-sell to introduce in many work environments.  </p>
<p>I can sympathize with a manager who is cautious about adopting TDD, compared to the very palpable up-front costs, the benefits tend to be future-oriented and hard-to-measure.  Luckily, I don&#8217;t have to worry about convincing managers to let me try TDD for my personal projects. This gives me a chance to give TDD a test-drive (bad pun, I know) and see just how it affects the code I write.  Being that I&#8217;ve been working a lot with XNA lately, I&#8217;ll demonstrate a method to integrate the <a href="http://www.nunit.org/" target="_blank">NUnit</a> unit-testing framework with Visual Studio 2010 Express in order to practice TDD while using XNA.<br />
<span id="more-987"></span></p>
<p>So, a super brief primer on test-driven development.  The main feedback loop in TDD runs roughly as follows:</p>
<ol>
<li>Write a test</li>
<li>Run your test suite and (presumably) see your new test fail</li>
<li>Write code that will satisfy the test</li>
<li>Run your test suite again and (hopefully) see your new test succeed</li>
<li>Refactor code as needed</li>
<li>Run your test suite to confirm that refactoring hasn&#8217;t changed any tested behavior</li>
<li>Brag to a co-worker about doing TDD</li>
<li>Repeat</li>
</ol>
<p>Given the above workflow, it becomes clear that I should pretty much run tests whenever I change code and build.  In that case, it sounds like I should set up my test runner as a post-build step in my Visual Studio solution.</p>
<p>In order to integrate NUnit with Visual Studio, I first <a href="http://nunit.org/?p=download" target="_blank">download</a> and install NUnit.  Then, I create a solution for my new game that looks something like this:</p>
<ul>
<li>MyGame Solution
<ul>
<li>MyGame (an XNA Game project)</li>
<li>MyGameLib (an XNA Game Library project)</li>
<li>MyGameLibTests (an empty C# project)</li>
</ul>
</li>
</ul>
<p>I then right-click the projects and tell MyGame and MyGameLibTests to both reference MyGameLib. I will be implementing the majority of my games code in MyGameLib as opposed to MyGame because I want to be able to reference the code in both my test project as well as in the executable itself.  Ideally, the MyGame project will contain very little code beyond what is needed to instantiate the code written in MyGameLib and run it as a game.</p>
<p>To actually run the test suite, I open up the property settings for MyGameLibTests and modify the post-build task to run the following:</p>
<pre class="brush: diff;">

&quot;C:\Program Files (x86)\NUnit 2.6\bin\nunit-console-x86.exe&quot; /nologo $(TargetFileName)
</pre>
<p>This will cause Visual Studio to run the command-line version of NUnit and pipe the output into the Build window.  If NUnit detects a test failure, it will spit out an error code which will cause Visual Studio to consider the build a failure which will stop me in my tracks until I fix the code so that the test passes (which is what I want.)  Because I&#8217;m running a 64-bit OS, I need to explicitly tell NUnit to use the 32-bit version of nunit-console since XNA is compiled as 32-bit, hence the x86 in the name of the NUnit executable.</p>
<p>I can run my test suite now but, without tests, that doesn&#8217;t mean a whole lot.  So, in order to add a little meaning to this whole process, I add a class to MyGameLibTests that looks like ths:</p>
<pre class="brush: c++">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace MyGameLibTests
{
    [TestFixture]
    public class SomeObjectTests
    {
        [SetUp]
        public void Init()
        {
        }

        [TearDown]
        public void Cleanup()
        {
        }

        [Test]
        public void SomeTest()
        {
        }
    }
}
</pre>
<p>This is pretty much standard NUnit boilerplate code.  The [SetUp] and [TearDown] tags mark functions that will be run before and after each test.  The [Test] tag marks a test itself.  If I go ahead and toss in a test guaranteed to fail like this one:</p>
<pre class="brush: c++">
[Test]
public void SomeTest()
{
    Assert.IsTrue(false);
}
</pre>
<p>I can build my solution and confirm that the test suite runs and reports a failure.  Great!  Also, since I will be adding a lot of test files in the future, I went ahead and made a Visual Studio item template (which you can <a href='http://gamedevwithoutacause.com/wp-content/uploads/2012/04/NUnitTests.zip'>download here</a>) for creating the NUnit boilerplate.  I want there to be as little friction as possible between me and writing new tests.</p>
<p>With my test framework in place, I can start writing real failing tests and then the code that makes them succeed.  By tying automated testing to my build process, I guarantee that I will run tests whenever I change code.  This should help keep me honest by forcing me to fix errors as soon as they crop up.</p>
<p>Now, if you&#8217;ll pardon me, I have some code (and tests) to write.</p>
]]></content:encoded>
			<wfw:commentRss>https://gamedevwithoutacause.com/?feed=rss2&#038;p=987</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
