Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test throws exception but cannot see the message

Hi all I'm making a Chess AI as hobby project with Test Driven Development. But for some reason the only message my tests give is "test has thrown an exception: ...". And therefore omitting the only thing that matters. Instead of directly reading the error I now have to right-click and view test result details. I have tried adding and removing columns, but I cannot get the whole message to be shown directly.

Can VS2010 be setup so the exception message gets shown directly for each unit test?

edit: I am using standaard VS unit tests:

[TestClass]
public class MiniMaxTest
{
    [TestMethod]
    public void TestConstructor()
    {
        throw new Exception("Must I click view details to see this?");
    }
}

Why these questions? You guys can reproduce these things. Debug or Run tests give the same messages: exception message remains hidden

like image 553
MrFox Avatar asked Jun 17 '12 21:06

MrFox


People also ask

How do you test a method that throws an exception?

In order to test the exception thrown by any method in JUnit 4, you need to use @Test(expected=IllegalArgumentException. class) annotation. You can replace IllegalArgumentException. class with any other exception e.g. NullPointerException.

How do I assert exception messages in xUnit?

Throws<T> is used to capture the exceptions in the xUnit unit test. We need to provide Assert. Throws<T> with an exception type, and an Action which is supposed to throw an exception. In our case, If num2 is zero then, capture the "ArithmeticException".

Should JUnit tests throw exceptions?

The JUnit TestRunners will catch the thrown Exception regardless so you don't have to worry about your entire test suite bailing out if an Exception is thrown. This is the best answer. I'll add that I think the question here is one of style: catch-and-fail, or throw? Normally, best practice avoids "throws Exception".


1 Answers

No, I don't believe you can configure VS to show it differently. The IDE shows the first line of the exception message, and there's a newline character in the full message text, so you'll need to click through to the details to view the whole thing.

What you can do however, is abuse the MSTest framework to show your own messages.

MS Test Assertions are all implemented by throwing exceptions. All the MS Test Assert functions throw exceptions which are derived from UnitTestAssertException. Visual studio has special handling for these kinds of exceptions.

For example: If you write this test:

[TestMethod]
public void AllAboard()
{
    throw new AssertFailedException("Failboat");
}

AssertFailedException is the standard base class for most other assertion failures.

You'll note that VS2010 does not print the generic "test threw an exception" message, instead it just prints "Failboat".

Now what you can do is surround your tests in things that convert normal exceptions into AssertFailedException and then you can print whatever messages you like.

[TestMethod]
public void TestStuff()
{
    try
    {
        string o = null; // pretend this came from some real code
        var x = o.Split(new[] { ',' }); // this will throw NullRefException
    }
    catch (Exception e)
    {
        throw new AssertFailedException(e.Message, e);
    }
}

Of course, I wouldn't recommend actually doing this... it's verbose, and more importantly, you lose the call stack... but hey, now you've got one more tool in your toolbelt

like image 53
Orion Edwards Avatar answered Oct 16 '22 19:10

Orion Edwards