码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-05 23:08:51      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

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

}

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

[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方法来触发控件的一个事件:

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

 

比如,为了引用窗体MyFormName类型中的button1的按钮,可以下面的方法引用此控件:

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配合起来,很容易启动,调试,修改,这样做单元测试,才是有益于改善代码的测试。

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

标签:

原文地址:http://www.cnblogs.com/zxktxj/p/4276121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!