Team LiB
Previous Section Next Section

Testing Tools

If you are going to develop an enterprise application and want to break rule 6 and use an automatic testing tool, which tool should you choose? Microsoft has developed a stress tool for Web-based applications that can be used for testing performance during peak times, but how do you test your Windows-form–based applications for both performance and logic?

There is a freeware tool on the market called NUnit that helps you to do unit tests and code tests automatically. When we say "automatically," we mean that you first need to set up a bunch of attributes in your classes that should be tested, specify a reference to the profiling DLL, and finally run the utility. NUnit is written entirely in C# and can be found at http://nunit.org/.

NUnit works with custom attributes on test methods that are used to test specific units. To enable a class for automatic unit testing, you need to follow the procedure we discuss next.

Earlier in this chapter we used a banking component, as shown in Listing 5-2.

Listing 5-2: The Banking Component
Start example
namespace bank
{
      public class Account
      {
            private float balance;
            public void Withdraw(float amount)
            {
                  balance-=amount;
            }
            public void Deposit(float amount)
            {
                  balance+=amount;
            }
            public void TransferFunds(Account destination, float amount)
            {
            }
            public float Balance
            {
                  get{ return balance;}
            }
      }
}
End example

To test this account class, you need to create a test class. Listing 5-3 shows a test class that tests the method TransferFunds to see that it handles the deposit and withdraw correctly.

Listing 5-3: Test Class and Test Method to Test the TransferFunds Method
Start example
namespace bank
{
      using NUnit.Framework;
      [TestFixture]
      public class AccountTest
      {
            [Test]
            public void TransferFunds()
            {
                  Account source = new Account();
                  source.Deposit(100.00F);
                  Account destination = new Account();

                  destination.Deposit(200.00F);
                  source.TransferFunds(destination, 80.00F);
                  Assertion.AssertEquals(220.00F, destination.Balance);
                  Assertion.AssertEquals(80.00F, source.Balance);
            }
      }
}
End example

The test class has an attribute named [TestFixture] that tells NUnit that this is a test class. The methods in the test class that should be used for tests have an attribute named [Test]. The TransferFunds test method simply creates an account object and uses the object as normal. The source code itself is not tampered with in any way.

When you have compiled your test class and your source code, you simply start the NUnit GUI and browse for your DLL. When the DLL is loaded, you will see a test structure on the left-hand side in NUnit. When you run NUnit, all tests that pass will be listed in green text, the failed tests will be listed in red.

To learn more about NUnit, we encourage you to visit the NUnit homepage at http://www.nunit.org. It is quite easy to implement unit test code and run it with NUnit. This is a great tool when you are doing several releases with changes to the code and want to be sure that no new bugs have been introduced.


Team LiB
Previous Section Next Section