Recently, I blogged about using NUnit and NUnitForms to test VS 2005 Windows Forms applications. I pointed you all at the NUnitForms website and promised to post more about getting started with NUnitForms.
After installing NUnitForms, you can use NUnitForms to make unit tests for a form or to make end to end scenario tests on an entire GUI application. The documentation for each class is adequate (usually installed under %ProgramFiles%\NUnitForms\NUnitForms v2.0 alpha5\Documentation.chm) but the documentation for getting started isn’t very detailed. Here’s an excerpt from the introduction on the NUnitForms homepage:
“NUnitForms is an NUnit extension for unit and acceptance testing of Windows Forms applications.
Now it is easy to write automated tests for your Windows Forms classes.
Your NUnit tests can open a window and interact with the controls. Your tests will automatically manipulate and verify the properties of the gui. NUnitForms takes care of cleaning up your forms between tests, detecting and handling modal dialog boxes, and verifying that your expectations for the test are fulfilled.”
The Documentation link’s overview on the website is out of date since it is for version 1 which doesn’t work with VS 2005. We on the Solutions team are using version 2.0 (alpha 5). The basic classes are the same as version 1 but, unfortunately, the recorder function doesn’t work correctly.
Things you need to know:
using NUnit.Framework;
using NUnit.Extensions.Forms;
…
Namespace yourTestSuiteNameSpace
{
[TestFixture]
public class myGUITests : NUnitFormTest
ButtonTester buttonTester = new ButtonTester("button1", "MyFormName");
CheckBoxTester uncheckBoxTester = new CheckBoxTester( "aPanelName.checkBoxName", "MyFormName");
Or
RadioButtonTester radioTester = new RadioButtonTester("mainFormControlName.panelName.radioButtonName", "MyFormName");
Hint: When you use checkboxes, use the Click method to check or uncheck the box. Using the test classes Checked attribute doesn’t trigger the event.
[Test]
public void TestOKButtonTest()
ExpectModal("FormName", "formNameHandler");
FormName form = new FormName();
form.ShowDialog();
public void formNameHandler ()
ButtonTester buttonTester = new ButtonTester("okButton", " FormName");
// Check the OK button's text and then click it
Assert.AreEqual("OK", buttonTester.Text, "FormName’s OK button text is wrong '" + buttonTester.Text + "'");
buttonTester.Click();
}
ExpectModal("messageBoxCaption", "messageBoxClickerMethod");
public void genericFormHandler()
// Do nothing in this method!
public void MainFormTest()
MainGUIForm mainForm = new MainGUIForm();
mainForm.OnFormReady += new EventHandler<EventArgs> (mainFormTestLogic);
ExpectModal("MainGUIForm", "genericFormHandler");
mainForm.ShowDialog();
public void mainFormTestLogic (object sender, EventArgs e)