NBear WebTest - 分享一個基於Web的UnitTest工具

簡介

這是一個ASP.NET 3.5的Web Application程序,實現了類似NUnit的簡單但實用的UnitTest功能。寫這個小工具的目的是在NBear5的開發中需要方便的在完全真實的模擬環境中測試所有組件功能的在ASP.NET下,尤其是Partial Trust模式下的運行效果,現有的UnitTest工具中似乎對這方面的支持都比較有限,所以,自己花兩天時間寫了一個。相比NUnit,本工具提供的UnitTest功能比較基礎,但是,對一般的UnitTest來說應該完全夠用了。如果您正在開發和測試一些ASP.NET下的Web組件,推薦一試。程序本身就是一個Web Application,所以,自然是包含了全部源代碼的。源碼對除.Net Framework 3.5之外的DLL沒有任何依賴,也可以做成VS的Project Template方便重複使用。


版權

本文內容及相關代碼遵守BSD開源協議,首發於http://www.cnblogs.com/teddyma/archive/2008/11/10/1330535.html


使用說明

UI界面

運行Default.aspx後,程序員自動列出所有的TestCases:

o_webtest1.jpg

選擇需要運行的TestCases,點擊Run Tests後,TestCases被運行:

o_webtest2.jpg

展開Tree可以看到運行的輸出結果:

o_webtest3.jpg


編寫TestCase

要編寫TestCase只需要在TestCases目錄下新建.cs文件,並給你的測試Class和測試方法分別標註TestMethodAttribute。下面是程序默認附帶的TestClass1類的源代碼,可以看到和NUnit等工具的定義基本類似,SetUpAttribute和CleanUpAttribute僅支持Classs範圍的,也就是說在一個Class中的所有TestMethod被執行的前後分別執行,其他的如TestClassAttribute,TestMethodAttribute和
ExpectedExceptionAttribute等就非常直觀的,隨便猜就能猜到意思了:

 1None.gifusing System;
 2None.gifnamespace NBear.WebTest.TestCases
 3ExpandedBlockStart.gif{
 4InBlock.gif    [TestClass]
 5InBlock.gif    public class TestContractDescriptor
 6ExpandedSubBlockStart.gif    {
 7InBlock.gif        [SetUp]
 8InBlock.gif        public void SetUp()
 9ExpandedSubBlockStart.gif        {
10ExpandedSubBlockEnd.gif        }

11InBlock.gif
12InBlock.gif        [TestMethod]
13InBlock.gif        public void TestMethod1()
14ExpandedSubBlockStart.gif        {
15InBlock.gif            Output.WriteLine("hello world");
16InBlock.gif            Assert.IsTrue(true);
17ExpandedSubBlockEnd.gif        }

18InBlock.gif
19InBlock.gif        [TestMethod]
20InBlock.gif        [ExpectedException(typeof(NotImplementedException))]
21InBlock.gif        public void TestMethod2()
22ExpandedSubBlockStart.gif        {
23InBlock.gif            throw new NotImplementedException();
24ExpandedSubBlockEnd.gif        }

25InBlock.gif
26InBlock.gif        [CleanUp]
27InBlock.gif        public void CleanUp()
28ExpandedSubBlockStart.gif        {
29ExpandedSubBlockEnd.gif        }

30ExpandedSubBlockEnd.gif    }

31ExpandedBlockEnd.gif}

None.gif
Output類用於輸出結果,支持Write(string), WriteLine(string)和WriteNewLine()。

Assert類用於Check,支持如下Check方法:

o_webtest4.jpg

下載地址

打包下載:http://files.cnblogs.com/teddyma/WebTest.zip

SVN下載:http://svn.cnblogs.com:8080/svn/NBear/trunk/src/WebTest/


Enjoy!

//the end


發佈了187 篇原創文章 · 獲贊 1 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章