Sometimes you need to be careful how you setup your tests

I’ve been working with MSTest (the unit test framework that comes with Visual Studio) lately and I learned the hard way that it is a bad idea to not use the TestInitialize attribute. This may sound obvious to you but I’m more used to work in and there I typically setup as much as I can with field initializers and the rest in the constructor. So while working in MSTest I did the same thing; I setup as much as I could in field initializers. I added a failing test, implemented it and suddenly I had multiple test errors. Not failures, but errors. The message was: Type Tin assembly A is not serializable. Interestingly I did not touch T and it’s an exception so it’s definitely serializable… Looking at what a search on the Internet gives you for this error indicates problems with finding the right assemblies. Well, the build environment I’m using is kind of tweaked, but i didn’t change that when the tests started to report errors. By looking at the tests now with errors I manage to conclude that they all use a default constructor for an object that calls another constructor with an empty string. And that constructor does not accept an empty string. It should but it doesn’t. So now I know how to fix it but why the bad error message I think. Well I don’t know that, but i do know that when i moved the call to the constructor from the field initialization to a method with the TestInitialize attribute i got a test failure; Unexpected excpetion in initializer: T: Path must start with ‘/’. I guess I’ll stop using field initializers when dealing with MSTest in the future…

Comments (0)

Skip to main content