September 16, 2008

Exception handling in NUnit

Filed under: NUnit,Tips and Tricks — Tags: , — @ 2:52 pm

This is very simple but I thought, it is worth a note (or for my note taking).  Before I go into handling exception, I need to tell you something I read couple of days ago. I can not remember which blog (I follow lot of blogs) but it kinda stuck with me. When you code you try to follow the following four things

  • Make it correct
  • Make it clear
  • Make it concise
  • Make it fast
I was tossed it to my friend Jim at work (I consider him very sharp and talented and look up to him). Here is what he has to say 
  • Write a test
  • Make it pass
  • Refactor
  • Optimize if needed <- condition seldom met, may be 1/10000 loops
  • repeat the loop
If you look closely both of them are same. What Jim specified in the loop above more of TDD in mind and the earlier one is much more general and it can adapt to any environment. But I thought it is worth sharing.
Lets go back to NUnit thing I was talking about. In one of coding situation, I need to throw an exception with just a string. Something like the following
throw new Exception(“Test exception”);
Now when you test it in NUnit, you need to make sure you use
[ExpectedException(“System.Exception”, ExpectedMessage=”Test exception”)]
thats it, nothing more. See this is very simple blog.

Powered by WordPress