您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > Nunit
在VS2005利用NUnit進(jìn)行測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/21 15:28:56 ] 推薦標(biāo)簽:

  TestFixtureTearDown:標(biāo)記在類中所有測(cè)試方法執(zhí)行之后再執(zhí)行的方法。在NUnit 2.5之前只能在類中將此標(biāo)記多使用于一個(gè)實(shí)例方法,在NUnit 2.5之后則可以標(biāo)記多個(gè)方法,而且不限于實(shí)例方法還可以用于靜態(tài)方法。
  Timeout:標(biāo)記被測(cè)試的方法大的執(zhí)行時(shí)間,如果超出標(biāo)記的時(shí)間,則會(huì)被取消執(zhí)行并且被標(biāo)記為測(cè)試失敗。
  Values:標(biāo)記作為測(cè)試方法的一系列的參數(shù)。前面的代碼實(shí)例中有用法實(shí)例。
NUnit的斷言(Assertions)
  斷言是所有基于xUnit單元測(cè)試系列的核心,NUnit通過(guò)NUnit.Framework.Assert類提供了豐富的斷言。具體說(shuō)來(lái),NUnit總共提供了11個(gè)類別的斷言,它們是:
  Equality Asserts:用于斷言對(duì)象是否相等方面的斷言,主要表現(xiàn)為兩個(gè)方法的重載:Assert.AreEqual()和Assert.AreNotEqual()兩種形式的重載,重載參數(shù)包括了常見(jiàn)的基本數(shù)值類型(int/float/double等)和引用類型(表現(xiàn)為使用object作為參數(shù)).
  Identity Asserts:用于判斷引用類型的對(duì)象是否是同一個(gè)引用的斷言及斷言對(duì)象是否存在于某個(gè)集合中,如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:用于數(shù)值及實(shí)現(xiàn)了IComparable接口的類型之間的斷言,如Assert.Greater(大于)、Assert.GreaterOrEqual(大于或等于)、Assert.Less(小于)、Assert.LessOrEqual(小于或等于)。
  Type Asserts:用于類型之間的判斷,比如判斷某個(gè)實(shí)例是否是某一類型或者是從某個(gè)類型繼承,如: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:有關(guān)異常方面的斷言,如Assert.Throws/Assert.Throws<T>、Assert.DoesNotThrow、Assert.Catch/Assert.Catch<T>。
  Utility Methods:用于精確控制測(cè)試過(guò)程,總共有四個(gè)方法,分別是:Assert.Pass、Assert.Fail、Assert.Ignore、Assert.Inconclusive。Assert.Pass和Assert.Fail是相反的,前者是表示將立即終止測(cè)試并將測(cè)試結(jié)果標(biāo)識(shí)為成功通過(guò)測(cè)試,后者是立即終止測(cè)試并將測(cè)試結(jié)果標(biāo)識(shí)為測(cè)試失敗。Assert.Ignore表示忽略測(cè)試,這個(gè)標(biāo)記可以用于標(biāo)識(shí)測(cè)試方法或者測(cè)試的類。
  StringAssert:用于字符串方面的斷言,提供的方法有StringAssert.Contains、StringAssert.StartsWith、StringAssert.EndsWith、StringAssert.AreEqualIgnoringCase及StringAssert.IsMatch。
  CollectionAssert:關(guān)于集合方面的斷言,提供的方法有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:用于文件相關(guān)的斷言,主要提供兩個(gè)方法:FileAssert.AreEqual和FileAssert.AreNotEqual。
  DirectoryAssert:用于文件夾的斷言,提供的方法有:DirectoryAssert.AreEqual、DirectoryAssert.AreNotEqual、DirectoryAssert.IsEmpty、DirectoryAssert.IsNotEmpty、DirectoryAssert.IsWithin和DirectoryAssert.IsNotWithin。
NUnit的使用
  第一次打開(kāi)NUnit時(shí)會(huì)是一個(gè)空白界面,如下圖所示:

  首先我們需要?jiǎng)?chuàng)建一個(gè)NUnit項(xiàng)目,點(diǎn)擊[File]->[New Project]會(huì)彈出一個(gè)保存NUnit項(xiàng)目的對(duì)話框,選擇合適的路徑并輸入合適的名稱(注意文件后綴名為.nunit),然后點(diǎn)擊保存按鈕,這樣創(chuàng)建了一個(gè)NUnit測(cè)試項(xiàng)目。以后我們可以再次打開(kāi)這個(gè)項(xiàng)目了。
  此時(shí)這個(gè)NUnit項(xiàng)目中還不包含任何單元測(cè)試用例,我們需要?jiǎng)?chuàng)建包含測(cè)試用例的項(xiàng)目。打開(kāi)Visual Studio創(chuàng)建一個(gè)類庫(kù)項(xiàng)目(在真實(shí)項(xiàng)目中通常做法是向當(dāng)前解決方案中添加類庫(kù)項(xiàng)目,這樣便于解決dll引用問(wèn)題),接著我們需要添加NUnit的引用,這取決于我們是采用安裝方式還是免安裝方式,通常情況下我們只需要添加對(duì)nunit.framework(對(duì)應(yīng)的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); 
            } 
        } 
    } 

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