著名的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
… }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
如果要显示GUID,则测试类型应该继承于NUnitFormTest, 添加TestFixture特性,再添加一个Test方法:
[Test]
pubilc void ShowDilalogTest()
{
Form dlg=new Form();
dlg.Show();
}
启动GUI界面
如果您的Visual Studio已经安装了Resharper插件,则可以直接点击被测试方法的签名地方,选择调试或是运行测试,上面的测试方面会显示一个GUI界面,关闭窗体,测试完成。
也可以用窗体实例的ShowDialog 方法调出界面,显示为
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }个model对话框。
引用控件
如果要引用被测试窗体中的控件,命名空间NUnitForms 中有一些以Tester类型结尾的类型可供使用。这些类型继承于ControlTester ,可以用ControlTester 来测试控件,也可以用它的派生类型。
以ControlTester类来测试任何控件,可以像这样通过属性的索引来访问它的属性.
ControlTester textBox = new ControlTester("nameOfSomeTextBox");
Assertion.AssertEquals("defaultText", textBox["Text"]);
textBox["text"] = "newText";
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
尝试使用FireEvent方法来触发控件的一个事件:
ControlTester button = new ControlTester("nameOfSomeButton");
button.FireEvent("Click");
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
比如,为了引用窗体MyFormName类型中的button1的按钮,可以下面的方法引用此控件:
ButtonTester buttonTester = new ButtonTester("button1", "MyFormName");
如果你省略了"formName"参数, NUnitForms将在所有打开的Form中查找控件。
对于Panel控件,要引用它的子控件,可参考下面的写法,以逗号分隔多个名称:
CheckBoxTester uncheckBoxTester = new CheckBoxTester( "aPanelName.checkBoxName", "MyFormName");
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
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();
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
测试时,如果要调出message box,请参考下面的写法
ExpectModal("messageBoxCaption", "messageBoxClickerMethod");
多线程测试
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
如果运用到多线程测试窗体,应该像下面的例子一样,注册一个委托类型,把测试代码放到该方法中
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)
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
目前可以下载到的版本是NUnitFormsV2.0.0.5 alpha4。
单元测试的目的是改善代码
既然可以调出窗体,就可以测试自定义控件,这是一种测试自定义控件的好方法。
测试项目与Resharper配合起来,很容易启动,调试,修改,这样做单元测试,才是有益于改善代码的测试。