Unit testing 反射测试的Resharper或nUnit测试计数
我有很多基于反思的测试。反射测试在给定类型的给定程序集上工作,例如,在service.dll中查找基于iSeries的所有内容。我之所以这样做是因为我需要将引用类型导入到测试中,而我不能使用TestCase属性。代码基本上如下所示:Unit testing 反射测试的Resharper或nUnit测试计数,unit-testing,nunit,resharper,Unit Testing,Nunit,Resharper,我有很多基于反思的测试。反射测试在给定类型的给定程序集上工作,例如,在service.dll中查找基于iSeries的所有内容。我之所以这样做是因为我需要将引用类型导入到测试中,而我不能使用TestCase属性。代码基本上如下所示: public static void TestRunnerforTypeList(Type baseType, Func<Type, string, bool> excluder, Action<Type, string> mockR
public static void TestRunnerforTypeList(Type baseType, Func<Type, string, bool> excluder, Action<Type, string> mockResolver, Dictionary<Type, Dictionary<Type, Mock>> mocks, Action<dynamic, Type> assertor, string methodName)
{
foreach (var type in GetTypesToTest(baseType))
{
if (excluder(type, methodName)) continue;
dynamic objectToTest = CreateInstance(type, mocks);
mockResolver(type, methodName);
assertor(objectToTest, type);
}
}
[Test]
public void Positive_outcome_for_Get()
{
GeneralTestRunner.TestRunnerforTypeList(typeof(IService<,,>),
_serviceFactoryContext.ExcludeTypeForMethod,
_serviceFactoryContext.ResolvePositiveMockSetup,
_mocks,
(service, type) => Assert.IsNotNull(service.Get(1)),
"Get");
}
public static void TestRunnerforTypeList(类型baseType、Func excluder、操作mockResolver、字典mock、操作assertor、字符串methodName)
{
foreach(GetTypesToTest(baseType)中的var类型)
{
if(excluder(type,methodName))继续;
DynamicObjectToTest=CreateInstance(类型,mocks);
mockResolver(类型、方法名);
assertor(objectToTest,类型);
}
}
对此的调用如下所示:
public static void TestRunnerforTypeList(Type baseType, Func<Type, string, bool> excluder, Action<Type, string> mockResolver, Dictionary<Type, Dictionary<Type, Mock>> mocks, Action<dynamic, Type> assertor, string methodName)
{
foreach (var type in GetTypesToTest(baseType))
{
if (excluder(type, methodName)) continue;
dynamic objectToTest = CreateInstance(type, mocks);
mockResolver(type, methodName);
assertor(objectToTest, type);
}
}
[Test]
public void Positive_outcome_for_Get()
{
GeneralTestRunner.TestRunnerforTypeList(typeof(IService<,,>),
_serviceFactoryContext.ExcludeTypeForMethod,
_serviceFactoryContext.ResolvePositiveMockSetup,
_mocks,
(service, type) => Assert.IsNotNull(service.Get(1)),
"Get");
}
[测试]
公共无效正结果(用于获取)
{
GeneralTestRunner.TestRunnerforTypeList(typeof(iSeries设备),
_serviceFactoryContext.ExcludeTypeFormMethod,
_serviceFactoryContext.ResolvePositiveMockSetup,
_嘲弄,
(service,type)=>Assert.IsNotNull(service.Get(1)),
“得到”);
}
这是一个简单的断言,但你明白了。通过这种方式,我获得了TestCase属性的好处,但引用类型(如mock)被引入
然而,我在其他地方使用了TestCase属性,Resharper会选择这些属性并增加测试会话中的测试数量,而反射属性则不会
我的问题是,有没有一种方法可以告诉Resharper(或nUnit),每次调用上述assertor操作时,该数字都会增加
提前感谢;) 我发现实现这一点的唯一方法是使用Resharper 6并删除反射特性,然后使用TestCaseSource属性。TestCaseSource将指向一个静态IEnumerable,为每个测试生成一个工厂引用类型。见以下文件: 与问题实现相比,唯一的问题是,如果程序员没有为实例创建者创建模拟,那么反射测试将失败,但是类型仍然被拾取(如果它在同一个程序集中)。因此,如果一个懒惰的程序员创建了一个基于IService(例如)的新类型,而没有测试,如果失败了,就开箱即用