Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Unit Testing in .NET : Page 2

You have been given the task of creating some business objects for a new .NET project. The UI has not been created (or designed) yet, so you start coding right away. After creating the first few objects, you decide that maybe you should do some unit testing. This article shows you how.


advertisement
Performing the Tests
To perform the NUnit tests, first compile your project to create a DLL. Then start up the NUnit GUI application. From the File menu choose Open to open the newly created DLL. NUnit reads the assembly and, in a tree view, displays the TestFixture classes and all Test methods, as shown in Figure 1.

 
Figure 1: The NUnit GUI has the test assembly loaded.
To run the test methods, click on the Run button. In this case, all tests passed, as indicated by the grey circles turning green.

Now test the Withdraw method. Add the following method to the AccountTest class:

[Test] public void TestWithdraw() { // Create a new account, deposit $250 Account account = new Account(); account.Deposit(250.00f); // Withdraw $100 account.Withdraw(100.00f); // See if we have $150 in the account Assert.AreEqual(150.00f, account.Balance); }

Now build the solution. Note that you don't have to shut down the NUnit GUI application because it automatically detects changes to the DLL and refreshes the display as necessary. After rebuilding the DLL, a new node shows up in the tree view for the new Test method. After clicking on the Run button again to perform the tests, an error is indicated by the red circles near the test method(s) that failed, as shown in Figure 2.

 
Figure 2: The NUnit GUI shows failed methods with the red circles.
As you can see, you also get a message that shows the expected value and the actual value. Obviously, this error is because you have not implemented the Withdraw method. You can now go back to implement this method and run the tests again. The code for the Withdraw method is:

public void Withdraw(float amount) { this._balance -= amount; }

After rebuilding the DLL and rerunning the tests, all tests should pass. Now you can implement and test the Transfer method. The code for the Transfer method is:



public void Transfer(Account destAccount, float amount) { destAccount.Deposit(amount); this.Withdraw(amount); }

The test method looks something like this:

[Test] public void TestTransfer() { // Create source and destination accounts Account source = new Account(); Account destination = new Account(); // Add some money to both accounts source.Deposit(250.00f); destination.Deposit(100.00f); // Transfer $50 from source to destination source.Transfer(destination, 50.00f); // Check balances in each account Assert.AreEqual(200.00f, source.Balance); Assert.AreEqual(150.00f,destination.Balance; }

Now you can rebuild the assembly and run the tests again. It is possible to execute an individual test method by selecting that method in the tree view prior to clicking on the Run button. Also, if you have more than one TestFixture class in your assembly, you can execute the methods in just one of them by selecting the corresponding node prior to clicking on the Run button.

This is a good overview of the capabilities of the NUnit framework and the NUnit GUI. However, there are still some potential problems with the Account class, one of which is handling insufficient funds. I'll discuss how to handle this with the NUnit framework as I show you some additional features.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date