天天看點

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

關于單元測試,如果不會用可以參照我的上篇博文————在Visual Studio 2012使用單元測試

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

泛型有兩種,一般泛型與類型限制泛型,在對包含泛型的方法進行單元測試中也可以這麼分,詳情可參閱http://msdn.microsoft.com/en-us/library/vstudio/ms243401.aspx  。從該頁面可以知道,關于泛型的單元測試,微軟類庫(Microsoft.VisualStudio.TestTools.UnitTesting)提供了類“GenericParameterHelper”幫助我們編寫Unit Test代碼。

首先看下非類型限制的一個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類将很可能不再能用了。

然後再來看我做的一個類型限制泛型的單元測試代碼。

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

VS2012 單元測試之泛型類(Generics Unit Test)
VS2012 單元測試之泛型類(Generics Unit Test)
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

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

VS2012 單元測試之泛型類(Generics Unit Test)
VS2012 單元測試之泛型類(Generics Unit Test)
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

測試類

[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的你也是。

 感謝閱讀,請留下您的意見或疑問! 能力有限,錯漏難免,歡迎指點!

 分割線:我的個人原創,請認準 http://freedong.cnblogs.com/ (轉摘不标原文出處可恥)

繼續閱讀