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类将很可能不再能用了。


然后再来看我做的一个类型约束泛型的单元测试代码。

 写一个类似栈的需测试的类:

    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

在测试项目编写一个测试帮助类

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

posted @ 2013-07-10 16:19  dong.net  阅读(6545)  评论(6编辑  收藏  举报