Failed assertion throws an exception…

Hmm, this is something I did not know: when a testing framework’s Assert method fails, an exception gets thrown. We don’t actually see that exception, because the framework catches it. We usually don’t see it because tests normally look like this:

[Test]
public void SomeTest()
{
    Assert.IsTrue(false, "whatever");
}

However, say the test was written like this:

[Test]
public void Test()
{
    try
    {
        Assert.IsTrue(false, "whatever");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.GetType().FullName);
    }
}

We get the following printed on the Output window:

NUnit.Framework.AssertionException

So why would somebody ever write a test like the one above, you may ask, right? Well, like I mentioned the other day, I wrote a framework for integration tests that has a bunch of assertions for some things so that the developer doesn’t have to write those over and over again. Even though those things are hidden from the developer, I try to capture as much information as possible, and if any assertion fails, I give all the details to the developer so that he know exactly what failed during the integration.

When running a specific test here, I noticed that I was getting some extra information that shouldn’t really be there, and that was because one assertion was failing and was getting trapped by a try/catch somewhere, and after some digging I actually realized this behavior of assertions throwing "AssertionException"’s.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: