MSTest Test Context Exception Handling
Posted
by Flip
on Stack Overflow
See other posts from Stack Overflow
or by Flip
Published on 2010-01-14T16:33:34Z
Indexed on
2010/05/03
3:28 UTC
Read the original article
Hit count: 521
Is there a way that I can get to the exception that was handled by the MSTest framework using the TestContext or some other method on a base test class?
If an unhandled exception occurs in one of my tests, I'd like to spin through all the items in the exception.Data dictionary and display them to the test result to help me figure out why the test failed (we usually add data to the exception to help us debug in the production env, so I'd like to do the same for testing).
Note: I am not testing that an exception was SUPPOSED TO HAPPEN (I have other tests for that), I am testing a valid case, I just need to see the exception data.
Here is a code example of what I'm talking about.
[TestMethod]
public void IsFinanceDeadlineDateValid()
{
var target = new BusinessObject();
SetupBusinessObject(target);
//How can I capture this in the text context so I can display all the data
//in the exception in the test result...
var expected = 100;
try
{
Assert.AreEqual(expected, target.PerformSomeCalculationThatMayDivideByZero());
}
catch (Exception ex)
{
ex.Data.Add("SomethingImportant", "I want to see this in the test result, as its important");
ex.Data.Add("Expected", expected);
throw ex;
}
}
I understand there are issues around why I probably shouldn't have such an encapsulating method, but we also have sub tests to test all the functionality of PerformSomeCalculation...
However, if the test fails, 99% of the time, I rerun it passes, so I can't debug anything without this information. I would also like to do this on a GLOBAL level, so that if any test fails, I get the information in the test results, as opposed to doing it for each individual test.
Here is the code that would put the exception info in the test results.
public void AddDataFromExceptionToResults(Exception ex)
{
StringBuilder whereAmI = new StringBuilder();
var holdException = ex;
while (holdException != null)
{
Console.WriteLine(whereAmI.ToString() + "--" + holdException.Message);
foreach (var item in holdException.Data.Keys)
{
Console.WriteLine(whereAmI.ToString() + "--Data--" + item + ":" + holdException.Data[item]);
}
holdException = holdException.InnerException;
}
}
© Stack Overflow or respective owner