zoukankan      html  css  js  c++  java
  • NUnitForms 测试GUI应用程序的优秀工具

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

  • 相关阅读:
    SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问
    谷歌浏览器扩展程序manifest.json参数详解
    获取天气api
    UVA 10385 Duathlon
    UVA 10668 Expanding Rods
    UVALIVE 3891 The Teacher's Side of Math
    UVA 11149 Power of Matrix
    UVA 10655 Contemplation! Algebra
    UVA 11210 Chinese Mahjong
    UVA 11384 Help is needed for Dexter
  • 原文地址:https://www.cnblogs.com/zxktxj/p/4276121.html
Copyright © 2011-2022 走看看