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.

April 29, 2008

NUnit in VS2008

Filed under: Tips and Tricks — Tags: , — @ 2:58 am

Even though VS2008 comes with Microsoft version of the unit testing, I still prefer NUnit. The real reason is one less to learn and on the other hand NUnit does gives me all the good things required for my TDD. I can imagine Microsoft test suite might give lot of nice integration with VS2008, NUnit can be integrated nicely to get what you want. There are third party tools you can buy that will integrate NUnit nicely into Visual Studio but I am not sure about VS2008 and if you are a person like me, I do not like to spend money on a tool if there is a way you can do it. By the way, if I spend money I spend it on book 🙂

My first approach and easy approach is to attach NUnit process to my Debug process and put break point in the line of code where I want to debug and everything goes well. Well, let me explain what I meant by this. Here are my steps, this can change based on your preference;


Powered by WordPress