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,
學習資源
對于NUnitForms,仍然是沒有發現有好的中文資源,大家可以參考官方文檔。
四.NUnitAsp
NUnitAsp可以說是NUnitForms的兄弟,它也是一個NUnit的擴展,用來自動測試ASP.NET頁面。雖然NunitAsp可以完成一些ASP.NET頁面的自動化測試工作,但是在編寫測試用例的時候,如果界面上的元素比較多,編寫起來會非常的麻煩,這也是為什么NunitAsp一直處于大紫不紅的原因。它可以編寫如下代碼片斷的測試代碼: [Page]
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();
}
學習資源
跟自己的兄弟NUnitForms一樣,仍然沒有好的中文文檔,有興趣的朋友可以參考‘NUnitAsp的官方文檔,相對來說還是比較全的,有很多的教程。
總結
對于單元測試工具,就簡單的介紹這么多,我個人還是推薦使用TestDriven.Net的個人版,至于后面兩個NUnit的擴展,大家可以參考一下,在實際開發中用它們來測試會很麻煩,至少我目前不敢去做這個嘗試。還有一個非常值得推薦的單元測試工具Mbunit,有興趣的朋友可以關注一下,我對它了解并不多。
評論:
頁面測試,可以關注一下 watiN,他可控制到 dialog/frame/js的alert/confirm 的測試。
http://watin.sourceforge.net/
單元測試的部份,我們是用 NUnitLite。
http://www.codeplex.com/NUnitLite
功能少nunit很多-_-,但他可以測試 web site project 里app_code里的cs。
符合工作的需求,也可參考看看。
對于Mobile的單元測試工具,恐怕很難找,因為netcf對于反射支持有限,而像NUnit這樣的工具也用到了反射。最典型的例子就是NDoc,這個對netcf有的時候就愛莫能助了。
ReSharper UnitRun
\"is a free add-in for Microsoft Visual Studio 2005 that allows you to automatically run and profile unit tests. This user-friendly tool detects test fixtures of the supported unit testing frameworks and lets you run or profile them right from the code editor or from Visual Studio\'s Solution Explorer. \"
免費但不是開源的...
http://www.jetbrains.com/unitrun/
VS2007 將支持Mobile 系統的 unit test, 請大家關注。
另外,VS2005 database professsional 版本已經發布,支持SQL的單元測試,請大家試用。
文章來源于領測軟件測試網 http://www.kjueaiud.com/