• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • NUnit --- 從零開始

    發表于:2008-01-21來源:作者:點擊數: 標簽:nunitNUnit
    NUnit --- 從零開始 既然是從零開始,就先介紹一下NUnit ( http://www.nunit.org ):一個.NET 框架下的單元測試框架,提供了類似于 JUnit的功能,也是 .NET 框架下開發者應用最廣泛的單元測試框架之一(其他的還包括 CSUnit 等等)。 它的基本原理是通過 .

    NUnit --- 從零開始

        既然是從零開始,就先介紹一下NUnit (http://www.nunit.org):一個.NET 框架下的單元測試框架,提供了類似于 JUnit的功能,也是 .NET 框架下開發者應用最廣泛的單元測試框架之一(其他的還包括 CSUnit 等等)。

        它的基本原理是通過 .NET 的反射機制,利用代碼中的元數據(Attribute)來辨識到底有哪些單元測試。單元測試(Unit Test)是測試驅動開發(Test-Driven Development,TDD)很重要的一環,而TDD又是敏捷開發方法(比如極限編程--eXtreme Programming)的重要組成部分…… 總之,單元測試很重要就對了。 ^_^ (有關TDD、XP的詳細介紹在博客園的很多blog上就有,當然 google 上就更多了)

    例子開始:

    1。下載、安裝 NUnit(最新版可能是 2.2.0)

    2。很重要的步驟:測試一下 NUnit 是否安裝成功。

        方法:打開 NUnit,File--Open--選 NUnit 安裝目錄下的bin目錄中的 nunit.tests.dll。這時NUnit 主窗口左部的樹型列表中會出現很多個測試的名字,然后點 Run 按鈕,接著測試就開始運行了,直到 NUnit 主窗口左部的樹型列表中所有的測試前面都變成綠色,那就是成功了。(失敗的測試會有紅色的提示,沒有運行的測試會有黃色的提示。在這一步中,有可能 Console Runner 那個測試集合會出現問題。萬一出現問題,重啟一下 NUnit 再 Run,一般都是沒問題的)




    2。怎么在開發中使用 NUnit 框架?
        1)打開VS.NET 2003,新建一個 C# 的 Console 項目,在項目的 References 添加 nunit.framework(References 在 Solution Explorer 窗口中,右鍵,Add Reference...)

        2)隨便寫一個類
    public class Aclearcase/" target="_blank" >ccount    // 銀行帳戶類
    {
        
    private float balance; // 賬戶的余額

        
    public void Deposit(float amount)    // 存錢
        {
            balance
    +=amount;
        }


        
    public void Withdraw(float amount)    //取錢
        {
            balance
    -=amount;
        }


        
    public void TransferFunds(Account destination, float amount) // 轉賬
        {
            destination.Deposit(amount);
            Withdraw(amount);
        }

     
        
    public float Balance
        
    {
            
    getreturn balance;}
        }

        
    public static void Main(string[] args) 
        
    {
            Account source 
    = new Account();    // 新建個賬戶
            source.Deposit(200.00F);    // 存200
            Account destination = new Account();    // 又建了一個
            destination.Deposit(150.00F);    // 存150
            source.TransferFunds(destination, 100.00F);    // 第一個賬戶轉給第二個100
        }

    }
    這個類很簡單,編譯通過,運行,一切ok。

       3)在同一個項目中,增加一個用來測試 Account 類中的方法的測試類(里面的幾個Attribute是最關鍵的)

    using NUnit.Framework;    // 千萬別忘了這一行

    [TestFixture] 
    // 這個Attribute說明 AccountTest 類中包含有測試
    public class AccountTest
    {
        [Test]    
    // 這個Attribute說明了 TestTransferFunds() 方法就是用來做測試的
        
    // 一般測試方法的名字就是在被測試方法名前加上Test
        public void TestTransferFunds()    
        
    {
            
    // 準備工作
            Account source = new Account();
            source.Deposit(
    200.00F);
            Account destination 
    = new Account();
            destination.Deposit(
    150.00F);

            source.TransferFunds(destination, 
    100.00F);    // 轉賬

            
    // 利用 Nunit.Framework 中的 Assert 類看看轉賬以后兩個賬戶的余額是否正確
            Assert.AreEqual(250.00F, destination.Balance);
            Assert.AreEqual(
    100.00F, source.Balance);
        }

    }

    然后編譯一下,生成一個 exe 文件(如果要生成 DLL 的話,更改一下這個這個項目的 Output Type屬性,改成 Class Library就可以了。這個改動還是在Solution Explorer 窗口中,項目名上 右鍵--屬性。 對于這個例子,生成DLL的話就不需要 Main() 方法了)。

        4)打開NUnit,File--Open,找到剛才編譯生成的 exe。然后 Run,滿眼可愛的綠色,就說明測試都成功了^_^。



        如果想看看測試失敗的樣子,可以把 Assert.AreEqual() 里面的值改一下……

    例子中只用到了 Test Fixture 和 Test 這兩個Attribute,其他更多的用法在 NUnit 文檔中寫得十分清楚,文檔中也有些更好的例子……

    自動化的單元測試有什么用? 答:省時省力。當一個系統需要測試的類/方法 成千上萬時,手工的測試方法(用控制臺打印出信息等等)的效率會比較低。

    總結:NUnit 很好的利用了反射機制,單元測試十分方便。但是對于復雜的對象,寫出低耦合的測試代碼可能有一定難度

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>