NUnit – A Tool for .NET you can’t live without!

If you’re a .NET developer and you’re NOT using NUnit, you REALLY need this utility!!!  This is something you should be really excited about.  Download it from here:

NUnit.org

What is NUnit?

NUnit is a testing framework.  In other words, when you write a class, library, web service, WCF service, or anything else that can be called from other code, you then write code that tests your library.  NUnit greatly simplifies and formalizes this and provides a GUI application that can load, run, and display the results of your tests.  It also provides a command line utility to run them too to help you automate the running of your tests with say an automated nightly build.

image

The user interface to NUnit after running a test suit.

So, what’s so great about that you ask?

EVERYTHING!  For me, it’s regression testing.  How many times has your code gotten really complex, then you find a bug, fix it, then later fix some other bug, only to break the first one?  The bigger and more complex your code, the more likely this is to happen AND the less likely you’ll want to get into the middle of it to make any changes.  This is especially true when making changes to someone else’s code.  Sometimes, you don’t know if what you changed had a affect on anything else, nor do you even know what the correct results are supposed to be, when working with someone else’s code.

The tests that you write (or that another developer has hopefully provided for you with their libraries) define how a particular method or routine should react to certain inputs.  When you make a change that you’re unsure of, you can then rerun the entire test suite to see, within seconds, what broke.

This is part of what Test Driven Development is all about.  You write your code, write your tests (or write your tests first, if you’re wanting to be a purist), run your tests, write more code, write more tests, rerun your tests.  This is a continuous process.  When you’re ready to ship, you’ll have the confidence that your stuff really works, AND you can PROVE that it works, repeatedly!

Once you identify a bug (or once one of your testers or users reports a bug), you then write a new unit test for that bug in your test suite.  Now, every time you rerun your test suite, that bug is tested for every single time, as long as that product is in continuous development.  This means, once a bug is identified, it should never resurface!

Until you get involved in this, this sounds like really dry, academic stuff.  But trust me, it changes your whole perception of programming.  You’ll find yourself changing the way you program to accommodate for unit testing.  Your code becomes more modular, more reusable, more stable.  You become significantly more confident in your code and the changes you make to other people’s code. It is truly remarkable how much this changes you.  The people that use your code, in most circumstances, will notice an improvement in the quality and robustness of the code you write.

If you’ve been getting into MVC (Model View Controller), then there’s just no excuse at all for not using NUnit.  MVC is perfectly tailored for TDD.

Oh!  And yes, Visual Studio 2008 does indeed have some of this capability built in.  If you don’t use NUnit, you should at least be using that, but NUnit has more capability (It’s been around since about 2002… Visual Studio’s testing framework has only been around since VS 2008 was released… It’s not as mature).

Check out my next article on how to use NUnit with your code.

NUnit – Writing your first Unit test

It’s really simple.  There’s not a big learning curve.  It’s simple and straight forward.

Leave a Reply