<?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>Yeah it&#039;s a blog &#187; testing</title>
	<atom:link href="http://www.owenpellegrin.com/blog/category/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.owenpellegrin.com</link>
	<description>I write stuff here</description>
	<lastBuildDate>Sun, 14 Aug 2011 17:36:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>How do you solve multiple asserts?</title>
		<link>http://www.owenpellegrin.com/blog/testing/how-do-you-solve-multiple-asserts/</link>
		<comments>http://www.owenpellegrin.com/blog/testing/how-do-you-solve-multiple-asserts/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 21:21:37 +0000</pubDate>
		<dc:creator>Owen Pellegrin</dc:creator>
				<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.owenpellegrin.com/blog/uncategorized/how-do-you-solve-multiple-asserts/</guid>
		<description><![CDATA[I’ve been studying unit testing for a few months, and trying to practice a more TDD approach to writing code.&#160; The most influential book that inspired this is Roy Osherove’s The Art of Unit Testing.&#160; It’s a great introduction to &#8230; <a href="http://www.owenpellegrin.com/blog/testing/how-do-you-solve-multiple-asserts/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I’ve been studying unit testing for a few months, and trying to practice a more TDD approach to writing code.&#160; The most influential book that inspired this is Roy Osherove’s The Art of Unit Testing.&#160; It’s a great introduction to the topic of unit testing and serves as a fantastic stepping stone to more advanced discussions on the topic.</p>
<p>One of the things that Osherove warns against is multiple asserts in unit tests.&#160; That is, one should generally avoid writing tests that can fail for more than one reason:</p>
<pre class="brush: csharp;">[Fact()]
public void Test_Some_Condition()
{
    Foo() sut = new Foo();
    bool[] expected = { true, false };

    bool[] actual = sut.GetSomeResult(SomeInput());

    Assert.Equals(2, actual.Length);
    Assert.Equals(expected[0], actual[0]);
    Assert.Equals(expected[1], actual[1]);
}</pre>
<p>I agree with this sentiment.&#160; Sticking to one assert per test tends to make it easier to figure out what is wrong when a test fails.&#160; If you have multiple asserts, the first one to fail tends to end the test; perhaps 2 or more assertions would have failed but you only get information about one.&#160; (My example is poor for this: if Length is incorrect then clearly the rest of the assertions will fail.)&#160; I’m not really sure I like any of the solutions to this problem.&#160; I’ll look at a few solutions and explain why.</p>
<h3>Messages with the asserts</h3>
<p>This solution involves attaching string messages to the assertions to make it clear which assertion failed for what reason.&#160; This can help if you have several of the same kind of assertion and the values don’t make it clear which one fails.&#160; I don’t like this technique for a personal reason: I use XUnit .NET and it only allows string messages on its true/false assertions:</p>
<pre class="brush: csharp;">[Fact()]
public void Test_Some_Condition()
{
    // ...

    Assert.True(
        2 == actual.Length,
        string.Format(&quot;The length should have been {0}, but was {1} instead.&quot;,
            2,
            actual.Length
            )
        );
    // ...
}</pre>
<p>This looks atrocious and is difficult to write.&#160; It’s easier in test frameworks like NUnit that provide the string message as part of the assertion.&#160; It’s also tedious to write multiple failure messages; I’d rather write “Arrays were not equal” than multiple individual strings, particularly when dealing with larger arrays that will require string interpolation to give me expected/actual values.</p>
<h3></h3>
</p>
<h3>Custom Assert Methods</h3>
<p>This is an idea I picked up from XUnit Test Patterns.&#160; Generally, these groups of multiple assertions tend to represent one logical assertion, so you wrap these multiple assertions in a single custom assertion method:</p>
<pre class="brush: csharp;">[Fact()]
public void Test_Some_Condition()
{
    // ...

    AssertArraysAreEqual(expected, actual);
}

private void AssertArraysAreEqual(bool[] expected, bool[] actual)
{
    Assert.Equals(expected.Length, actual.Length);
    Assert.Equals(expected[0], actual[0]);
    Assert.Equals(expected[1], actual[1]);
}</pre>
<p>This doesn’t address the problems that arise when the order of the assertions hides that multiple failures are happening.&#160; It also doesn’t shed light on the fact that the custom assertion exists; “Assert.Equals failed, Expected: 0 Actual: 1” is not as helpful here as “Arrays were not equal.”&#160; Adding strings to the messages can help a little bit, but I’ve already noted why this isn’t a universally convenient solution.</p>
<h3>More Verbose Custom Assert Methods</h3>
<p>This is something I tried recently and it burns like fire.&#160; Instead of making multiple asserts inside of the custom assert, I make multiple tests and build a string that describes what tests fail:</p>
<pre class="brush: csharp;">private void AssertArraysAreEqual(bool[] expected, bool[] actual)
{
    bool lengthsAreSame = (expected.Length == actual.Length);
    bool elementsAreSame = true;
    for (int i = 0; i &lt; elementsAreSame.Length; i++)
    {
        if (expected[i] != actual[i])
        {
            elementsAreSame = false;
            break;
        }
    }

    if (!lengthsAreSame)
    {
        Assert.Fail(&quot;Array lengths are not equal.&quot;);
    }
    else if (!elementsAreSame)
    {
        Assert.Fail(&quot;Some elements held unexpected values.&quot;);
    }

    Assert.Pass();
}</pre>
<p>If that doesn’t make your eyes hurt, you aren’t looking hard enough.&#160; It’s big.&#160; It has logic that should probably require its own tests to verify.&#160; It was a pain in the butt to write.&#160; (And what makes it worse is the code I’m testing that spawned this post actually works with 2D rectangular and jagged arrays.)&#160; Note that XUnit .NET doesn’t have an Assert.Fail() equivalent, so I’d really end up using something nasty like “Assert.False(true, “…”)”.&#160; I’m starting to question why I’m using it again.</p>
<h3></h3>
<h3>Multiple Tests</h3>
<p>On the “pain of implementation” meter this is pretty high.&#160; To implement this solution, you create an individual test for each assertion:</p>
<pre class="brush: csharp;">[Fact()]
public void Foo_GetSomeResult_ValidInput_CorrectLength()
{
    // setup code

    Assert.Equals(2, actual.Length);
}

[Fact()]
public void Foo_GetSomeResult_ValidInput_CorrectElements()
{
    // setup code

    Assert.Equals(expected[0], actual[0]);
    Assert.Equals(expected[1], actual[1]);
}</pre>
<p>That doesn’t even fully solve the problem!&#160; Note that “CorrectElements” had to make multiple asserts, and without adding a custom string it’s hard to figure out which elements are wrong if that information is needed.&#160; The setup logic is heavily duplicated; this tends to lead to the need to implement setup methods, which can cloud the scenario under test (particualrly if you use your test framework’s automatic setup/teardown mechanism.)</p>
<h3>Am I interpreting a guideline as a rule?</h3>
<p>I’m not sure I need to solve the problem every time I see it.&#160; Since all of the solutions I know about have some tradeoffs I don’t want to deal with, perhaps sometimes it’s best to just live with multiple asserts?</p>
<p>I’m not happy with that answer.&#160; I made this post because of some “do something fancy with multi-dimensional arrays” code I’m testing.&#160; I decided to live with multiple asserts at first.&#160; Then, I had a couple of tests fail where the diagnosis would have been easier had I known that more than one assertion was failing.&#160; I tried moving to the verbose custom assert technique outlined above, but the custom assert ended up as a &gt; 30-line behemoth that feels many different flavors of wrong.</p>
<p>Maybe I should just live with multiple asserts in this case, since the techniques to avoid them would cause more problems than they solve?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.owenpellegrin.com/blog/testing/how-do-you-solve-multiple-asserts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

