Find and Fix Bugs With NUnit
Test your business logic with this structured API.
October 30, 2009
ToolKit
LANGUAGES: All .NET Languages
TECHNOLOGIES: .NETAttributes
Find and Fix Bugs With NUnit
Test your business logic with this structured API.
By Ken McNamee
Arguably, the most important step in professional softwaredevelopment is the testing phase. I'm not talking about load testing, althoughthat is a critical step as well. I'm talking about testing the classes andmethods that implement your business logic to ensure they function the way youintend and return the correct results. If you know a method should return aBoolean value of true but it returns false instead, that's certainly a problemyou need to address. NUnit is a tool that gives you the power to find thesebugs and address them while in the development phase rather than during QA orproduction.
NUnit has two components: the NUnit Framework API you useto create the testing assembly, and the Windows Forms application you use torun the testing assembly and display the results. Take this class for example:
public class Math {
public static intDivide(int a, int b) {
return a / b;
}
}
This is about as simple as it gets, but it will work wellfor this example. To verify that the Add method can add two integers correctly,you must first create another class to test it. You can create this class inthe same source-code file as the class you're testing, or in a different file,project, or even namespace. Here is what the test class for the Math.Dividemethod looks like:
[TestFixture]
public class MathTest {
[Test]
public voidDivideTest() {
int result =Math.Divide(4, 2);
Assertion.AssertEquals(2, result);
}
}
The first thing you might notice is the [TestFixture]class attribute and the [Test] method attribute. When the NUnit GUI applicationreflects on the compiled assembly, it uses these attributes to know whichclasses and methods to run. The previous version of NUnit requires that thetest class inherit from a TestCase class and that the names of the methodsrunning the tests begin with a "Test" prefix. These are no longer requirementsbecause the NUnit developers have moved on to an attribute-based system that ismore elegant, flexible, and intuitive.
The key line of code in the DivideTest method is the onecalling Assertion.AssertEquals. The first parameter passed in represents thevalue the Math.Divide method must return for it to work correctly and for thetest to pass. AssertEquals simply compares that value with the one in thesecond parameter, which is the result variable. Again, this is a fairly simpleexample but it demonstrates the simplicity of NUnit adequately. You can takewhat I've shown so far and get an enormous return on your investment in NUnitand, because NUnit is open-source and free, the only investment required is alittle bit of your time.
In addition to testing the values returned from yourmethods, you also can verify that exceptions you would expect to be thrownunder certain circumstances are indeed thrown. This is an important pointbecause your application might depend on certain code being run within aparticular catch block. You could rewrite the DivideTest method like this:
[TestFixture]
public class MathTest {
[Test]
[ExpectedException(typeof(DivideByZeroException))]
public void DivideTest(){
int result =Math.Divide(4, 0);
}
}
The ExpectedException attribute tells NUnit that this callto Math.Divide must throw a DivideByZeroException in order for this test topass. This is a convenient capability and an example of a test that wouldcertainly be possible to duplicate using your own testing mechanism. Yet, thisis not necessary because NUnit provides this ability. If the ExpectedExceptionattribute were missing from this code, you would see something similar toFigure 1 when you run the test.
Figure 1. Not telling NUnit which exceptions to expect results in a testfailure.
NUnit provides an easy, consistent framework for testingyour code, thus negating the need to develop your own testing mechanism. Thefact that it's also free and open-source makes its use - in my humble opinion -a no-brainer. Verifying the soundness of your business logic and the integrityof its results should be a frequent and well understood step in your everydaydevelopment workflow. This process is sometimes painful, so I believe youshould consider any solution that can make it easier and more successful,perhaps implementing it on a trial basis. I strongly recommend that youdownload NUnit and make a wholehearted effort to integrate it into yourdevelopment process. Change can be difficult, but the long-term gains inproductivity and code quality far outweigh any short-term grief.
The sample used in thisarticle is available for download.
REFERENCES
NUnithome page: http://nunit.org
NUnitat SourceForge: http://sourceforge.net/projects/nunit
Ken McNamee is a senior software engineer withRelayHealth Corp., a provider of secure, Web-based services for doctor-patientcommunication. Prior to this, he led a team of developers in rebuilding theHome Shopping Network's e-commerce site, HSN.com, to 100 percent ASP.NET withC#. E-mail Ken at mailto:[email protected].
Tell us what you think! Please send any comments about thisarticle to [email protected] include the article title and author.
About the Author
You May Also Like