Home > Microsoft .Net Development Tips > Application Testing and Security > Find and fix bugs with NUnit
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

APPLICATION TESTING AND SECURITY

Find and fix bugs with NUnit


Ken McNamee
05.15.2003
Rating: -3.67- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Please let other users know how useful this tip is by rating it below. Do you have a tip or code of your own you'd like to share? Submit it here.


Test your business logic with this structured API.

Arguably, the most important step in professional software development is the testing phase. I'm not talking about load testing, although that is a critical step as well. I'm talking about testing the classes and methods that implement your business logic to ensure they function the way you intend and return the correct results. If you know a method should return a Boolean value of true but it returns false instead, that's certainly a problem you need to address. NUnit is a tool that gives you the power to find these bugs and address them while in the development phase rather than during QA or production.

NUnit has two components: the NUnit Framework API you use to create the testing assembly, and the Windows Forms application you use to run the testing assembly and display the results. Take this class for example:

This is about as simple as it gets, but it will work well for 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 in the 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.Divide method looks like:

The first thing you might notice is the [TestFixture] class attribute and the [Test] method attribute. When the NUnit GUI application reflects on the compiled assembly, it uses these attributes to know which classes and methods to run. The previous version of NUnit requires that the test class inherit from a TestCase class and that the names of the methods running the tests begin with a "Test" prefix. These are no longer requirements because the NUnit developers have moved on to an attribute-based system that is more elegant, fle


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
.NET Framework security tools
Toolset auto generates test data for database testing
Windows Developments: Product news February 2008
Rhino Mocks adds remote proxies, calls to void methods
Axosoft bug tracker adds wiki, improves collaboration
Exact Magic updates it Visual Studio tools
Windows Developments: Product news, January 2008
JetBrains updates TeamCity, offers free Professional edition
Tool allows cross-site, cross-platform access to SourceSafe
Automation tool provides functional Web app testing
Windows Developments: Product news, November 2007

Application Testing and Security
Test-driven development in .NET yields complete unit test coverage
How to write installers in Vista that work correctly under UAC
How to elevate programs' privileges correctly using Vista's UAC
Internet Explorer 8 beta's development tools add source visualizations
Microsoft previews new features in Visual Studio 2010
Advanced Windows Debugging Book Chapter and Podcast
Book excerpt: Advanced Windows Debugging
Book excerpt: Pragmatic unit testing in C# with NUnit
Security interoperability with .NET/WSE and WebLogic Workshop 8.1
How to avoid regression bugs while adding new features

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


xible, and intuitive.

The key line of code in the DivideTest method is the one calling Assertion.AssertEquals. The first parameter passed in represents the value the Math.Divide method must return for it to work correctly and for the test to pass. AssertEquals simply compares that value with the one in the second parameter, which is the result variable. Again, this is a fairly simple example but it demonstrates the simplicity of NUnit adequately. You can take what I've shown so far and get an enormous return on your investment in NUnit and, because NUnit is open-source and free, the only investment required is a little bit of your time.

In addition to testing the values returned from your methods, you also can verify that exceptions you would expect to be thrown under certain circumstances are indeed thrown. This is an important point because your application might depend on certain code being run within a particular catch block. You could rewrite the DivideTest method like this:

The ExpectedException attribute tells NUnit that this call to Math.Divide must throw a DivideByZeroException in order for this test to pass. This is a convenient capability and an example of a test that would certainly be possible to duplicate using your own testing mechanism. Yet, this is not necessary because NUnit provides this ability. If the ExpectedException attribute were missing from this code, you would see something similar to Figure 1 when you run the test.

[IMAGE]

NUnit provides an easy, consistent framework for testing your code, thus negating the need to develop your own testing mechanism. The fact 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 integrity of its results should be a frequent and well understood step in your everyday development workflow. This process is sometimes painful, so I believe you should consider any solution that can make it easier and more successful, perhaps implementing it on a trial basis. I strongly recommend that you download NUnit and make a wholehearted effort to integrate it into your development process. Change can be difficult, but the long-term gains in productivity and code quality far outweigh any short-term grief.

The sample used in this article is available for download.

REFERENCES

  • NUnit home page: http://nunit.org
  • NUnit at SourceForge: http://sourceforge.net/projects/nunit

    Ken McNamee is a senior software engineer with RelayHealth Corp., a provider of secure, Web-based services for doctor-patient communication. Prior to this, he led a team of developers in rebuilding the Home Shopping Network's e-commerce site, HSN.com, to 100 percent ASP.NET with C#. E-mail Ken at mailto:kenmcnamee@hotmail.com.


    This article was provided by asp.netPRO Magazine, an online information resource for the ASP.NET developer community, which helps professional software developers build, deploy and run the next generation of dynamic, distributed Web applications quickly and easily. Click here for subscription information.

    Rate this Tip
    To rate tips, you must be a member of SearchWinDevelopment.com.
    Register now to start rating these tips. Log in if you are already a member.




    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts