NUnitForms 测试GUI应用程序的优秀工具

著名的NUnit是单元测试的优秀工具,可是要在一个测试方法中启动GUI程序,好比Windows Form界面,这比较难作到。NUnitForms就是为解决这个问题产生的,它是NUnit的一个扩展程序,可用于测试Windows Forms 类型的程序。css

首先从NUnitForm网站下载安装程序,地址是 http://nunitforms.sourceforge.net/index.html,并执行安装。html

在VS2010中新增一个测试项目,添加对两个程序集NUnit.Framework和NUnit.NunitForms引用,添加新测试类型:多线程

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

}

若是要显示GUID,则测试类型应该继承于NUnitFormTest, 添加TestFixture特性,再添加一个Test方法:less

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

启动GUI界面

若是您的Visual Studio已经安装了Resharper插件,则能够直接点击被测试方法的签名地方,选择调试或是运行测试,上面的测试方面会显示一个GUI界面,关闭窗体,测试完成。工具

也能够用窗体实例的ShowDialog 方法调出界面,显示为 个model对话框。单元测试

 

引用控件

若是要引用被测试窗体中的控件,命名空间NUnitForms 中有一些以Tester类型结尾的类型可供使用。这些类型继承于ControlTester ,能够用ControlTester 来测试控件,也能够用它的派生类型。测试

以ControlTester类来测试任何控件,能够像这样经过属性的索引来访问它的属性.网站

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

尝试使用FireEvent方法来触发控件的一个事件:ui

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

 

好比,为了引用窗体MyFormName类型中的button1的按钮,能够下面的方法引用此控件:this

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

若是你省略了"formName"参数, NUnitForms将在全部打开的Form中查找控件。

对于Panel控件,要引用它的子控件,可参考下面的写法,以逗号分隔多个名称:

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

若是NUnitForms找不到你的控件, 会抛出一个NoSuchControlException异常. 若是控件的名称没有资格使它成为一个惟一命名的控件, 将会被抛出AmbiguousNameException异常.

对于层层嵌套控件的命名,请参考下面的例子

 

控件的命名
NUnitForms经过控件的Name属性来查找你要测试的控件. 若是在一个form中有多个相同名称的控件, 那么他们必须像下面这样进行限定:

Form
  PanelA
    UserControl1
      Button           (PanelA.UserControl1.Button)  
    UserControl2
      Button           (UserControl2.Button)
  PanelB
    UserControl1
      Button           (PanelB.UserControl1.Button)

 

Model/Modeless Dialog 模式窗体/非模式窗体

当测试窗体时,若是这个窗体要调出子窗体或是调出对话框,这时须要把窗体的测试逻辑放到一个public void签名的方法中,并用ExprectModel指定方法名称:

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

测试时,若是要调出message box,请参考下面的写法

ExpectModal("messageBoxCaption", "messageBoxClickerMethod");

 

多线程测试

若是运用到多线程测试窗体,应该像下面的例子同样,注册一个委托类型,把测试代码放到该方法中

 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)

目前能够下载到的版本是NUnitFormsV2.0.0.5 alpha4。

 

单元测试的目的是改善代码

既然能够调出窗体,就能够测试自定义控件,这是一种测试自定义控件的好方法。

测试项目与Resharper配合起来,很容易启动,调试,修改,这样作单元测试,才是有益于改善代码的测试。

相关文章
相关标签/搜索