NUnitForms is an excellent tool for testing GUI applications

Source: Internet
Author: User

The famous NUnit is an excellent tool for unit testing, but it is difficult to start a GUI program, such as the Windows Form interface, in a test method. NUnitForms is generated to solve this problem. It is an extension of NUnit and can be used to test programs of the Windows Forms type.

Download the installation program from the NUnitForm website at http://nunitforms.sourceforge.net/index.html and install the program.

Add a new test project in VS2010, add NUnit. Framework and NUnit. NunitForms references to the two assembly, and add the new test type:

using NUnit.Framework;using NUnit.Extensions.Forms;…Namespace yourTestSuiteNameSpace{    [TestFixture]    public class myGUITests : NUnitFormTest…}

To display GUID, the Test Type should inherit from NUnitFormTest, add the TestFixture feature, and then add a Test method:

[Test]pubilc void ShowDilalogTest(){   Form dlg=new Form();   dlg.Show();}
 
 
Start GUI

If your Visual Studio has installed the Resharper plug-in, you can directly click the signature of the method to be tested and choose to debug or run the test. The preceding test shows a GUI, close the form and the test is complete.

You can also use the ShowDialog method of the form instance to call up the interface, which is displayed

Model dialog box.

 

Reference Control

If you want to reference controls in the tested form, the namespace NUnitForms has some types that end with the Tester type. These types inherit from ControlTester. You can use ControlTester to test the control, or use its derived type.

You can use the ControlTester class to test any control. You can use the attribute index to access its attributes like this.

ControlTester textBox = new ControlTester("nameOfSomeTextBox");Assertion.AssertEquals("defaultText", textBox["Text"]);textBox["text"] = "newText"; 

Try to use the FireEvent method to trigger an event of the control:

ControlTester button = new ControlTester("nameOfSomeButton");button.FireEvent("Click");

 

For example, to reference the button1 button in the form MyFormName type, you can reference this control using the following method:

ButtonTester buttonTester = new ButtonTester("button1", "MyFormName");
 

If you omit the "formName" parameter, NUnitForms searches for controls in all open forms.

For a Panel control, you can refer to the following method to reference its child control. Multiple Names are separated by commas:

 CheckBoxTester uncheckBoxTester = new CheckBoxTester( "aPanelName.checkBoxName", "MyFormName");
 RadioButtonTester radioTester = new RadioButtonTester("mainFormControlName.panelName.radioButtonName",  "MyFormName");

If NUnitForms cannot find your control, a NoSuchControlException exception will be thrown. If the control name is not qualified to make it a unique name control, an AmbiguousNameException will be thrown.

For the naming of layer-by-layer nested controls, refer to the example below

 

Control name

NUnitForms uses the Name attribute of the control to find the control you want to test. If there are multiple controls with the same Name in a form, they must be limited as follows:

Form

PanelA

UserControl1

Button (PanelA. UserControl1.Button)
UserControl2

Button (UserControl2.Button)

PanelB

UserControl1

Button (PanelB. UserControl1.Button)

 

Model/Modeless Dialog mode form/non-mode form

When you test a form, if you want to call up a subform or call up a dialog box, you need to put the test logic of the form into a public void signature method and use ExprectModel to specify the method name:

[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();       }

If you want to call up the message box during testing, refer to the following method.

ExpectModal("messageBoxCaption", "messageBoxClickerMethod");

 

Multithreading Test

If a multi-threaded test form is used, register a delegate type as in the following example and put the test code in this method.

 public void genericFormHandler(){       // Do nothing in this method! }…[Test]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)

The available version is NUnitFormsV2.0.0.5 alpha4.

 

Unit testing aims to improve code

Now that you can call out a form, you can test the custom control. This is a good way to test the custom control.

The test item works with Resharper to easily start, debug, modify, and perform unit tests, which is helpful for code improvement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.