I found out this to be interesting. So I gave it a try.
You can download NUnitASP from here http://nunitasp.sourceforge.net/index.html.
Basically it extends from NUnit Framework.
In order to use NUnitASP for your asp.net unit testing, you need both NUnitASP and
NUnit to be available on your machine.
I created a simple website here
http://community.sgdotnet.org/photos/chuawenching/category1106/picture21368.aspx
The tutorial in the original website is quite helpful, but I fail to get something to
work. More to research. If anyone know how to use this, please let me know yeah :)
[Test]
public void TestPassingArrayOfUsers()
{RegisterNow("wenching", "haha", "haha");// How to get this to work?
string [][] users = new string [][]
{new string [] { "ultraman", "ultra", "ultra" },new string [] { "spiderman", "spider", "spider" }};
AssertEquals("dgResults", users, regResults.TrimmedCells);}
Error which I obtained from NUnit-GUI:
SimpleRegisterTests.RegisterTest.TestPassingArrayOfUsers : dgResults
expected:
{
{"ultraman", "ultra", "ultra"}
{"spiderman", "spider", "spider"}
}
but was:
{
{"wenching", "haha", "haha"}
}
And if i removed this line of code - RegisterNow("wenching", "haha", "haha");I will get this error:
SimpleRegisterTests.RegisterTest.TestPassingArrayOfUsers :
NUnit.Extensions.Asp.HtmlTag+ElementNotVisibleException : Couldn't find 'dgResults' on ''
I think this error makes sense since that datagrid is not loaded until there is a data inserted to it.
Anyway, I had attached my full source code here:
RegisterTest.cs
using System;
using NUnit.Framework;
using NUnit.Extensions.Asp;
using NUnit.Extensions.Asp.AspTester;
namespace SimpleRegisterTests
{[TestFixture]
public class RegisterTest : WebFormTestCase
{private TextBoxTester regName;
private TextBoxTester regPassword;
private TextBoxTester regNewPassword;
private ButtonTester regSave;
private ButtonTester regCancel;
private DataGridTester regResults;
public RegisterTest()
{//
// TODO: Add constructor logic here
//
}
// Notice:
// NUnitASP already uses the [SetUp] attribute, so you are only allowed
// to use this way
// Purpose:
// Load this before running any test - a way of refactoring
protected override void SetUp()
{regName = new TextBoxTester("txtName", CurrentWebForm);regPassword = new TextBoxTester("txtPassword", CurrentWebForm);regNewPassword = new TextBoxTester("txtNewPassword", CurrentWebForm);regSave = new ButtonTester("btnSave", CurrentWebForm);regCancel = new ButtonTester("btnCancel", CurrentWebForm);regResults = new DataGridTester("dgResults", CurrentWebForm);// Check the URL exists or not
Browser.GetPage("http://localhost/SimpleASPTest/Register.aspx");}
[Test]
public void TestVisibiltyControls()
{AssertVisibility(regName, true);
AssertVisibility(regPassword, true);
AssertVisibility(regNewPassword, true);
AssertVisibility(regSave, true);
AssertVisibility(regCancel, true);
// Cannot be true, as you need to enter data before the datagrid
// can display anything
// Error: SimpleRegisterTests.RegisterTest.TestVisibiltyControls :
// dgResults control should be visible (HTML ID: dgResults; ASP
// location: DataGridTester 'dgResults' in web form 'Form1')
AssertVisibility(regResults, false);
}
[Test]
public void TestSaveNewUser()
{RegisterNow("wenching", "haha", "haha");RegisterNow("nicholas", "necoders", "necoders");// The Compare Validator is not supported as it is not
// checked during click event
// RegisterNow("jason", "huh", "heh");}
[Test]
public void TestPassingArrayOfUsers()
{//RegisterNow("wenching", "haha", "haha");string [][] users = new string [][]
{new string [] { "ultraman", "ultra", "ultra" },new string [] { "spiderman", "spider", "spider" }};
AssertEquals("dgResults", users, regResults.TrimmedCells);}
[Test]
public void TestCancel()
{// Call the Cancel OnClick event
regCancel.Click();
// Make sure it empties the text boxes
AssertEquals("txtName", "", regName.Text);AssertEquals("txtPassword", "", regPassword.Text);AssertEquals("txtNewPasssword", "", regNewPassword.Text);}
private void RegisterNow(string strName, string strPassword, string strNewPassword)
{regName.Text = strName;
regPassword.Text = strPassword;
regNewPassword.Text = strNewPassword;
// Manually have to add OnClick event in the html section
regSave.Click();
}
}
}
Register.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace SimpleASPTest
{/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class Register : System.Web.UI.Page
{protected System.Web.UI.WebControls.Button btnSave;
protected System.Web.UI.WebControls.Button btnCancel;
protected System.Web.UI.WebControls.TextBox txtNewPassword;
protected System.Web.UI.WebControls.DataGrid dgResults;
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.Label lblOutput;
protected System.Web.UI.WebControls.CompareValidator cvPassword;
protected System.Web.UI.WebControls.TextBox txtPassword;
private void Page_Load(object sender, System.EventArgs e)
{// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{ this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
// cannot be private here
protected void btnSave_Click(object sender, System.EventArgs e)
{DataTable resultsTable = (DataTable) Session["Registration"];
if (resultsTable == null)
{resultsTable = new DataTable();
resultsTable.Columns.Add(new DataColumn("Name", typeof(string)));resultsTable.Columns.Add(new DataColumn("Password", typeof(string)));resultsTable.Columns.Add(new DataColumn("New Password", typeof(string)));}
DataRow resultsRow = resultsTable.NewRow();
resultsRow["Name"] = txtName.Text;
resultsRow["Password"] = txtPassword.Text;
resultsRow["New Password"] = txtNewPassword.Text;
resultsTable.Rows.Add(resultsRow);
dgResults.DataSource = resultsTable;
dgResults.DataBind();
Session["Registration"] = resultsTable;
txtName.Text = "";
txtPassword.Text = "";
txtNewPassword.Text = "";
}
// cannot be private here
protected void btnCancel_Click(object sender, System.EventArgs e)
{lblOutput.Text = "Registration Cancelled";
txtName.Text = "";
txtPassword.Text = "";
txtNewPassword.Text = "";
}
}
}