Test-driven development with NUnit

Wrapping Up

The Nunit-Addin

Now that you understand the basics of writing unit tests with Nunit, it's time for me to introduce one of the coolest gadgets related to this subject – the Nunit Addin.

This add-in allows you to ,instead of re-opening the Nunit-Gui every time you need to make sure your tests pass, to just right click on the project or class you wish to test and hit “Run Test(s)”.  You'll get all the information inside VS.NET's output window.

This add-in allows more than just this functionality, however. It allows you to test a single method from inside the code editor. Just click anywhere inside the code of that method and hit “Run test”.

Another very powerful feature allows you to do what is called “Ad-Hoc testing”. You can create any method, and not even put a [Test] attribute on it. Than, inside that method right click and hit “Test with”-“Debugger” and you immediately step into that method without needed to create a separate project that calls this method. Indeed, very powerful. You can also debug using different versions of the .NET framework or even Mono. This add-in is a must have for quick incremental development.

A word before we finish

The technique I've shown here means very little if not pursued diligently. Remember – the first thing you ever do is write the test, not the code .  If you keep this up you'll eventually end up with a system that is fully testable and with fewer bugs. You'll also find that you think about your component's design more responsibly, because you're looking at them from a different perspective.  Once you get the “Zen” of it, you'll start to even have more fun doing it. You'll also gain confidence in changing your code. You'll get instant feedback if something broke and you can squash bugs at their inception point.

Another thing that needs to be known– Nunit is the unit testing framework for .Net, but like Nunit there are many others, for practically any semi-popular programming language out there. If you program in C++ for example, take a look at CPPUnit. There's also a JUnit out there. In fact, Nunit is a port from JUnit into .Net. There are also commercial frameworks and addins that try to provide added functionality through add-ins for .Net unit testing: Some of those include HarnessIt, csUnit and X-Unity.

Most of the non-.Net frameworks all support the same kind of logical notions of test case, fixture and suite, but each one might provide different means of expressing them. Attributes are unique to .Net but in other OO laguages you might have to derive a class from  TestFixture to declare it  as a fixture and so on. You can find the complete list of frameworks over at www.xprogramming.com (look for the “downloads” link).

Advanced issues

This article is just a first in this series. In the next articles I'll talk more about real world problems facing a developer who wants to test real-world applications. Some of these issues include:

  • Testing abstract classes?
  • Testing complex object models and dependencies
  • Testing database related features
  • Mock objects and their use
  • Testing GUI interactions

You might also like...

Comments

About the author

Roy Osherove Israel

Roy Osherove has spent the past 6+ years developing data driven applications for various companies in Israel. He's acquired several MCP titles, written a number of articles on various .NET topic...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond