VS2012 單元測試之泛型類(Generics Unit Test)

關於單元測試,若是不會用能夠參照個人上篇博文————在Visual Studio 2012使用單元測試html

首先分享一篇博文,[Visual Studio] 開啓Visual Studio 2012經過右鍵菜單建立單元測試(Unit Test) ide

泛型有兩種,通常泛型與類型約束泛型,在對包含泛型的方法進行單元測試中也能夠這麼分,詳情可參閱http://msdn.microsoft.com/en-us/library/vstudio/ms243401.aspx  。從該頁面能夠知道,關於泛型的單元測試,微軟類庫(Microsoft.VisualStudio.TestTools.UnitTesting)提供了類「GenericParameterHelper」幫助咱們編寫Unit Test代碼。post

首先看下非類型約束的一個demo,我就直接上代碼了單元測試

        public static bool IsCollectionEmpty<T>(ICollection<T> collection)
        {
            return collection == null || collection.Count < 1;
        }

測試代碼測試

        /// <summary>
        ///IsCollectionEmpty 的測試
        ///</summary>
        public void IsCollectionEmptyTestHelper<T>()
        {
            //三個用例:以非空集合,空集合,null分別做爲參數
            ICollection<T> collection = new T[]{default(T)}; // TODO: 初始化爲適當的值
            bool expected = false; // TODO: 初始化爲適當的值
            bool actual;
            actual = UtilityCheckData.IsCollectionEmpty<T>(collection);
            Assert.AreEqual(expected, actual);

            collection = new T[] { };
            Assert.AreEqual(true, UtilityCheckData.IsCollectionEmpty<T>(collection));

            Assert.AreEqual(true, UtilityCheckData.IsCollectionEmpty<T>(null));
        }

        [TestMethod()]
        public void IsCollectionEmptyTest()
        {
            IsCollectionEmptyTestHelper<GenericParameterHelper>();
        }

關於泛型的測試其實也挺簡單的,沒什麼能夠囉嗦的,可是若是有了類型約束,那麼GenericParameterHelper類將極可能再也不能用了。this


而後再來看我作的一個類型約束泛型的單元測試代碼。url

 寫一個相似棧的需測試的類:spa

    public class StackNum<T> where T : struct
    {
        List<T> array = null;

        public StackNum()
        {
            this.array = new List<T>();
        }

        public void Push(T value)
        {
            array.Add(value);
        }

        public T Pop()
        {
            T val = array[this.Length - 1];
            this.array.Remove(val);
            return val;
        }

        public int Length
        {
            get { return this.array.Count; }
        }
    }
StackNum

在測試項目編寫一個測試幫助類code

    class StackTestHelper
    {
        public static void LengthTest<T>()
            where T : struct
        {
            var stack = GetStackInstance<T>();
            Assert.AreEqual(stack.Length, 0);
        }

        public static void PushTest<T>()
            where T : struct
        {
            var stack = GetStackInstance<T>();
            stack.Push(default(T));
            Assert.AreEqual(stack.Length, 1);
        }

        public static void PopTest<T>(params T[] values)
            where T : struct
        {
            var stack = GetStackInstance<T>();
            if (values == null)
            {
                return;
            }
            
            int pushLength = 0;
            foreach (T val in values)
            {
                stack.Push(val);
                Assert.AreEqual(stack.Length, ++pushLength);
            }

            for (int i = stack.Length - 1; i >= 0; i--)
            {
                Assert.AreEqual<T>(stack.Pop(), values[i]);
                Assert.AreEqual(stack.Length, i);
            }
        }

        public static StackNum<T> GetStackInstance<T>()
            where T : struct
        {
            return new StackNum<T>();
        }
    }
StackTestHelper

測試類htm

    [TestClass]
    public class StackTest
    {
        [TestMethod]
        public void PushTest()
        {
            StackTestHelper.PushTest<decimal>();
            StackTestHelper.PushTest<double>();
        }

        [TestMethod]
        public void PopTest()
        {
            StackTestHelper.PopTest<int>(22, 33, 55);
            StackTestHelper.PopTest<bool>(true, false);
        }

        [TestMethod]
        public void LengthTest()
        {
            StackTestHelper.LengthTest<char>();
        }
    }

這麼寫單元測試能夠簡單的切換咱們所須要進行測試的各類類型。

總結:對泛型作單元測試時相對會比通常的測試多寫一些代碼,不過多進行些抽象封裝仍是徹底能夠接受的,目前還不知道有什麼更好的辦法,如您有更好的辦法,請賜教,草民將不盡感激!!

 

題外話:感受我編寫單元測試的代碼比我編寫知足功能需求的代碼還多,可是我對着玩意兒卻絲毫沒任何抵觸情緒,但願剛開始步入Unit Test的你也是。

相關文章
相關標籤/搜索