MSTest breaking changes in VS2013

Mariusz Wojcik

Over three years ago I posted a blog showing how you can extend MS Test to verify that code under the test throws expected exception with given message. The solution extends MS Test with new test attribute and reports assertion error when either exception type or message doesn’t satisfy expected values. It is based on the way Microsoft implemented ExpectedExceptionAttribute and works well until you move to Visual Studio 2013.

Unfortunately there are breaking changes in the way VS2013 test runner is working, and as a result the extension doesn’t work any more.

The problem is that TestContext property is no longer initialised on the attribute class. It looks like Microsoft developers have messed up test runner and there is some hacky solution which will initialise TestContext on the ExpectedExceptionBaseAttribute base class only when derived type is ExpectedExceptionAttribute. Any other type will not have property initialised. I even tried example from MSDN site with no luck – the property doesn’t get initialised.

Enough complaining, time to give some solution to the problem. To solve the issue I had to remove all usages of TestContext. Fortunately, the only area when it was used was in assert error messages for getting name of test method (quite similar to what ExpectedExceptionAttribute does). It is nice to get this information in assert errors, but it is not necessarily required. Below is the modified code. I hope that will solve the problems you may experience.

As for me, I moved back to using NUnit couple of years ago. I also am using FluentAssertions.