VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
【提示】
1. 阅读文本前希望您具备如下知识:了解单元测试,了解Dynamic,熟悉泛型(协变与逆变)和Lambda,熟悉.NET Framework提供的
Action与Func委托。
2.如果您对单元测试无兴趣请止步。
3.本文将使用一些我自己的测试公共代码,位于https://idletest.codeplex.com/,此处亦非常欢迎来访。
4.关于本人之前单元测试的文章可参阅
《VS2012 单元测试之泛型类(Generics Unit Test)》、
《VS2012 Unit Test —— 我对接口进行单元测试使用的技巧》
【修改IdleTest】
为了适应本次单元测试的编码,对IdleTest进行了一些更新,本文只描述更新部分,具体源码请移步https://idletest.codeplex.com/。
1.重构TestCommon类的ArrayEqual方法,变成了
#region Equal
/// <summary>
/// 判断两个数组项相等(顺序必须一致),对数组项使用"Equal方法"校验,
/// 如果非CTS类型(即自定义),则应在使用本方法前对Equal方法进行重载
/// </summary>
public static bool ArrayEqual(Array array1, Array array2)
{
bool isCountEqual = CollectionCountEqual(array1, array2);
if (!isCountEqual || array1 == null || array2 == null)
{
return isCountEqual;
} for (int i = ; i < array1.Length; i++)
{
if (!object.Equals(array1.GetValue(i), array2.GetValue(i)))
{
return false;
}
} return true;
} /// <summary>
/// 判断两个集合项相等(顺序必须一致),对集合项使用"Equal方法"校验,
/// 如果非CTS类型(即自定义),则应在使用本方法前对Equal方法进行重载
/// </summary>
public static bool ListEqual(IList list1, IList list2)
{
bool isCountEqual = CollectionCountEqual(list1, list1);
if (!isCountEqual || list1 == null || list2 == null)
{
return isCountEqual;
} for (int i = ; i < list1.Count; i++)
{
if (!object.Equals(list1[i], list2[i]))
{
return false;
}
} return true;
} /// <summary>
/// 判断两个集合项相等(顺序必须一致),对集合项使用"Equal方法"校验,
/// 如果非CTS类型(即自定义),则应在使用本方法前对Equal方法进行重载
/// </summary>
public static bool CollectionEqual(object collection1, object collection2)
{
if (collection1 == null && collection2 == null)
{
return true;
} if (collection1 is Array && collection2 is Array)
{
return ArrayEqual(collection1 as Array, collection2 as Array);
} if (collection1 is IList && collection2 is IList)
{
return ListEqual(collection1 as IList, collection2 as IList);
} return false;
} /// <summary>
/// 验证两个集合的长度是否一致
/// </summary>
/// <param name="collection1">要判断的集合1</param>
/// <param name="collection2">要判断的集合2</param>
/// <returns>长度相等(两个集合为null或者长度为0以及一个为null另一个长度为0均认为相等)
/// 返回true,否则返回false</returns>
public static bool CollectionCountEqual(ICollection collection1, ICollection collection2)
{
if ((collection1 == null || collection1.Count < )
&& (collection2 == null || collection2.Count < ))
{
return true;
}
else if ((collection1 == null || collection1.Count < )
|| (collection2 == null || collection2.Count < ))
{
return false;
} return collection1.Count == collection2.Count;
} #endregion
2. AssertCommon类新增方法如下
/// <summary>
/// 断言为Empty
/// </summary>
/// <typeparam name="TParameter1">方法参数类型1</typeparam>
/// <typeparam name="TParameter2">方法参数类型2</typeparam>
/// <typeparam name="TReturn">方法返回类型</typeparam>
/// <param name="action">调用的方法</param>
/// <param name="args1">需断言的参数集1</param>
/// <param name="args2">需断言的参数集2</param>
/// <param name="funcs">测试的方法集合</param>
/// <param name="assertNull">是否断言为空</param>
public static void AssertEmpty<TParameter1, TParameter2, TReturn>(
TParameter1[] args1, TParameter2[] args2, bool assertEmpty = true, params Func<TParameter1, TParameter2, TReturn>[] funcs)
{
AssertHandle<TParameter1, TParameter2, TReturn>((TReturn actual) =>
{
AssertEmpty<TReturn>(actual, assertEmpty);
}, args1, args2, funcs);
}
【入正题】
如标题所说,我现在有如下无返回值以及使用Action和Func作为参数的几个方法
public class UtilityCommon
{
#region Foreach Handle
/// <summary>
/// 进行遍历
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="array">遍历的集合</param>
/// <param name="action">遍历到每一项执行的方法</param>
/// <param name="breakBeforeFunc">跳出循环的方法(action执行前)</param>
/// <param name="breakAfterFunc">跳出循环的方法(action执行后)</param>
public static void ForeachHandle<T>(IEnumerable<T> array, Action<T> action,
Func<T, bool> breakBeforeFunc, Func<T, bool> breakAfterFunc)
{
if (array == null || action == null)
{
return;
} foreach (T item in array)
{
if (breakBeforeFunc != null && breakBeforeFunc(item))
{
break;
} action(item);
if (breakAfterFunc != null && breakAfterFunc(item))
{
break;
}
}
} /// <summary>
/// 进行遍历
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="array">遍历的集合</param>
/// <param name="action">遍历到每一项执行的方法</param>
public static void ForeachHandle<T>(IEnumerable<T> array, Action<T> action)
{
ForeachHandle<T>(array, action, null, null);
} /// <summary>
/// 进行遍历,如果迭代器中的项不为T类型,则跳过不执行操作(action)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="array">遍历的集合</param>
/// <param name="action">遍历到每一项执行的方法</param>
/// <param name="breakBeforeFunc">跳出循环的方法(action执行前)</param>
/// <param name="breakAfterFunc">跳出循环的方法(action执行后)</param>
public static void ForeachHandle<T>(IEnumerable array, Action<T> action,
Func<T, bool> breakBeforeFunc, Func<T, bool> breakAfterFunc)
{
if (array == null || action == null)
{
return;
} foreach (var item in array)
{
if (item is T)
{
T t = (T)item;
if (breakBeforeFunc != null && breakBeforeFunc(t))
{
break;
} action(t);
if (breakAfterFunc != null && breakAfterFunc(t))
{
break;
}
}
}
} /// <summary>
/// 进行遍历,如果迭代器中的项不为T类型,则不执行操作(action)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="array">遍历的集合</param>
/// <param name="action">遍历到每一项执行的方法</param>
public static void ForeachHandle<T>(IEnumerable array, Action<T> action)
{
ForeachHandle<T>(array, action, null, null);
}
#endregion
}
需要进行测试的代码
这正体现着单元测试中三个难点:
(1)void返回类型。由于没有返回值,所以只能模拟方法内部操作的需求进行测试。作为无返回值的方法,其肯定改变了外部的一些信息,否则该方法基本上没有意义,这就使得其具有可测试性。比如下面的代码,将方法对外界的影响转变成了对“arrayActual”这个变量的影响,使用该方式需要注意闭包产生影响。
(2)以委托对象作为参数,由于调用的委托未知,故而对其做单元测试很难完全做到客观上绝对的100%覆盖率,尽管用VS运行覆盖率分析时达到了100%。这个大家可以看我的代码找到未覆盖的模块,很容易看出来的,呵呵,不是我预留,而是我遵从VS的覆盖分析结果就懒得去改罢了。
(3)方法有重载时,消除重复测试代码。我的做法一般是把所有的重载测试代码的数据构造提炼成一个方法,然后在各测试中以实际调用的方法作为参数传入,能做到这步田地真的非常感谢dynamic。
具体关于我对以上三点的做法,请看如下测试代码
[TestClass()]
public class UtilityCommonTest
{
/// <summary>
///ForeachHandle 的测试
///</summary>
public void ForeachHandleTestHelper(dynamic actionTest, bool hasBreakBefore = false, bool hasBreakAfter = false)
{
bool notBreak = !hasBreakAfter && !hasBreakBefore; IEnumerable<int> array = new int[] { , , , , };
List<int> arrayActual = new List<int>();
Action<int> action = p => arrayActual.Add(p);
Func<int, bool> breakBeforeFunc = null; // TODO: 初始化为适当的值
Func<int, bool> breakAfterFunc = null; // TODO: 初始化为适当的值
//UtilityCommon.ForeachHandle<int>(array, action, breakBeforeFunc, breakAfterFunc);
if (notBreak)
{
actionTest(array, action);
}
else
{
actionTest(array, action, breakBeforeFunc, breakAfterFunc);
} AssertCommon.AssertEqual(array, arrayActual); //************************************************************************************* arrayActual = new List<int>();
AssertCommon.AssertEmpty<IEnumerable<int>, List<int>, List<int>>(
new IEnumerable<int>[] { null, new int[] { }, new List<int>() },
new List<int>[] { arrayActual },
true,
(pIn1, pIn2) =>
{
//UtilityCommon.ForeachHandle<int>(pIn1, p => pIn2.Add(p));
if (notBreak)
actionTest(pIn1, new Action<int>(p => pIn2.Add(p)));
else
actionTest(pIn1, new Action<int>(p => pIn2.Add(p)), breakBeforeFunc, breakAfterFunc); return pIn2;
}); if (notBreak)
return; //*************************************************************************************
// If Has Break Function breakBeforeFunc = p => p > ;
breakAfterFunc = p => p > ; arrayActual = new List<int>();
actionTest(array, action, breakBeforeFunc, null);
AssertCommon.AssertEqual<IList>(new int[] { , , , }, arrayActual); arrayActual = new List<int>();
actionTest(array, action, null, breakAfterFunc);
AssertCommon.AssertEqual<IList>(new int[] { , , , }, arrayActual); arrayActual = new List<int>();
breakBeforeFunc = p => p > ;
actionTest(array, action, breakBeforeFunc, breakAfterFunc);
AssertCommon.AssertEqual<IList>(new int[] { , , }, arrayActual); arrayActual = new List<int>();
breakAfterFunc = p => p > ;
actionTest(array, action, breakBeforeFunc, breakAfterFunc);
AssertCommon.AssertEqual<IList>(new int[] { }, arrayActual);
} [TestMethod()]
public void ForeachHandleTest()
{
ForeachHandleTestHelper(new Action<IEnumerable, Action<int>>(UtilityCommon.ForeachHandle<int>));
} [TestMethod()]
public void ForeachHandleGenericTest()
{
ForeachHandleTestHelper(new Action<IEnumerable<int>, Action<int>>(UtilityCommon.ForeachHandle<int>));
} [TestMethod()]
public void ForeachHandleHasBreakTest()
{
ForeachHandleTestHelper(new Action<IEnumerable, Action<int>, Func<int, bool>, Func<int, bool>>(
UtilityCommon.ForeachHandle<int>), true, true);
} [TestMethod()]
public void ForeachHandleGenericHasBreakTest()
{
ForeachHandleTestHelper(new Action<IEnumerable<int>, Action<int>, Func<int, bool>, Func<int, bool>>(
UtilityCommon.ForeachHandle<int>), true, true);
}
}
测试代码
运行通过测试后执行代码覆盖率分析,结果为100%,如图中选中行所示
【后话】
本文代码在IdleTest的位置如图中选中的文件所示
能力有限文中不免有漏,还望各位前辈同仁后浪海涵并及时指正,不尽感激。我将会坚持写关于单元测试的文章以及更新https://idletest.codeplex.com/,愿与您携手同进步。
VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试的更多相关文章
- Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
http://blog.csdn.net/config_man/article/details/25578767 #region 调用timer控件实时查询开关机时间 private void tim ...
- 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
VS2012 Unit Test(Void, Action, Func) —— 对无返回值.使用Action或Func作为参数.多重载的方法进行单元测试 [提示] 1. 阅读文本前希望您具备如下知识: ...
- 慕课网-Java入门第一季-7-2 Java 中无参无返回值方法的使用
来源:http://www.imooc.com/code/1578 如果方法不包含参数,且没有返回值,我们称为无参无返回值的方法. 方法的使用分两步: 第一步,定义方法 例如:下面代码定义了一个方法名 ...
- Android——关于Activity跳转的返回(无返回值和有返回值)——有返回值
说明: 跳转页面,并将第一页的Edittext输入的数据通过按钮Button传到第二页用Edittext显示,点击第二页的 返回按钮Button返回第一页(改变第二页的Edittext的内容会传至第一 ...
- Java 中无参无返回值方法的使用
如果方法不包含参数,且没有返回值,我们称为无参无返回值的方法. 方法的使用分两步: 第一步,定义方法 例如:下面代码定义了一个方法名为 show ,没有参数,且没有返回值的方法,执行的操作为输出 “ ...
- 面试题----入参两个Integer,无返回值,然后使这个两个值在调用函数后交换
我最近看到过一个比较好玩的面试题. 写个方法,入参两个Integer,无返回值,然后使这个两个值在调用函数后交换 很有意思的一个题目,引发我的深思,根据一路的学习过来,下面把实现代码贴出来,方便学习. ...
- 无返回值的异步方法能否不用await
1.无返回值的异步方法能否不用await? 如果你不需要等待加一的操作完成,那就可以直接执行后面的操作.那要看你的需求了,如果你后面的操作必须在加一的操作后执行,那就要await了 2.请问C#中如何 ...
- ForkJoin有参无返回值、有参有返回值实例
介绍: a . Fork/Join为JKD1.7引入,适用于对大量数据进行拆分成多个小任务进行计算的框架,最后把所有小任务的结果汇总合并得到最终的结果 b . 相关类 public abstract ...
- Java 中无返回值的方法在使用时应该注意的问题
Java 中的方法是形态多样的.无返回值的方法在使用时应该规避哪些问题呢? 一.不可以打印调用或是赋值调用,只能是单独调用(非常重要): 二.返回值没有,不代表参数就没有: 三.不能return一个具 ...
随机推荐
- ExtJs布局详解
序言 1.百度百科上说:ExtJs功能丰富,无人能出其右.无论是界面之美,还是功能之强,extjs都高居榜首. 2.呵呵,界面之美当是少不了布局的,这篇文章我写layout的七种布局.(extjs是4 ...
- 【Win10 应用开发】语音命令与App Service集成
昨天,老周演示了语音命令集成这一高大上功能,今天咱们来点更高级的语音命令. 在昨天的例子中,响应语音命令是需要启动应用程序的,那么如果可以不启动应用程序,就直接在小娜面板上进行交互,是不是会更高大小呢 ...
- javascript中函数声明和函数表达式浅析
记得在面试腾讯实习生的时候,面试官问了我这样一道问题. //下述两种声明方式有什么不同 function foo(){}; var bar = function foo(){}; 当初只知道两种声明方 ...
- 【HTTP劫持和DNS劫持】实际JS对抗
1.对于DIV注入的,可以初始化时检查全部html代码. 检测是否被劫持比较简单,但对抗就略麻烦,这个在说完第2点之后再解释. 2.对于js注入,可以在window监听DOMNodeInserted事 ...
- IOS开发之显示微博表情
在上一篇博客中山寨了一下新浪微博,在之后的博客中会对上一篇代码进行优化和重用,上一篇的微博请求的文字中有一些表情没做处理,比如带有表情的文字是这样的“我要[大笑],[得意]”.显示的就是请求的字符串, ...
- 使用免费组件view pdf 文档
“Is there any way to view PDF files in a Winforms tool?” 这个是我在技术论坛上发现的一个老外求助的帖子,然后看到别人回复的帖子中建议了一个免费的 ...
- Blob初探
简介 Blob在js中意味着二进制大数据.实现该接口的对象有3个属性,分别是type(MIME),size(byte)和 一个切割方法:slice(在大文件分片上传可能用到).另外,File实现了Bl ...
- js实现可拖拽的div
前言 下午忙里偷闲想写一个可拖拽的例子,留在脑海里一直都是三个事件mouseDown,mouseUp,mouseMove, 但从没有动手实践过,今天想起了自己实践了并学习了张鑫旭的demo实现. 学习 ...
- 创建ASP.NET Core MVC应用程序(6)-添加验证
创建ASP.NET Core MVC应用程序(6)-添加验证 DRY原则 DRY("Don't Repeat Yourself")是MVC的设计原则之一.ASP.NET MVC鼓励 ...
- Openfire 4.1.0
http://www.igniterealtime.org/downloads/index.jsp 服务器端口 接口 端口 类型 描述 所有的地址 5222 客户端到服务器 客户端使用标准端口连接 ...