快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

新黄金城xhjc.667722:Visual Studio 2012单元测试之对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试



为了适应本次单元测试的编码,对IdleTest进行了一些更新,本文只描述更新部分,详细源码请移步https://idletest.codeplex.com/ 。

1.重构TestCommon类的ArrayEqual措施,变成了

#region Equal

////// 判断两个数组项相等(顺序必须同等),对数组项应用"Equal措施"校验,

/// 假如非CTS类型(即自定义),则应在应用本措施前对Equal措施进行重载///

public static bool ArrayEqual(Arra新黄金城xhjc.667722y array1, Array array2){

bool isCountEqual = CollectionCountEqual(array1, array2);if (!isCountEqual || array1 == null || array2 == null)

{return isCountEqual;

}

for (int i = 0; i{

if (!object.Equals(array1.GetValue(i), array2.GetValue(i))){

return false;}

}

return true;}

///

/// 判断两个聚拢项相等(顺序必须同等),对聚拢项应用"Equal措施"校验,/// 假如非CTS类型(即自定义),则应在应用本措施前对Equal措施进行重载

///public static bool ListEqual(IList list1, IList list2)

{bool isCountEqual = CollectionCountEqual(list1, list1);

if (!isCountEqual || l新黄金城xhjc.667722ist1 == null || list2 == null){

return isCountEqual;}

for (int i = 0; i

{if (!object.Equals(list1[i], list2[i]))

{return false;

}}

return true;

}

////// 判断两个聚拢项相等(顺序必须同等),对聚拢项应用"Equal措施"校验,

/// 假如非CTS类型(即自定义),则应在应用本措施前对Equal措施进行重载///

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新黄金城xhjc.667722, collection2 as Array);

}

if (collection1 is IList && collection2 is IList){

return ListEqual(collection1 as IList, collection2 as IList);}

return false;

}

////// 验证两个聚拢的长度是否同等

//////

要判断的聚拢1

///

要判断的聚拢2/// 长度相等(两个聚拢为null或者长度为0以及一个为null另一个长度为0均觉得相等)

/// 返回true,否则返回falsepublic 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类新增措施如下

///

/// 断言为Empty///

/// 措施参数类型1/// 措施参数类型2

/// 措施返回类型///

调用的措施

///

需断言的参数集1///

需断言的参数集2

///

测试的措施聚拢///

是否断言为空

public static void AssertEmpty(TParameter1[] args1, TParameter2[] args2, bool assertEmpty = true, params Func[] funcs)

{AssertHandle((TReturn actual) =>

{AssertEmpty

(actual, assertEmpty);

}, args1, args2, funcs);}

如标题所说,我现在有如下无返回值以及应用Action和Func作为参数的几个措施

public class UtilityCommon

{#region Foreach Handle

////// 进行遍历

////// 类型

///

遍历的聚拢///

遍历到每一项履行的措施

///

跳出轮回的措施(action履行前)///

跳出轮回的措施(action履行后)

public static void ForeachHandle(IEnumerable array, Action action,Funcbool> breakBeforeFunc, Funcbool> breakAfterFunc)

{if (array == null || action == null)

{return;

}

foreach (T item in array){

if (break新黄金城xhjc.667722BeforeFunc != null && breakBeforeFunc(item)){

break;}

action(item);

if (breakAfterFunc != null && breakAfterFunc(item)){

break;}

}}

///

/// 进行遍历///

/// 类型///

遍历的聚拢

///

遍历到每一项履行的措施public static void ForeachHandle(IEnumerable array, Action action)

{ForeachHandle(array, action, null, null);

}

////// 进行遍历,假如迭代器中的项不为T类型,则跳过不履行操作(action)

////// 类型

///

遍历的聚拢///

遍历到每一项履行的措施

///

跳出轮回的措施(action履行前)///

跳出轮回的措施(action履行后)

public static void ForeachHandle(IEnumerable array, Action action,Funcbool> breakBeforeFunc, Funcbool> 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;

}}

}}

///

/// 进行遍历,假如迭代器中的项不为T类型,则不履行操作(action)///

/// 类型///

遍历的聚拢

///

遍历到每一项履行的措施public static void ForeachHandle(IEnumerable array, Action action)

{ForeachHandle(array, action, null, null);

}#endregion

}

必要进行测试的代码

这正表现着单元测试中三个难点:

(1)void返回类型。因为没有返回值,以是只能模拟措施内部操作的需求进行测试。作为无返回值的措施,其肯定改变了外部的一些信息,否则该措施基础上没故意义,这就使得其具有可测试性。比如下面的代码,将措施对外界的影响改变成了对“arrayActual”这个变量的影响,应用该要领必要留意闭包孕育发生影响。

(2)以委托工具作为参数,因为调用的委托未知,故而对其做单元测试很难完全做到客不雅上绝对的100%覆盖率,只管用VS运行覆盖率阐发时达到了100%。这个大年夜家可以看我的代码找到未覆盖的模块,很轻易看出来的,呵呵,不是我预留,而是我服从VS的覆盖阐发结果就懒得去改罢了。

(3)措施有重载时,打消重复测试代码。我的做法一样平常是把所有的重载测试代码的数据构造提炼成一个措施,然后在各测试中以实际调用的措施作为参数传入,能做到这步境地真的异常谢谢dynamic。

详细关于我对以上三点的做法,请看如下测试代码

[TestClass()]

public class UtilityCommonTest{

//////ForeachHandle 的测试

///public void ForeachHandleTestHelper(dynamic actionTest, bool hasBreakBefore = false, bool hasBreakAfter = false)

{bool notBreak = !hasBreakAfter && !hasBreakBefore;

IEnumerableint> array = new int[] { 1, 2, 3, 4, 5 };

Listint> arrayActual = new Listint>();Actionint> action = p => arrayActual.Add(p);

Funcint, bool> breakBeforeFunc = null; // TODO: 初始化为适当的值Funcint, bool> breakAfterFunc = null; // TODO: 初始化为适当的值

//UtilityCommon.ForeachHandle(array, action, breakBeforeFunc, breakAfterFunc);if (notBreak)

{actionTest(array, action);

}else

{actionTest(array, action, breakBeforeFunc, breakAfterFunc);

}

AssertCommon.AssertEqual(array, arrayActual);

//奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫*

arrayActual = new Listint>();AssertCommon.AssertEmptyint>, Listint>, Listint>>(

new IEnumerableint>[] { null, new int[] { }, new Listint>() },new Listint>[] { arrayActual },

true,(pIn1, pIn2) =>

{//UtilityCommon.ForeachHandle(pIn1, p => pIn2.Add(p));

if (notBreak)actionTest(pIn1, new Actionint>(p => pIn2.Add(p)));

elseactionTest(pIn1, new Actionint>(p => pIn2.Add(p)), breakBeforeFunc, breakAfterFunc);

return pIn2;

});

if (notBreak)return;

//奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫奸淫*

// If Has Break Function

breakBeforeFunc = p => 新黄金城xhjc.667722p > 4;breakAfterFunc = p => p > 3;

arrayActual = new Listint>();

actionTest(array, action, breakBeforeFunc, null);AssertCommon.AssertEqual(new int[] { 1, 2, 3, 4 }, arrayActual);

arrayActual = new Listint>();

actionTest(array, action, null, breakAfterFunc);AssertCommon.AssertEqual(new int[] { 1, 2, 3, 4 }, arrayActual);

arrayActual = new Listint>();

breakBeforeFunc = p => p > 3;actionTest(array, action, breakBeforeFunc, breakAfterFunc);

AssertCommon.AssertEqual(new int[] { 1, 2, 3 }, arrayActual);

arrayActual = new Listint>();breakAfterFunc = p => p > 1;

actionTest(array, action, breakBeforeFunc, breakAfterFunc);AssertCommon.AssertEqual(new int[] { 1 }, arrayActual);

}

[TestMethod()]public void ForeachHandleTest()

{ForeachHandleTestHelper(new Actionint>>(UtilityCommon.ForeachHandleint>));

}

[TestMethod()]public void ForeachHandleGenericTest()

{ForeachHandleTestHelper(new Actionint>, Actionint>>(UtilityCommon.ForeachHandleint>));

}

[TestMethod()]public void ForeachHandleHasBreakTest()

{ForeachHandleTestHelper(new Actionint>, Funcint, bool>, Funcint, bool>>(

UtilityCommon.ForeachHandleint>), true, true);}

[TestMethod()]

public void ForeachHandleGenericHasBreakTest(){

ForeachHandleTestHelper(new Actionint>, Actionint>, Funcint, bool>, Funcint, bool>>(UtilityCommon.ForeachHandleint>), true, true);

}}

测试代码

运行经由过程测试后履行代码覆盖率阐发,结果为100%,如图中选中行所示

【后话】

本文代码在IdleTest的位置如图中选中的文件所示

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: