读书人

C# 替私有方法添加单元测试

发布时间: 2013-01-07 10:02:24 作者: rapoo

C# 为私有方法添加单元测试
C# 为私有方法添加单元测试(使用反射)

Add Unit Test for private method in C# using reflection

在Visuall Studio 2010或更老的版本中,使用系统生成默认的单元测试就可以测试私有方法。以VS 2010为例,在一个方法上单击右键,选择创建单元测试即可。生成的单元测试中,会为私有方法生成accesor,这时就可以在单元测试中访问私有方法了。

但是,这个方法在Visual Studio 2012中被去掉了,并且微软官方声称这是不佳实践,以后可能都不会支持了。于是笔者四处寻觅,找到了几种解决方案:

    使用InternalsVisibleToAttribute类,但是这样做要把私有方法改成Internal方法。使用内嵌单元测试。即把单元测试直接写到含有私有方法的类中,然后通过条件编译的方法保证在发布时,软件中的单元测试方法不随着软件发布。因为单元测试在对应的类中,所以就不存在私有问题了。这种方法的弱点在于单元测试管理不方便,最后很可能是单元测试到处都是。使用反射。笔者最终选用了这个方法。

使用反射方法,可以调用任意级别的函数。需要做以下几步:

    在测试工程中添加对待测试工程的引用(相信大家都已经做了)使用如下两个公有函数调用对应的私有函数:

/// <summary>

/// 调用静态方法

/// </summary>

/// <param name="t">类全名</param>

/// <paramname="strMethod">方法名</param>

/// <paramname="aobjParams">参数表</param>

/// <returns>函数返回值</returns>

public static object RunStaticMethod(System.Type t, string strMethod, object[] aobjParams)

{

BindingFlags eFlags =

BindingFlags.Static | BindingFlags.Public |

BindingFlags.NonPublic;

return RunMethod(t, strMethod,

null, aobjParams, eFlags);

}

/// <summary>

/// 调用实例方法

/// </summary>

/// <param name="t">类全名</param>

/// <paramname="strMethod">方法名</param>

/// <paramname="objInstance">类的实例</param>

///<paramname="aobjParams">参数表</param>

///<returns>函数返回值</returns>

public static object RunInstanceMethod(System.Type t, string strMethod,

object objInstance, object[] aobjParams)

{

BindingFlags eFlags = BindingFlags.Instance | BindingFlags.Public |

BindingFlags.NonPublic;

return RunMethod(t, strMethod,

objInstance, aobjParams, eFlags);

}

private static object RunMethod(System.Type t, string

strMethod, object objInstance, object[] aobjParams, BindingFlags eFlags)

{

MethodInfo m;

try

{

m = t.GetMethod(strMethod,eFlags);

if (m == null)

{

throw new ArgumentException("There is no method '" +

strMethod + "' for type'" +t.ToString() + "'.");

}

object objRet = m.Invoke(objInstance,aobjParams);

return objRet;

}

catch

{

throw;

}

}

读书人网 >C#

热点推荐