Monday, June 20, 2005

NUnit and NUnitAsp Demo.

Last Friday I introduced NUnit as a unit testing tool to my team. It was a one and a half hours demonstration. And I also introduced NUnitAsp an extension to the NUnit Framework which is used to Unit Test ASP.Net User Interfaces.

The NUnitAsp uses an HttpClient to download the output of an ASP.Net page and runs unit testing on the page using Tester controls.

NUnitAsp contains Tester controls for the controls listed below found in the namespace System.Web.UI.WebControls
Button
CheckBox
DataGrid
DropDownList
ImageButton
Label
LinkButton
ListBox
Panel
RadioButton
TextBox
UserControl
ValidationSummary

and two Testers for the controls in the System.Web.UI.HtmlControls namespace
HtmlAnchor
HtmlInputCheckBox

I give below a simple sample which I demonstrated to my team, but I assume that you're familiar with unit testing using NUnit GUI or console based version.

The first thing is to create your unit testing application and adding a reference to nunit.framework.dll and NUnitAsp.dll assembly files
which enables you to use the following using directives.

using NUnit.Framework;
using NUnit.Extensions.Asp;
using NUnit.Extensions.Asp.AspTester;
using NUnit.Extensions.Asp.HtmlTester;


the next step is to create a test fixture which enables you to define your tests.
but you don't have to mark the class with the TestFixture attribute as in NUnit.

public class ASPUITester:WebFormTestCase
{
//Implementation ommitted.

}


the next step is to create your test methods. Always remember to mark your test methods with the Test attribute, otherwise Nunit framework would not be informed to test the particular method.


[Test()]
public void TestTransfer()
{
// Implementation omitted.
}


After creating a Tester object for a control on your web form you can manipulate the properties of the control using the Tester Control. Before start testing you must load the web form you want to the Browser object.

Browser.GetPage(@"http://localhost/AccountsManagerWebUI/TransactionManager.aspx");

Ex: If you want to set the Text property of a text box control you do the following steps.
Create an instance of the TextBoxTester class you'll have to provide the TextBoxes id as a constructor parameter.
Set the Text Property of the TextBoxTester instance to the value you wish.

TextBoxTester textBoxTester = new TextBoxTester("transferAmountTextBox",CurrentWebForm);

ButtonTester buttonTester = new ButtonTester("transferButton",CurrentWebForm);

LabelTester labelTester = new LabelTester("resultLabel",CurrentWebForm);

textBoxTester.Text = "500";

You can then call methods of the Tester objects to perform actions such as button click to perform post back of the WebForm.

//Click the button
buttonTester.Click();
//Assert the result.
AssertEquals("Result is incorrect",labelTester.Text,"500");

Another thing to note is that NUnitAsp does not use [SetUp()] and [TearDown()] attributes to define pre-test and post-test methods, rather we override the SetUp() and TearDown() methods.

protected override void SetUp()
{
textBoxTester = new TextBoxTester("transferAmountTextBox",CurrentWebForm);
buttonTester = new ButtonTester("transferButton",CurrentWebForm);
labelTester = new LabelTester("resultLabel",CurrentWebForm);
}

There are tester controls for most of the ASP.Net controls provided with VS.Net, even the DataGrid.
Most of the simple UI's could be unit tested using NUnitAsp. But currently NUnitAsp does not support frames, JavaScripts, Pop-up windows and a number of HtmlControls provided with VS.Net.
The main idea behind using NUnitAsp for UI testing is testing the code-behind logic.
You can find NUnitAsp at http://nunitasp.sourceforge.net/ and NUnit at http://www.nunit.org/

Tuesday, June 07, 2005

Now an MCAD

It feels great to announce that I got through my 070-320 examination yesterday with 824/1000, which makes me an MCAD. I'm going to pause my process of getting MCSD for a two months period since I have to submit my BIT project documentation to the University of Colombo School of Computing.
I'm using C#.Net to implement my project and I use some other things as EAB, FxCop, NUnit, NHibernate as supporting tools.

We missed a meeting with Ingo Rammer

I found out that Ingo Rammer who is an expert in the area of distributed application design and development has visited Sri Lanka on the 17th of May. I read this news when I was reading Buddhike's blog. I left a comment on his blog asking to inform us at the Sri Lankan .Net user group since we could have arranged a special meeting to meet this geek from Austria.
Ingo Rammer is the author of the award winning book "Advanced .Net Remoting" and he's the co-founder of Thinktecture.
And I'm happy since Ingo says that he will definitely try to arrange a meeting with the Sri Lankan .net User group during his next visit.
Thanks Ingo & Buddhike. And wish you all the best with your new Job with the thinktects Buddhike.