NUnit and NUnitAsp Demo.
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/
1 Comments:
Hi
I just wanted to know in the following statement that we write in the test:
TextBoxTester name = new TextBoxTester("name", CurrentWebForm);
what is the "CurrentWebForm" used for ? Are there any other things that can be written there.
Post a Comment
<< Home