您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > Nunit
在VS2005利用NUnit進行測試
作者:網絡轉載 發(fā)布時間:[ 2013/3/21 15:28:56 ] 推薦標簽:

  TestFixtureTearDown:標記在類中所有測試方法執(zhí)行之后再執(zhí)行的方法。在NUnit 2.5之前只能在類中將此標記多使用于一個實例方法,在NUnit 2.5之后則可以標記多個方法,而且不限于實例方法還可以用于靜態(tài)方法。
  Timeout:標記被測試的方法大的執(zhí)行時間,如果超出標記的時間,則會被取消執(zhí)行并且被標記為測試失敗。
  Values:標記作為測試方法的一系列的參數。前面的代碼實例中有用法實例。
NUnit的斷言(Assertions)
  斷言是所有基于xUnit單元測試系列的核心,NUnit通過NUnit.Framework.Assert類提供了豐富的斷言。具體說來,NUnit總共提供了11個類別的斷言,它們是:
  Equality Asserts:用于斷言對象是否相等方面的斷言,主要表現(xiàn)為兩個方法的重載:Assert.AreEqual()和Assert.AreNotEqual()兩種形式的重載,重載參數包括了常見的基本數值類型(int/float/double等)和引用類型(表現(xiàn)為使用object作為參數).
  Identity Asserts:用于判斷引用類型的對象是否是同一個引用的斷言及斷言對象是否存在于某個集合中,如Assert.AreSame、Assert.AreNotSame及Assert.Contains。
  Condition Asserts:用于某些條件的斷言,如:Assert.IsTrue、Assert.True、Assert.IsFalse、Assert.False、Assert.IsNull、Assert.Null、Assert.IsNotNull、Assert.NotNull、Assert.IsNaN、Assert.IsEmpty及Assert.IsNotEmpty。
  Comparisons Asserts:用于數值及實現(xiàn)了IComparable接口的類型之間的斷言,如Assert.Greater(大于)、Assert.GreaterOrEqual(大于或等于)、Assert.Less(小于)、Assert.LessOrEqual(小于或等于)。
  Type Asserts:用于類型之間的判斷,比如判斷某個實例是否是某一類型或者是從某個類型繼承,如:Assert.IsInstanceOfType、Assert.IsNotInstanceOfType、Assert.IsAssignableFrom、Assert.IsNotAssignableFrom。在NUnit 2.5之后增加了泛型方法,如Assert.IsInstanceOf<T>、Assert.IsNotInstanceOf<T>、Assert.IsAssignableFrom<T>、Assert.IsNotAssignableFrom<T>。。
  Exception Asserts:有關異常方面的斷言,如Assert.Throws/Assert.Throws<T>、Assert.DoesNotThrow、Assert.Catch/Assert.Catch<T>。
  Utility Methods:用于精確控制測試過程,總共有四個方法,分別是:Assert.Pass、Assert.Fail、Assert.Ignore、Assert.Inconclusive。Assert.Pass和Assert.Fail是相反的,前者是表示將立即終止測試并將測試結果標識為成功通過測試,后者是立即終止測試并將測試結果標識為測試失敗。Assert.Ignore表示忽略測試,這個標記可以用于標識測試方法或者測試的類。
  StringAssert:用于字符串方面的斷言,提供的方法有StringAssert.Contains、StringAssert.StartsWith、StringAssert.EndsWith、StringAssert.AreEqualIgnoringCase及StringAssert.IsMatch。
  CollectionAssert:關于集合方面的斷言,提供的方法有CollectionAssert.AllItemsAreInstancesOfType、CollectionAssert.AllItemsAreNotNull、CollectionAssert.AllItemsAreUnique、CollectionAssert.AreEqual、CollectionAssert.AreEquivalent、CollectionAssert.AreNotEqual、CollectionAssert.AreNotEquivalent、CollectionAssert.Contains、CollectionAssert.DoesNotContain、CollectionAssert.IsSubsetOf、CollectionAssert.IsNotSubsetOf、CollectionAssert.IsEmpty、CollectionAssert.IsNotEmpty和CollectionAssert.IsOrdered。
  FileAssert:用于文件相關的斷言,主要提供兩個方法:FileAssert.AreEqual和FileAssert.AreNotEqual。
  DirectoryAssert:用于文件夾的斷言,提供的方法有:DirectoryAssert.AreEqual、DirectoryAssert.AreNotEqual、DirectoryAssert.IsEmpty、DirectoryAssert.IsNotEmpty、DirectoryAssert.IsWithin和DirectoryAssert.IsNotWithin。
NUnit的使用
  第一次打開NUnit時會是一個空白界面,如下圖所示:

  首先我們需要創(chuàng)建一個NUnit項目,點擊[File]->[New Project]會彈出一個保存NUnit項目的對話框,選擇合適的路徑并輸入合適的名稱(注意文件后綴名為.nunit),然后點擊保存按鈕,這樣創(chuàng)建了一個NUnit測試項目。以后我們可以再次打開這個項目了。
  此時這個NUnit項目中還不包含任何單元測試用例,我們需要創(chuàng)建包含測試用例的項目。打開Visual Studio創(chuàng)建一個類庫項目(在真實項目中通常做法是向當前解決方案中添加類庫項目,這樣便于解決dll引用問題),接著我們需要添加NUnit的引用,這取決于我們是采用安裝方式還是免安裝方式,通常情況下我們只需要添加對nunit.framework(對應的dll是unit.framework.dll)的引用夠了。
  這里周公采用的示例代碼如下:

[csharp] view plaincopy

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using NUnit.Framework; 
     
    namespace UnitTestDemo 
    { 
        [TestFixture] 
        public class NUnitTestDemo 
        { 
            private IList<int> intList = new List<int>(); 
     
            [SetUp] 
            [Category("NA")] 
            public void BeforeTest() 
            { Console.WriteLine("BeforeTest"); } 
     
            [TestFixtureSetUp] 
            [Category("NA")] 
            public void BeforeAllTests() 
            { Console.WriteLine("BeforeAllTests"); } 
     
            [TearDown] 
            [Category("NA")] 
            public void AfterTest() 
            { Console.WriteLine("AfterTest"); } 
     
            [TestFixtureTearDown] 
            [Category("NA")] 
            public void AfterAllTests() 
            { Console.WriteLine("AfterAllTests"); } 
     
            [Test] 
            [Category("NA")] 
            public void Test1() 
            { Console.WriteLine("Test1"); } 
     
            [Test] 
            [Category("NA")] 
            public void Test2() 
            { Console.WriteLine("Test2"); } 
     
            [Test] 
            public void TestFloat() 
            { 
                float value = 0.9999999999999999999999999999f; 
                //value = 0.9999999999999999999999999999; 
                Console.WriteLine("float value:" + value); 
                Assert.AreEqual(value, 1f); 
                Console.WriteLine("TestFloat");  
            } 
     
            [Test] 
            public void TestDouble() 
            { 
                double value = 0.9999999999999999999999999999d; 
                Console.WriteLine("double value:" + value);  
                Assert.AreEqual(value, 1d); 
                Console.WriteLine("Test2");  
            } 
     
            [Test] 
            public void TestDecimal() 
            { 
                decimal value = 0.9999999999999999999999999999M; 
                Console.WriteLine("decimal value:" + value);  
                Assert.AreEqual(value, 1M); 
                Console.WriteLine("Test2");  
            } 
     
            [Test,Repeat(3)] 
            public void TestIntList2() 
            { 
                Assert.AreEqual(0, intList.Count); 
            } 
     
            [Test] 
            public void TestIntList1() 
            { 
                intList.Add(1); 
                Assert.AreEqual(1, intList.Count); 
            } 
     
            [TestCase(12, 3, 4)] 
            [TestCase(12, 2, 6)] 
            [TestCase(12, 4, 3)] 
            public void DivideTest(int n, int d, int q) 
            { 
                Assert.AreEqual(q, n / d); 
            } 
     
            [Test, Combinatorial,Description("This is used for show Combinatorial")] 
            public void MyTest( 
            [Values(1, 2, 3)] int x, 
            [Values("A", "B")] string s) 
            { 
                string value = x + s; 
                Assert.Greater(2, value.Length); 
            } 
     
            [Test] 
            public void TestDemo1( 
            [Values(1, 2, 3)] int x, 
            [Random(-10,10,2)] int y) 
            { 
                Assert.Greater(x + y, 0); 
            } 
     
            [Test] 
            public void TestDemo2( 
            [Range(0, 11, 4)] int x) 
            { 
                Assert.AreEqual(x%3,0); 
            } 
        } 
    } 

上一頁123下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd