Sunday 28 September 2008

MSTest – ExpectedException and exception message.

Since this is my first blog post ill start out small enough.

Recently we have migrated our unit test from NUnit into MSTest. Although by large the transition was quite smooth, one of the more annoying issues is the different behavior of the ExpectedException attribute.

An intended design or an oversight by MS?

I would (wishfully) think the latter is the case. But in any case I would say its a Mistake.

In any case, since MsTest does not support asserting on the exception message, we had to revise some of the tests. The revision itself is quite simple, just need to wrap the test code with try catch clause. Something like:

[TestMethod]
public void SomeTest()
{
try
{
//The test code:
//...
//...
Assert.Fail("We should not get here");
}
catch(SomeExceptionType ex)
{
Assert.AreEqual(
"Expected Message",
ex.Message);
}
}

However after doing this for a couple of tests we have managed to come up with a slightly better pattern:

[TestMethod]
[ExpectedException(
typeof(SomeExceptionType))]
public void SomeTest()
{
try
{
//The test code:
//...
//...

}
catch(SomeExceptionType ex)
{
Assert.AreEqual(
"Expected Message",
ex.Message);
throw;
}
}

Although the difference is quite minor, still we found the second version much more expressive and readable.

To conclude, there’s a lesson to be learnt here. When trying to replace a well used framework its best to make sure that the transition will be as easy as possible (Unless you are MS and can get away with this).

Saturday 27 September 2008

First Post (Introduction)

Hi, my name is Lior Friedman and I'm a software developer.
This blog is all about me trying to recapture and share my past (and future) mistakes, in the hope that putting them on paper will help me remember and avoid repeating them in the future. I will try to focus on my own personal mistakes, but I’m sure that from time to time I will share those I have seen made around me.

I've been developing software in various fields and technologies for more then 10 years now (wow time do fly), and during the way I've made more then my fair share of errors along the way. The bigger of them are what got me to look up agile methodologies and so far I have found those much more appealing and effective then the rest.

So do expect a good share of philosophy here, I will however always try to relate everything to real world experience. Also being a technical guy I’ll also try to put in technical issues as much as I can in hope that someone out there will find this interesting.

Being an experiment I really hope that you can share your thoughts and comment with me, feel free to use the comments to put everything you wish that me or other should know. I would really like to know if someone out there finds this interesting or not

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Walgreens Printable Coupons