领测软件测试网
MILY: 宋体">
三.NunitForms
NUnitForms从命名上看,就知道它跟NUnit有关,没错,它是NUnit的一个WinFrom的扩展。它为Windows Forms应用程序提供单元测试和压力测试,可以非常容易的用它为你的Windows Forms类进行自动化测试,它提供了一个Recorder Application,来记录你的操作。我们编写类似于如下代码片断的测试代码:
ButtonTester button = new ButtonTester("buttonName", "formName");

ControlTester textBox = new ControlTester("nameOfSomeTextBox");

Assertion.AssertEquals("defaultText", textBox["Text"];

textBox["text"] = "newText";
或者类似于这样的代码进行操作记录:
//records button.Click()
public void Click(object sender, EventArgs args)


{
listener.FireEvent(TesterType, sender, "Click");
}

//records: comboBox.Enter("text");
public void TextChanged(object sender, System.EventArgs e)


{
listener.FireEvent(TesterType, sender, "Enter", ((ComboBox)sender).Text);
}

//records: comboBox.Select(3); //text of item 3
public void SelectedIndexChanged(object sender, System.EventArgs e)


{
EventAction action = new EventAction("Select", ((ComboBox)sender).SelectedIndex);
action.Comment = ((ComboBox)sender).Text;
listener.FireEvent(TesterType, sender, action);
}
对于NUnitForms,它还有一个兄弟工具叫NUnitASP,
官方主页:http://nunitforms.sourceforge.net/
学习资源
对于NUnitForms,仍然是没有发现有好的中文资源,大家可以参考官方文档。
四.NUnitAsp
NUnitAsp可以说是NUnitForms的兄弟,它也是一个NUnit的扩展,用来自动测试ASP.NET页面。虽然NunitAsp可以完成一些ASP.NET页面的自动化测试工作,但是在编写测试用例的时候,如果界面上的元素比较多,编写起来会非常的麻烦,这也是为什么NunitAsp一直处于大紫不红的原因。它可以编写如下代码片断的测试代码:
public void TestLayout()


{
TextBoxTester name = new TextBoxTester("name", CurrentWebForm);
TextBoxTester comments = new TextBoxTester("comments", CurrentWebForm);
ButtonTester save = new ButtonTester("save", CurrentWebForm);
DataGridTester book = new DataGridTester("book", CurrentWebForm);

Browser.GetPage("http://localhost/GuestBook/GuestBook.aspx");

AssertVisibility(name, true);
AssertVisibility(comments, true);
AssertVisibility(save, true);
AssertVisibility(book, false);
}

public void TestSave()


{
TextBoxTester name = new TextBoxTester("name", CurrentWebForm);
TextBoxTester comments = new TextBoxTester("comments", CurrentWebForm);
ButtonTester save = new ButtonTester("save", CurrentWebForm);
DataGridTester book = new DataGridTester("book", CurrentWebForm);

Browser.GetPage("http://localhost/GuestBook/GuestBook.aspx");

name.Text = "Dr. Seuss";
comments.Text = "One Guest, Two Guest! Guest Book, Best Book!";
save.Click();
}
官方主页:http://nunitasp.sourceforge.net/index.html
学习资源
跟自己的兄弟NUnitForms一样,仍然没有好的中文文档,有兴趣的朋友可以参考‘NUnitAsp的官方文档,相对来说还是比较全的,有很多的教程。
总结
对于单元测试工具,就简单的介绍这么多,我个人还是推荐使用TestDriven.Net的个人版,至于后面两个NUnit的扩展,大家可以参考一下,在实际开发中用它们来测试会很麻烦,至少我目前不敢去做这个尝试。还有一个非常值得推荐的单元测试工具Mbunit,有兴趣的朋友可以关注一下,我对它了解并不多。
本来想在文章中再介绍一下开源的Mock框架的,感觉太多了,只好单独放一篇文章来介绍了。
文章来源于领测软件测试网 https://www.ltesting.net/