Nunit2.5.10快速上手(筆記)

一、下載Nunit:http://www.nunit.org/index.php?p=download,下載MSI格式的安裝包;php

二、安裝Nunit,根據提示安裝便可,沒有什麼須要配置的,直接下一步就能夠了。html

三、新建類庫項目NUnitQuickStart,在該項目添加引用nunit.framework。正則表達式

四、新建類NumbersFixture,添加以下代碼:app

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
 
namespace NUnitQuickStart
{
    [TestFixture]
    public class NumbersFixture
    {
        [Test]
        public void Sum()
        {
            int a=1;
            int b=2;
            int sum = a + b;
            Assert.AreEqual(3,sum);
        }
    }
}

五、右擊該類庫項目,選擇屬性,打開以後單擊調試,選擇啓動外部程序,單擊帶……的按鈕,在打開新的對話框以後,選擇安裝Nunit目錄下的C:\Program Files\NUnit 2.5.10\bin\net-2.0\nunit.exe這是個人安裝目錄。選擇好以後,單擊vs中的調試按鈕,便可調試。vs會自動打開Nunit,這時Nunit什麼也沒顯示,選擇File,Open File打開你的項目,而後單擊Run按鈕便可進行測試。測試

六、根據測試計劃,使用Assert下的各個方法對項目中的單元方法進行各項測試。ui

6.1 Equality Asserts,用來斷定測試結果是否與預期一致this

       公式: Assert.AreEqual( object expected, object actual );spa

       例子:NUnit.Framework.Assert.AreEqual(10, vTestResult),其中vTestResult爲一個變量,它能夠經過運行指定的測試狀況來獲取相應的值。調試

       特殊狀況:當指定的值需考慮精度的時候,能夠設置容錯的範圍。code

       Assert.AreEqual( double expected, double actual, double tolerance );

       Assert.AreEqual( 10.9F, 10.1F, 0.8F );能夠正常結束

       Assert.AreEqual( 10.9F, 10.1F, 0.7F );不能正常結束

6.2 Identity Asserts 判斷兩個對象是否相同

       公式: Assert.AreSame( object expected, object actual );//斷定兩個對象是否相同
              Assert.AreNotSame( object expected, object actual );//斷定兩個對象是否不相同
              Assert.Contains( object anObject, IList collection );//斷定一個對象是否包含在一組數據裏
       例子:

       DataTable vDT1 = new DataTable("FirstDT");

       DataTable vDT2 = vDT1.Copy();

       vDT2.TableName = "SecondDT";

       DataTable vDT3 = vDT1;

       ArrayList vList = new ArrayList();

       vList.Add(vDT2);

       NUnit.Framework.Assert.AreSame(vDT1, vDT3);

       NUnit.Framework.Assert.AreNotSame(vDT1, vDT2);

       NUnit.Framework.Assert.Contains(vDT2, vList);

       特殊狀況:當判斷兩個對象是否相同,最好不一樣對象以不一樣的名稱命名。

       若是之上的vDT2.TableName沒有進行命名,並以如下方法斷定,那出錯信息將沒有任何意義。

       NUnit.Framework.Assert.Contains(vDT1, vList);

6.3 Condition Tests 條件斷言

      公式: Assert.IsTrue( bool condition );//判斷執行結果是否爲真
              Assert.IsFalse( bool condition);//判斷執行結果是否爲假
              Assert.IsNull( object anObject );//判斷執行結果是否爲空
              Assert.IsNotNull( object anObject );//判斷執行結果是否爲非空

              Assert.IsNaN( double aDouble );//(Not a Number)的意思,通常只有當aDouble=double.NaN的時候才能實現。

              Assert.IsEmpty( string aString );//判斷指定值是否爲空字符串 ※可是絕對不是Null
              Assert.IsNotEmpty( string aString );//斷定指定值是否爲非空,即有值。
              Assert.IsEmpty( ICollection collection );//斷定全部列表組成是否爲空

                   例:

                         string vstrbychar = "";

                         NUnit.Framework.Assert.IsEmpty(vstrbychar.ToCharArray());

                         string[] vstres = new string[2];

                         NUnit.Framework.Assert.IsEmpty(vstres);

                         ArrayList vList = new ArrayList();

                         NUnit.Framework.Assert.IsEmpty(vList );

              Assert.IsNotEmpty( ICollection collection );//斷定全部列表組成是否爲非空

6.4 Comparisons 比較斷言

       公式:Assert.Greater( int arg1, int arg2 );//判斷arg1比arg2大,arg1爲執行結果,arg2爲預期的範圍;※注意必須是大於,不包含等於。                   

                             本身能夠試試 Assert.Greater(1, 2);執行測試程序,將會出現 Excepted:greater then 2;But was:1的錯誤

              Assert.GreaterOrEqual( int arg1, int arg2 );//判斷arg1大於等於arg2

              Assert.Less( int arg1, int arg2 );//判斷arg1小於arg2

              Assert.LessOrEqual( int arg1, int arg2 );//判斷arg1小於等於arg2

 6.5 Type Asserts 類型斷言

        公式:Assert.IsInstanceOfType( Type expected, object actual );//判斷actual指定實例是否爲expected類型            

                   例:在當前測試類內,實現以下斷言:

                       NUnit.Framework.Assert.IsInstanceOfType(typeof(AccountTest3), this);//AccountTest3爲當前類的類名,this表示當前測試類的實例

               Assert.IsNotInstanceOfType( Type expected, object actual );//判斷actual指定實例不是expected類型

               Assert.IsAssignableFrom( Type expected, object actual );//判斷actual是否由expected類型生成的實例;

                    例:Assert.IsAssignableFrom(typeof(object), "word");//這個斷言將會失敗,由於字符串"word"是string類型,而不是object類型建立的

               Assert.IsNotAssignableFrom( Type expected, object actual );//判斷actual是否不禁expected類型生成的實例;

         

 6.6 Utility Methods 實用方法(主要用來自定義斷言)

       公式:Assert.Fail();//當不使用斷言時,能夠直接使用Fail方法來提交測試失敗

              Assert.Fail( string message );//提交失敗時,顯示失敗的緣由等信息

                  例:if(vPath.Equal("")){Assert.Fail("Path is not right!")}

              Assert.Ignore();//忽略,主要爲了暫時不進行測試或者指定條件能夠不予考慮,可是測試結果會在測試界面以黃色顯示

6.7 StringAssert  字符串斷言

       公式:StringAssert.Contains( string expected, string actual );//指定字符串包含預期的值 ※注意這裏是區分大小寫字母的,也區分全角半角字符

              StringAssert.StartsWith( string expected, string actual );//指定字符串起始於預期的值

              StringAssert.EndsWith( string expected, string actual );//指定字符串以預期的值結束

              StringAssert.AreEqualIgnoringCase( string expected, string actual );//判斷兩個字符串是否相同。※不區分大小寫,可是區分全角和半角

              StringAssert.IsMatch( string expected, string actual );//判斷實際執行的結果是否與預期的格式匹配

                  例:NUnit.Framework.StringAssert.IsMatch(@"^\d{3}[A-Z]{2}", "991XX");

                       //具體正則表達式請參考 http://www.cnblogs.com/si812cn/archive/2008/10/10/1307792.html 

 

6.8 CollectionAssert 列表集斷言

       公式:CollectionAssert.AllItemsAreInstancesOfType( IEnumerable collection,Type expectedType );//斷定全部的列表項都屬於expectedType類型

                例:

                    ArrayList vList2 = new ArrayList();
                    vList2.Add("string");
                    vList2.Add("123");

                    CollectionAssert.AllItemsAreInstancesOfType( vList2, typeof(string) );

                    若是以上的vList2.Add("123")改爲vList2.Add(123);則將測試失敗。

              CollectionAssert.AllItemsAreNotNull( IEnumerable collection );//全部列表項均爲非空

                  例:

                   ArrayList vList3 = new ArrayList();
                    vList3.Add(null);
                    vList3.Add("123");

                    CollectionAssert.AllItemsAreNotNull( vList3);

                    以上測試將失敗,由於第一項爲空。

              CollectionAssert.AllItemsAreUnique( IEnumerable collection );//全部列表項是惟一的 ※區分大小寫,區分全角和半角

                  例:

                    ArrayList vList4= new ArrayList();
                    vList4.Add(1);
                    vList4.Add(2);

                    vList4.Add(1);

                    CollectionAssert.AllItemsAreUnique( vList4);

                    以上測試將失敗,由於第一項與第三項重複出現了。

               CollectionAssert.AreEqual( IEnumerable expected, IEnumerable actual );

                   //按照順序比較兩個列表內各項值是否一致 ※包括類型,大小寫,全半角,NULL也能夠比較

               CollectionAssert.AreEquivalent( IEnumerable expected, IEnumerable actual);

                  //與CollectionAssert.AreEqual不一樣,它能夠是沒有順序的,可是一項只能對應一項,不能一個值與多個相同值比較

               CollectionAssert.AreNotEqual( IEnumerable expected, IEnumerable actual );//兩列不相同

               CollectionAssert.AreNotEquivalent( IEnumerable expected, IEnumerable actual );//兩列不等價

               CollectionAssert.Contains( IEnumerable expected, object actual );//執行結果包含在預期的列表中

               CollectionAssert.DoesNotContain( IEnumerable expected, object actual );//執行結果不被包含在預期的列表中

               CollectionAssert.IsSubsetOf( IEnumerable subset, IEnumerable superset );

                  //subset內的列表項都出如今superset中,則表示subset是superset的子集 ※包含兩個列表集數據數相同

                  例:

                        ArrayList vList7 = new ArrayList();

                        vList7.Add(1);

                        vList7.Add("123");

 

                        ArrayList vList8 = new ArrayList();

                        vList8.Add(1);

                        vList8.Add("123");

                        vList8.Add(null);

                        CollectionAssert.IsSubsetOf( vList7, vList8 );//該斷言將能夠經過,反過來則不能經過

                        若是vList8.Add(null);去除掉
                        CollectionAssert.IsSubsetOf( vList7, vList8 );//該斷言將能夠經過,反過來也能夠經過,由於根據子集的概念,他倆相互均可以是對方的子集

                 CollectionAssert.IsNotSubsetOf( IEnumerable subset, IEnumerable superset);//判斷不是子集

                 CollectionAssert.IsEmpty( IEnumerable collection );//判斷列表值是否爲空值,即已定義,未賦值

                      例:

                           ArrayList vList9 = new ArrayList();

                           CollectionAssert.IsEmpty(vList9);//執行成功

                          ArrayList vList10 = new ArrayList();
                          vList10.Add("");

                         CollectionAssert.IsEmpty(vList10);//執行失敗

6.9 FileAssert  文件斷言

       公式:FileAssert.AreEqual( Stream expected, Stream actual );//判斷兩個文件流一致

              
6.10 Constraint-Based Assert Model 約束斷言模式

       公式:Assert.That( object actual, IConstraint constraint ) //執行結果與指定的約束值對比

               例:

                   Assert.That( myString, Is.EqualTo("Hello") );//Is屬性存在於NUnit.Framework.SyntaxHelpers命名空間內

                   Assert.That( myString, new EqualConstraint("Hello") );

                   Assert.That( 2.3, Is.GreaterThan( 2.0 ) & Is.LessThan( 3.0 ) );//能夠組合使用

       備註:NUnit.Framework.SyntaxHelpers裏包含了IS,Has,Text,List,ListMapper屬性進行一系列的操做。

              .Net中的map類有Dictionary、SortedDictionary、HashTable、SortedList等,SortedDictionary提供了排序支持。

              Has裏主要有Has.Perporty(vName)判斷是否有相應的屬性

                              Has.Count(int),Has.Length(int)

              List Mapper:能夠對列表內每一個項指定預期值

              string[] strings = new string[] { "a", "ab", "abc" };              int[] lengths = new int[] { 1, 2, 3 };              Assert.That(List.Map(strings).Property("Length"), Is.EqualTo(lengths));                 Assert.That(new ListMapper(strings).Property("Length"),Is.EqualTo(lengths));

相關文章
相關標籤/搜索