<?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>Sarah Taraporewalla's Technical Ramblings &#187; Testing</title>
	<atom:link href="http://sarahtaraporewalla.com/thoughts/category/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://sarahtaraporewalla.com/thoughts</link>
	<description></description>
	<lastBuildDate>Sun, 31 Jan 2010 22:03:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Canned, Stubbed and Mocked Fake Objects</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/canned-stubbed-and-mocked-fake-objects/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/canned-stubbed-and-mocked-fake-objects/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 22:16:42 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtaraporewalla.com/thoughts/?p=209</guid>
		<description><![CDATA[I had a very interesting discussion with Jen Smith today about the differences in approach to faking objects during testing. As a result, I am finally putting down my thoughts on the matter.
Canned Objects
Canned objects are fantastic to use in situations where you want an object to respond with a predefined value or you don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I had a very interesting discussion with <a href="http://twitter.com/JenniferSmithCo">Jen Smith</a> today about the differences in approach to faking objects during testing. As a result, I am finally putting down my thoughts on the matter.</p>
<h3>Canned Objects</h3>
<p>Canned objects are fantastic to use in situations where you want an object to respond with a predefined value or you don&#8217;t care what the response is as it doesn&#8217;t directly effect your test. It responds with the same value no matter how many times I call it. I use these objects most extensibly in my testing, especially for repositories. I only have one canned fake object per real object, except when I have services which, eg do validation; in this case, I create NegativeValidationService and PositiveValidationService objects to help readability of tests.</p>
<pre class="brush: csharp;">
public class CannedBookRepository : IBookRepository
{
    private IBook book;
    public CannedBookRepository(IBook book)
    {
        this.book = book;
    }
    public CannedBookRepository() : this(new BookBuilder()) {}

    public IBook Find(IKey bookKey)
    {
        return this.book;
    }
    public void Save(IBook book) {}
}
</pre>
<h3>Stubbed objects</h3>
<p>Stubbed objects are fake representations of their real counterparts, and as such will tend to emulate their behaviour. The main difference, however, is that they do not talk to expensive services, eg database connections. I use stubbed objects mainly when I need my fake object to reply in different ways for different inputs, or when testing that I can put an object into a repository and will access an identical version of it. Again, generally I have at most one per real object; I usually don&#8217;t use them.</p>
<pre class="brush: csharp;">
public class StubbedBookRepository : IBookRepository
{
    private IList books = new List;

    public IBook Find(IKey bookKey)
    {
        return this.books.Find(book =&gt; book.Key.Equals(bookKey));
    }
    public void Save(IBook book)
    {
        this.books.Add(book);
    }
}
</pre>
<h3>Mocked Objects</h3>
<p>Mocked objects make use of existing mocking libraries, such as Mockito, Rhino mocks or mocha and are used mainly to test either that the correct method has been used for an API or when testing legacy code (ie when they have used static methods/classes and I cannot change that &#8211; TypeMock can do weird stuff with statics).</p>
<h3>Why</h3>
<p>The main reasons why I prefer these fake objects, as opposed to using a mocking library to stub certain methods are:</p>
<ul>
<li>it is less lines of code in the test (only by declaration) &#8211; there are no setup, stub, replay/verify calls. All setup is done in the constructor</li>
<li>it is more maintainable &#8211; resharper can refactor it easily. Also if the methods are being refactored, eg changing a bool return type to an enum there is a single point of failure</li>
<li>it is easier to read (imho)</li>
<li>I don&#8217;t have to worry about different syntax when creating real and fake objects &#8211; they are created in exactly the same manner. This also helps testing, as usually they are not the main concern of the test</li>
<li>once created, they are always there to be used</li>
<li>they help drive out design &#8211; if I find I have a fake object with many methods which throw the NotImplementedException, I can begin to ask &#8220;Are all these behaviours on the correct object?&#8221;</li>
</ul>
<p>To summarize: canned fake objects are cool.</p>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/canned-stubbed-and-mocked-fake-objects/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How does Architecture fit with TDD</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/how-does-architecture-fit-with-tdd/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/how-does-architecture-fit-with-tdd/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 21:12:25 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtaraporewalla.com/thoughts/?p=121</guid>
		<description><![CDATA[The question a lot of people (especially software architects) ask when adopting TDD is how does Architecture fit in the whole Test Driven Design paradigm.
Traditionally, any new functionality is vetted by a software architect who completely designs it (on paper) and sometimes adds the foundation to the code base. The developers in the team then [...]]]></description>
			<content:encoded><![CDATA[<p>The question a lot of people (especially software architects) ask when adopting TDD is how does Architecture fit in the whole Test Driven Design paradigm.</p>
<p>Traditionally, any new functionality is vetted by a software architect who completely designs it (on paper) and sometimes adds the foundation to the code base. The developers in the team then take the designs and implement them, sometimes in a cookie-cutter fashion. The developers often don&#8217;t understand the design (at least why it is in the form that the architect has specified) and when they start to query it get negative reaction from the architect.</p>
<p>The question is, if we now say our tests will drive out design, where does the architecture go? Won&#8217;t this just lead to a micro-optimisation, without anyone looking at the system as a whole. Chaos!</p>
<p>In my mind, the role the architect plays in this brave new world is to provide the team with a 50000-ft view of the system. The architecture declares that MVC will be used, how the infrastructure is strung together, but it does not specify which classes will be created, what services will be used and what design patterns will be used. These are really implementation details, and are the elements which are driven by your tests.</p>
<p>Other XP and agile principles will also help with ensuring your newly TDD code base does not turn into chaos. If you adopt a domain driven design approach, then your business layer and logic will be driven out by the business and you will know from that which classes need to be created, what should be a service and what are your repositories. If you adopt pair programming, then the &#8216;architects&#8217; experience can be shared with other team members and the knowledge of what constitutes good design increases, lessening the role of the architect. </p>
<p>So, how does Architecture fit into TDD? Quite nicely, I think.</p>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/how-does-architecture-fit-with-tdd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A conversation with a TDDer</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/a-conversation-with-a-tdder/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/a-conversation-with-a-tdder/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 10:20:02 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtaraporewalla.com/thoughts/?p=111</guid>
		<description><![CDATA[This is part of a conversation I had this week with my pair (in the conversation, I am A and he is B) as we were working on a new story. He, like many others, has been trying to do TDD but it wasn&#8217;t until we had this conversation that he began to understand what [...]]]></description>
			<content:encoded><![CDATA[<p>This is part of a conversation I had this week with my pair (in the conversation, I am <em>A</em> and he is <em>B</em>) as we were working on a new story. He, like many others, has been trying to do TDD but it wasn&#8217;t until we had this conversation that he began to understand what I was talking about in my last post &#8211; that Test First does not drive out good design, but Test Driven Development can. </p>
<p>A: <em>What</em> are we trying to test</p>
<p>B: The instrument controller</p>
<p>A: Ok, <em>what</em> is it meant to do</p>
<p>B: Well, we are going to send it an instrument and its going to go to the Songs table in the database and query it for songs which use that instrument and then its going to render them on the page</p>
<p>A: Hmm &#8211; that sounds like <span style="text-decoration: underline;">how</span>. <em>What</em> is it meant to do?</p>
<p>B: Umm &#8211; find the songs that use the instrument and display it on the page.</p>
<p>A: That sounds pretty good. Except is it <em>responsible</em> for displaying it on the page or is that the <em>responsibility</em> of the view object?</p>
<p>B: Yeah &#8211; the view object actually renders it.</p>
<p>A: Ok &#8211; perhaps we can say that the instrument controller is <em>responsible</em> <em>for</em> finding the songs that use the instrument and returning it in a form that the view object can use.</p>
<p>B: Sounds good.</p>
<p>A: Ok, so what&#8217;s our first test</p>
<p>B: Well, we give it an instrument and it should find all the songs and return them.</p>
<p>A: That seems to be a lot for our first test. Perhaps we should <em>split</em><em> that into two tests</em> and have one which returns a list of songs and one that asserts the songs are correct.</p>
<p>B: Cool, sounds good</p>
<p>A: Ok, so the first <em>test name</em> could be ShouldReturnListOfSongs and the second test could be ShouldReturnSongsWhichUseTheGivenInstrument. So, we want to assert that we get a list of songs. It is often useful to work bottom-up: work from the assertion up to the setup.</p>
<pre class="brush: csharp;">
public void ShouldReturnListOfSongs()
{
	// Given

	// When

	// Then
	Assert.IsTrue(returnedList is List&lt;Song&gt;);
}
public void ShouldReturnSongsWhichUseTheGivenInstrument()
{
	Assert.Fail()
}
</pre>
<p>B: But where does returnedList come from</p>
<pre class="brush: csharp;">
public void ShouldReturnListOfSongs()
{
	// Given

	// When
	InstrumentController controller = new InstrumentController();
	List&lt;Song&gt; returnedList = controller.FindSongs();

	// Then
	Assert.IsNotNull(returnedList);
}
public void ShouldReturnSongsWhichUseTheGivenInstrument()
{
	Assert.Fail()
}
</pre>
<p>A: Ok &#8211; your turn. Lets do the simplest thing to make that pass</p>
<pre class="brush: csharp;">
public class InstrumentController
{
	public List&lt;Song&gt; FindSongs()
	{
		return new List&lt;Song&gt;();
	}
}
</pre>
<p>B: Cool. Ok, now I want to test that it finds the right songs from the database</p>
<p>A: Testing that we get the correct songs is good, but its not the responsibility of the controller to know that it is getting the songs from a database. What happens if you want to change your storage mechanism? Now you have to change everywhere that you just pulled information straight from it. Anyway, thats a how again. Lets get back to what it should do: return the songs which use the instrument.</p>
<pre class="brush: csharp;">
public void ShouldReturnListOfSongs() { ... }
public void ShouldReturnSongsWhichUseTheGivenInstrument()
{
	// Given
	Song hereComeTheDrums = new SongBuilder().Build();

	// When
	InstrumentController controller = new InstrumentController();
	List&lt;Song&gt; returnedList = controller.FindSongs(&quot;drums&quot;);

	// Then
	List&lt;Song&gt; expectedSongs = Lists.Build(hereComesTheDrums);
	Assert.AreEqual(expectedSongs, returnedList);
}
</pre>
<p>A: Notice I have used a builder &#8211; this is so that our setup is not long, we know we have a valid Song object and if at anytime we need to change the way a song was constructed, we would only need to change the builder &#8211; not every test which created a Song. Now, we know that the controller needs to ask another object to do the finding, as it is not the controllers responsibility. An objet that usually encapsulates that work is called a Repository. As far as the controller is concerned, the Repository holds a bag full of Songs and it can ask it for songs that use instruments. It is the responsibility of the repository to actually find the songs. So, lets give the controller the repository when it is created.</p>
<pre class="brush: csharp;">
public void ShouldReturnListOfSongs() { ... }
public void ShouldReturnSongsWhichUseTheGivenInstrument()
{
	// Given
	Song hereComeTheDrums = new SongBuilder().Build();
	ISongRepository songLibrary = new CannedSongRepository(hereComeTheDrums);

	// When
	InstrumentController controller = new InstrumentController(songLibrary);
	List&lt;Song&gt; returnedList = controller.FindSongs(&quot;drums&quot;);

	// Then
	List&lt;Song&gt; expectedSongs = Lists.Build(hereComeTheDrums);
	Assert.AreEqual(expectedSongs, returnedList);
}
</pre>
<p>A: Again, note that the controller takes in an interface and in the test we are using a Canned repository. A canned object is useful in testing as we can guarantee what it returns. Some people use mocking frameworks to stub the method call; the difference between a canned repository and a mock is that we can reuse the canned object without any effort in other tests, but we would need to recreate the mock every time. That leaves us with a failing test.</p>
<pre class="brush: csharp;">
public class InstrumentController
{
	ISongRepository songLibrary;

	public InstrumentController(ISongRepository songs)
	{
		songLibrary = songs;
	}
	public List&lt;Song&gt; FindSongs(string instrument)
	{
		return songLibrary.Find(instrument);
	}
}
</pre>
<p>A: Right, now the only problem with the code is that I don&#8217;t really understand what the method Find does. I can&#8217;t look at it and instinctively know what it will do. It is not an intention revealing method name. Perhaps we can call it FindSongsWhichUse so that the code now reads</p>
<pre class="brush: csharp;">
public class InstrumentController
{
	ISongRepository songLibrary;

	public InstrumentController(ISongRepository songs)
	{
		songLibrary = songs;
	}
	public List&lt;Song&gt; FindSongs(string instrument)
	{
		return songLibrary.FindSongsWhichUse(instrument);
	}
}
</pre>
<p>A: Now we have a choice &#8211; we can continue testing the controller or we can start testing the repository. If we test the repository, we can continue with a thin vertical slice, we can see the results on the page and get immediate feedback (this is my preferred method)</p>
<p>And so the development (and conversation) continued until we ended up with a nice design which was fully tested and only delivering what the business wanted.</p>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/a-conversation-with-a-tdder/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>What is a good test?</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/what-is-a-good-test/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/what-is-a-good-test/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 10:19:40 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtaraporewalla.com/thoughts/?p=110</guid>
		<description><![CDATA[I have been helping my current client introduce TDD, and in doing so explaining principles such as single-responsibility, encapsulation and intention revealing names. As I said in my previous post Test first does not magically improve the code base, but you need to concentrate on writing good tests to help you drive out good design. [...]]]></description>
			<content:encoded><![CDATA[<p>I have been helping my current client introduce TDD, and in doing so explaining principles such as single-responsibility, encapsulation and intention revealing names. As I said in my previous post Test first does not magically improve the code base, but you need to concentrate on writing good tests to help you drive out good design. This is my list of what I think characterises a good test (although I don&#8217;t think its definitive &#8211; I am sure I have missed some things out).</p>
<p> </p>
<ul>
<li>Test <em>the what, not the </em><em>how</em>. Concentrate on the behaviour of your object, not how it will achieve that behaviour.</li>
<li>Have<em> minimal, simple setup</em>. This will help drive out classes which have low dependancies, low coupling and single responsibility. Use builders to help you minimise the number of lines in your setup.</li>
<li><em>Write</em> your test<em> bottom up</em> &#8211; start with your assertion, then fill in the blanks</li>
<li>Test <em>one behaviour at a time</em> (often referred to one assertion per test). If you hear the magic keyword And when describing the behaviour, you have two tests.</li>
<li>It is useful to structure your test in the<em> Given When Then </em>format. This allows you to see the what is just there to form as setup and how much setup you need to do (in the Given), see the intention of the test (in the Then) and understand which method you should alter to make the test work (in the When).</li>
<li><em>Naming is important</em>. Give your test a good name &#8211; it will help with your documentation and it will also help you concentrate on what you are testing, not how it will be implemented. Also, names of your test variables should reveal the different examples of the values that could be used. Instead of declaring User user = new User(&#8220;mary jane&#8221;) try User maryJane  = new User(&#8220;mary jane&#8221;) and see how that changes your understanding of the class.</li>
</ul>
<p>In my next post I will share a conversation I had this week with my client, which shows how I introduce these characteristics to my test.</p>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/what-is-a-good-test/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>TDD does not mean Test First</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/tdd-does-not-mean-test-first/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/tdd-does-not-mean-test-first/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 22:11:41 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtaraporewalla.com/thoughts/?p=107</guid>
		<description><![CDATA[Those starting out on their XP or Agile journey often hear supposedly enlightening phrases touted by those in the know like &#8220;TDD will lead you to better, nicely encapsulated, decoupled code&#8221; which leaves them scratching their heads. They are scratching their heads because they understand the benefits of adopting these practices, but they can&#8217;t see [...]]]></description>
			<content:encoded><![CDATA[<p>Those starting out on their XP or Agile journey often hear supposedly enlightening phrases touted by those in the know like &#8220;TDD will lead you to better, nicely encapsulated, decoupled code&#8221; which leaves them scratching their heads. They are scratching their heads because they understand the benefits of adopting these practices, but they can&#8217;t see how to actually apply them in their situation.</p>
<p>Take TDD for example &#8211; how can doing Test Driven Development/Design lead to good code? After all, you are only writing the same unit test before you write your code, instead of after. &#8216;How will that act magically transform my code base?&#8217; The problem is that when you first hear about TDD, all you really hear is &#8216;Test First&#8217;. What you usually don&#8217;t hear is that when you actually test drive your application, you need to ensure that each test you write should be a good test, and it will look nothing like the tests you are used to writing. So, the first problem is that we don&#8217;t explain the what a good test should look like loudly enough.</p>
<p>Another problem is that you can&#8217;t expect a code base filled with statics and procedural code to magically transform by adding tests. The developers who are responsible for that code will continue to work on it, and they will find a way of writing unit tests which allows them to continue with their procedural code (although it will violate the rules of a good test). This will cause two bad side effects: tests will be hard to write, so developers will not get into the habit of test-first; and the code will still be as stinky as ever, so they won&#8217;t believe that TDD works. The other side of the coin is that the developers need to understand basic concepts and principles like single-responsibility, encapsulation and dependancy injection because without this understanding and mindset, it is really hard to write good tests, and the code base will never improve.</p>
<p>The other problem is that the developers don&#8217;t actually realise that they can&#8217;t just change how they write tests; they are actually changing how they think about code, how they write the code and how they design the code. And this can be scary, especially for the &#8216;technical architects&#8217; who are used to spending a few weeks in front of a white board designing the application before writing a single line of code. It also takes a lot of discipline and you need to unlearn nearly all software development practices and instincts that you know. </p>
<p>The simple phrase of &#8220;TDD will lead to better code&#8221; has bigger implications than just writing tests upfront. Those starting out on their XP journey need to understand the bigger ramifications before they can begin to embrace their new practices. Those in the know need to understand that others don&#8217;t realise that there are bigger ramifications and when they see the head scratching, try to explain why the phrase is so (hopefully this post will help).</p>
<p>TDD does lead to better code &#8211; test first does not.</p>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/tdd-does-not-mean-test-first/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using Builders in Tests</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/using-builders-in-tests/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/using-builders-in-tests/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 20:03:05 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtaraporewalla.com/thoughts/?p=90</guid>
		<description><![CDATA[An annoying part of writing a test is the amount of setup an object can need in order to use it within the test. 

public void ShouldCalculateVolume()
{
	Box box = new Box();
	box.Type = Cardboard;
	box.Width = 10;
	box.Height = 20;
	box.Depth = 40;
	Assert.AreEqual(8000, box.Volume)
}

However, there is a solution: builders. Builders help by allowing you to only setup the data relevant [...]]]></description>
			<content:encoded><![CDATA[<p>An annoying part of writing a test is the amount of setup an object can need in order to use it within the test. </p>
<pre class="brush: csharp;">
public void ShouldCalculateVolume()
{
	Box box = new Box();
	box.Type = Cardboard;
	box.Width = 10;
	box.Height = 20;
	box.Depth = 40;
	Assert.AreEqual(8000, box.Volume)
}
</pre>
<p>However, there is a solution: builders. Builders help by allowing you to only setup the data relevant to the test, but still having a fully formed object. By chaining your methods, you also reduce the amount of lines required for setup. Another advantage of using builders is obvious when you start to refactor the object: instead of fixing all your tests because you have added a new mandatory field, you only have one point of failure.</p>
<pre class="brush: csharp;">
public void ShouldCalculateVolume()
{
	Box box = new BoxBuilder().Width(10).Height(20).Depth(40).Build();
	Assert.AreEqual(8000, box.Volume)
}

public class BoxBuilder
{
	int width = 1;
	int height = 1;
	int depth = 1;
	Material type = Cardboard;

	public Box Build()
	{
		Box box = new Box();
		box.Type = Cardboard;
		box.Width = width;
		box.Height = height;
		box.Depth = depth;
		return box;
	}

	public BoxBuilder Width(width)
	{
		this.width = width;
		return this;
	}
	public BoxBuilder Height(height)
	{
		this.height = height;
		return this;
	}
	public BoxBuilder Depth(depth)
	{
		this.depth = depth;
		return this;
	}
}
</pre>
<p>Of course, my example is quite trivial and the only obvious benefit is that I put all the property definitions on one line. However, when you start adding other more complex objects (like perhaps those within your aggregate), then you have some powerful testing techniques under your fingers. </p>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/using-builders-in-tests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Are types of testing important?</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/are-types-of-testing-important/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/are-types-of-testing-important/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 20:45:08 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtaraporewalla.com/thoughts/?p=61</guid>
		<description><![CDATA[The comments on my last post about acceptance tests have made me think a little more about testing, particularly the value in specifically declaring the type of testing you are doing. 
Unit testing is a method of testing that verifies the individual units of source code are working properly
http://en.wikipedia.org/wiki/Unit_testing
 
Integration testing&#8230;is the phase of software testing in [...]]]></description>
			<content:encoded><![CDATA[<p>The comments on my last <a title="I don't believe in acceptance tests" href="http://sarahtaraporewalla.com/thoughts/?p=31" target="_blank">post about acceptance tests</a> have made me think a little more about testing, particularly the value in specifically declaring the type of testing you are doing. </p>
<blockquote><p>Unit testing is a method of testing that verifies the individual units of source code are working properly<br />
<em><a class="alignright" title="Unit testing" href="http://en.wikipedia.org/wiki/Unit_testing" target="_blank">http://en.wikipedia.org/wiki/Unit_testing</a></em></p></blockquote>
<p> </p>
<blockquote><p>Integration testing&#8230;is the phase of software testing in which individual software modules are combined and tested as a group<br />
<a class="alignright" title="Integration Testing" href="http://en.wikipedia.org/wiki/Integration_testing" target="_blank"><em>http://en.wikipedia.org/wiki/Integration_testing</em></a></p></blockquote>
<p>But what is the distinction between unit and integration tests? How granular do you make your unit tests? For instance, would you consider testing an <a title="Aggregate objects - Domain Driven Design" href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html" target="_blank">aggregate object</a> unit testing or integration testing? More importantly, what value do we get in declaring it.</p>
<p>When first learning how to write good tests, and learning about TDD and other wonderful testing practices, specifying what type of test you are writing helps define the boundaries of the test. Knowing this helps you know what objects to piece together to form the test for example to use a canned repository or use the real one. But once mastered, do you still need to explicitly call out the type. </p>
<p>I understand that some people use these distinctions in their CI process &#8211; their CI build runs the unit tests first and then, only if they are successful, they run the integration (mainly database) tests, and then the &#8220;acceptance&#8221; tests. The main drivers for doing this are that, by their nature, unit tests are quite fast and so you fail fast; integration (and acceptance) tests are often too slow as they sometimes start up the whole framework, or talk to slow database connections.</p>
<p>If someone gave me these reasons I think I would run through the <a title="5 Whys" href="http://en.wikipedia.org/wiki/5_Whys" target="_blank">5 Ys</a> with them to make sure that the problem they were actually solving by separating out the integration tests was the correct problem. For instance:</p>
<p><em><strong>Why</strong> do you run integration tests after your unit tests? Because they are slow and take a long time to run.<br />
<strong>Why</strong> do they take a long time to run? Because Rails initializes the whole environment when testing.<br />
<strong>Why</strong> do you need to initialize the environment? Umm&#8230;I guess I don&#8217;t need it for my tests&#8230;but Rails does it for me automatically.<br />
<strong>Why </strong>don&#8217;t you remove this unneeded dependancy? Umm&#8230;..</em></p>
<p>That&#8217;s where I would probably stop with the questioning, because I would point them in the direction of <a title="George Malamidis" href="http://nutrun.com/" target="_blank">George Malamidis</a>&#8217;s <a title="Fast testing in Rails" href="http://nutrun.com/weblog/rails-fast-test-suite/" target="_blank">work around for fast unit tests</a>. My point is that there maybe a better solution than separating the tests.</p>
<p> </p>
<p>So, what value do we really get by explicitly declaring what type a test should conform to?</p>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/are-types-of-testing-important/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I don&#8217;t believe in Acceptance Tests</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/i-dont-believe-in-acceptance-tests/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/i-dont-believe-in-acceptance-tests/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 06:44:44 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtaraporewalla.com/thoughts/?p=31</guid>
		<description><![CDATA[There &#8211; I said it. Heresy. But, I am yet to be sold on their value.
Before I continue, I should describe my observations of acceptance testing. From what I&#8217;ve seen, acceptance tests are created as assurance that a story is complete. They are a layer above unit, integration and persistence tests. Their assertions are based around acceptance [...]]]></description>
			<content:encoded><![CDATA[<p>There &#8211; I said it. Heresy. But, I am yet to be sold on their value.</p>
<p>Before I continue, I should describe my observations of acceptance testing. From what I&#8217;ve seen, acceptance tests are created as assurance that a story is complete. They are a layer above unit, integration and persistence tests. Their assertions are based around acceptance criteria, and verify the functionality for the story. And, from what I have seen (as with all other tests) they are not constantly maintained, once written forever forgotten, and quite long to run.</p>
<p>Somewhere down the road of the project, the build takes too long to run, or you spend too much time fixing broken acceptance tests or you have flaky acceptance tests, and your mind starts to filter out a red build, thinking the problem must be caused by the said flaky test. A common workaround to these problems is to stop writing as many acceptance tests.</p>
<p>I guess my biggest problem with acceptance testing (in this form) is that I can push down any assertion that is made in the acceptance tests onto integration, persistence or unit tests and have a higher level of confidence that the acceptance criteria has been met. In fact, when I finish developing my story, the integration/persistence/unit tests already cover all acceptance criteria.</p>
<p>Now, you might argue that acceptance tests make good reference material, as there is one place you can go do to find out what the story was meant to do at the time of sign-off. If this is true, why do I see developers constantly referring to the code or unit tests for this information, rather than the acceptance tests?</p>
<p>I don&#8217;t see the value of these story-based acceptance tests.</p>
<p>What I can see real value in is scenario-based testing. What I would like are a handful of scenario tests which describe the system as a whole. After all, the stories we develop have all been derived from some scenario which delivers business value. That way, as I develop a new story, I just extend the scenario tests to capture the essence of the story, but have all acceptance criteria still tested on the unit/integration tests.</p>
<p>Testing is a form of art; I don&#8217;t think we have mastered acceptance level testing. Perhaps scenario testing is the answer, but it requires constant improvement to them, maintaining them, refactoring were necessary; generally looking out for the well being of the tests: one thing I don&#8217;t think we are good at. Remember, refactoring your code base is just as important as refactoring your test code.</p>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/i-dont-believe-in-acceptance-tests/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Say the right thing &#8211; don&#8217;t just do the right thing.</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/say-the-right-thing-dont-just-do-the-right-thing/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/say-the-right-thing-dont-just-do-the-right-thing/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 09:48:00 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtarap.wordpress.com/2008/09/07/say-the-right-thing-dont-just-do-the-right-thing/</guid>
		<description><![CDATA[Often, I see tests like the following:

public void SuccessulUpdateOfCarDeatilsShouldReturnSuccessResponse()
{
CarController controller = new CarController()
controller.UpdateCarDetails(0, string.Empty, string.Empty, string.Empty)
Assert.AreEqual(200, controller.Response)
}

This conveys to me two things about the author of the test:


They treat tests as second-class citizens
They have no respect for me, the unknown reader of the test

Let me explain. I could be viewing these tests for a variety [...]]]></description>
			<content:encoded><![CDATA[<p>Often, I see tests like the following:</p>
<pre class="brush: csharp;">
public void SuccessulUpdateOfCarDeatilsShouldReturnSuccessResponse()
{
CarController controller = new CarController()
controller.UpdateCarDetails(0, string.Empty, string.Empty, string.Empty)
Assert.AreEqual(200, controller.Response)
}
</pre>
<div>This conveys to me two things about the author of the test:</div>
<div>
<ol>
<li>They treat tests as second-class citizens</li>
<li>They have no respect for me, the unknown reader of the test</li>
</ol>
<div>Let me explain. I could be viewing these tests for a variety of reasons &#8211; to add new tests as bugs have appeared, to alter the functionality of the unit-under-test, to understand the unit-under-test &#8230; etc.</div>
<div>Before I can understand what the test is actually testing, I need to look at the signature of the class (ie open the file, navigate to the constructor&#8230;) to determine what the string.empty and zeros are representing.  This takes extra effort on my part to understand the intent of the test*.</p>
<pre class="brush: csharp;">
public class CarController{
...
public void UpdateCarDeatils(int carId, string carRegistration, string colour, string engineIdentificationNumber)
{
...
}
}
</pre>
</div>
<div>It also makes life difficult, because I am not sure if they are there for a particular purpose (ie the test is testing how the method handles nulls/empty strings) or if they are there because the author thinks they they are unimportant to the test.</div>
<div>I prefer to explicitly call out my unimportant parameters, by passing realistic but dummy values. I would rework the above example into:</div>
<div>
<pre class="brush: csharp;">
public void SuccessulUpdateOfCarDeatilsShouldReturnSuccessResponse()
{
CarController controller = new CarController()
controller.UpdateCarDetails(42, &quot;car registration&quot;, &quot;colour&quot;, &quot;engineIdentificationNumber&quot;)
Assert.AreEqual(200, controller.Response)
}
</pre>
</div>
<div>This way I can see at a simple glance at the one test file what the method expects to take as parameters. I tend to use <a href="http://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Everything">42</a> as my default dummy id value, so when my team mates see it, they know it is an id value (zero is often an indicator of an object which is not persisted).</div>
<div>By replacing the meaningless empty values, with meaningless values which explain themselves you run into less problems adding the new tests, refactoring or altering functionality as the reader understands the intent of the test. Now, the test not only <span style="font-style:italic;">does</span> the right thing, but also <span style="font-style:italic;">says</span> the right thing.</div>
<div>*Even with Intelli-J or Resharper, there is work involved to understand these values</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/say-the-right-thing-dont-just-do-the-right-thing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test names go wild</title>
		<link>http://sarahtaraporewalla.com/thoughts/testing/test-names-go-wild/</link>
		<comments>http://sarahtaraporewalla.com/thoughts/testing/test-names-go-wild/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 14:41:00 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://sarahtarap.wordpress.com/2008/08/26/test-names-go-wild/</guid>
		<description><![CDATA[Around Thoughtworks, there is much discussion about test strategies. The arguments range from setup vs inline; named vs anonymous; mocks vs stubs; high level vs unit level, and everyone has their own preferred techniques. While a lot of people talk about the virtues of either side, I wanted to talk about using test names wisely.
I [...]]]></description>
			<content:encoded><![CDATA[<div>Around <a href="http://www.thoughtworks.com/">Thoughtworks</a>, there is much discussion about test strategies. The arguments range from setup vs inline; named vs anonymous; mocks vs stubs; high level vs unit level, and everyone has their own preferred techniques. While a lot of people talk about the virtues of either side, I wanted to talk about using test names wisely.</div>
<div>I have worked with <a href="http://blog.jayfields.com/">Jay Fields</a>, a man who is quite passionate about tests. His blog has a whole stream of entries around testing. One post which I believe caused a bit of a stir was his entry on <a href="http://blog.jayfields.com/2008/05/testing-value-of-test-names.html">the value of test names.</a></div>
<div>I understand where he is coming from, and if you are fortunate to be writing in Ruby, then your testing battles are half won already. If you use the gems like <a href="http://mocha.rubyforge.org/">mocha</a> and <a href="http://expectations.rubyforge.org/">expectations</a>, then testing is so natural, that you do end up with 2 to 3 line tests, <a href="http://blog.jayfields.com/2007/06/testing-one-assertion-per-test.html">one assertion per test</a> and there isn&#8217;t a need for test names because everything is so neat and understandable, that they become clutter.</div>
<div>However, if you are in a language like C# (which is what my current project is in) then testing becomes that little bit harder, a little more like running through treacle. Mocking is one example of this &#8211; compare <a href="http://www.nmock.org/">NMock</a> to mocha&#8230; well, actually don&#8217;t bother because they can&#8217;t compare. Mocha beats NMock hands down. On my current project, I started to use mocks as I would on another ruby-based project, and quickly switched to stubbed classes because it was lacking in far too many places.</div>
<div>So, sometimes your environments dictate which testing strategy you should use. But here is my plea &#8211; if you use test names, please oh please treat them with the same respect as you treat your method names.</div>
<div>My biggest problem with test names is that they may be perfectly fine when you write the code for the first time, but then sometime later someone starts refactoring the code base. Tests start failing (perhaps the requirements have changed, so the test failures are a good thing) and you fix the failing test&#8230;but not the test name. And so, a little while later you have a test which is similar to this very trivial example (and yes, I have seen tests like this):</div>
<div>
<pre class="brush: csharp;">
public void ShouldContainOrderedListOfClients()
{
Event first = new Event(&quot;AAA&quot;)
Event second = new Event(&quot;ZZZ&quot;)
Event[] result = Order(first, second)
Assert.AreEqual(first, result[0]) Assert.AreEqual(second, result[0])
}
</pre>
</div>
<div>Hang on &#8211; what the&#8230;? That test name has nothing to do with what is being tested. But which is correct &#8211; the test name or the test itself. And has there been a test which is now lost? Or has it moved? What is the source of truth?</div>
<div>Sometimes, knowing what the requirements for the unit in question are should be enough to fix up the offending item. Sometimes they are not.</div>
<div>My take home advice &#8211; think about what your test is doing before you begin to write the test. Perhaps you even write the names for 4 tests at once, before implementing any of them. That way you can focus on the test at hand,  you don&#8217;t have to be thinking of testing a whole variety of cases in the one test and your tests become small enough that you can easily identify when the test name differs from the test at hand.</div>
<div>Also, treat your test code not as a second class citizen, but as important, if not more important than your code. Tests give you a confidence in the application you are building. To increase your confidence in your tests, you need to increase the time you spend refactoring them and making sure they are testing exactly what they say they are testing, and that there are no holes where your code can fall through and fail.</div>
]]></content:encoded>
			<wfw:commentRss>http://sarahtaraporewalla.com/thoughts/testing/test-names-go-wild/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
