VistaDB 5
Running VistaDB Unit Tests - What Is NUnit?

NUnit is a unit testing framework. It allows a standardized way for you to write tests that can be manually or automatically run against your application. For more detailed information please visit the NUnit site.

The important thing to note about NUnit testing is that you are writing your tests in your language of choice (VB.NET or C#). You write classes and set attributes on them to be run by the test runner framework.

Basic Unit Test Example

The following is taken from the NUnit documentation:

namespace bank
{
    using NUnit.Framework;

    [TestFixture]
    public class AccountTest
    {
        [Test]
        public void TransferFunds()
        {
            Account source = new Account();
            source.Deposit(200.00F);
            Account destination = new Account();
            destination.Deposit(150.00F);
            source.TransferFunds(destination, 100.00F);
            Assert.AreEqual(250.00F, destination.Balance);
            Assert.AreEqual(100.00F, source.Balance);
        }
    }
}

The first thing to notice about this class is that it has a [TestFixture] attribute associated with it.  This is the way to indicate that the class contains test code (this attribute can be inherited). The class has to be public and there are no restrictions on its superclass. The class also has to have a default constructor.

The only method in the class TransferFunds, has a [Test] attribute associated with it this is an indication that it is a test method. Test methods have to return void and take no parameters. In our test method we do the usual initialization of the required test objects, execute the tested business method and check the state of the business objects. The Assert class defines a collection of methods used to check the post-conditions and in our example we use the AreEqual method to make sure that after the transfer both accounts have the correct balances (there are several overloads of this method, the version that was used in this example has the following parameters : the first parameter is an expected value and the second parameter is the actual value).

Other Unit Test Libraries

There are a number of other testing frameworks for .NET (XUnit and MSTest which is included in most versions of Visual Studio). Most languages have some form unit testing framework available.

See Also