I'm writing API tests and am using the mstest framework. My setup is pretty simple, my cleanup and initialize functions look like below -- stand up and shut down logging instaces. My problem is that some tests throw exceptions that are not caught (this is usually due to test code that is still in development). This causes the test to fail, which I would expect, but I can't manage to get the actual exception object and log it which makes debugging a little difficult. The obvious answer is "wrap your test in a try catch block that handles the logging and gracefully fails the test", but I don't really like that because it just adds another responsibility that future test authors (probably me) poorly implementing the catch, log, and rethrow logic.
TL;DR : How can I, using TestContext (or another solution) get access to an exception that was thrown but not caught by the actual testing logic in the test cleanup method?
public TestContext TestContext { get; set; }
[TestInitialize]
public void TestInitialize()
{
Log.RegisterLogger(new TestLog() {
TestCaseName = TestContext.TestName,
TestCastStart = DateTime.UtcNow
});
}
[TestCleanup]
public void TestCleanup()
{
/*I want to be able to get the details of an uncaught exception here*/
Log.FinalizeLogging();
}