是否可以创建动态单元测试?
Is it possible to create a dynamic unit test?
我目前有一些测试 运行,确保对于每个 class 实现特定接口,我都得到了一些尊重。
目前 运行 在一个 winform 应用程序中,它允许我为每个 class 给出一个结果,如果它们正常或有问题。
我想将其转换为 TestClass/TestMethod,但我目前不知道如何操作。
问题是我需要得到每个 class 的结果(或者至少每个 class 的个体都有一个错误,这是行不通的。
目前我有这段代码:
foreach (Type type in GetTypesToCheck())
{
m_logger.Debug("Checking type " + type.FullName);
FieldInfo staticField;
dynamic definition;
if (CheckStaticField(type, out staticField) && CheckDefinitionPresent(type) && CheckParentDefinition(type, staticField, out definition) && CheckRegistration(type, definition) &&
CheckSubTypes(type, definition))
{
m_logger.Information(type.FullName + ": OK");
}
}
有没有一种方法可以使用 UnitTests 进行这种检查,并且每个 class 有一个结果(或者每个 class 有多个结果)?
您可以将失败的类型存储在临时集合中,然后对该集合执行所需的断言。
下面是一个例子:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var failedTypes = new List<Type>(); //to keep failed types
foreach (Type type in GetTypesToCheck())
{
FieldInfo staticField;
dynamic definition;
if (!CheckStaticField(type, out staticField)
|| !CheckDefinitionPresent(type)
|| !CheckParentDefinition(type, staticField, out definition)
|| !CheckRegistration(type, definition)
|| !CheckSubTypes(type, definition))
failedTypes.Add(type);
}
Assert.IsTrue(
failedTypes.Count == 0,
"Failed types: " + string.Join(", ", failedTypes)
);
}
}
我目前有一些测试 运行,确保对于每个 class 实现特定接口,我都得到了一些尊重。
目前 运行 在一个 winform 应用程序中,它允许我为每个 class 给出一个结果,如果它们正常或有问题。
我想将其转换为 TestClass/TestMethod,但我目前不知道如何操作。
问题是我需要得到每个 class 的结果(或者至少每个 class 的个体都有一个错误,这是行不通的。
目前我有这段代码:
foreach (Type type in GetTypesToCheck())
{
m_logger.Debug("Checking type " + type.FullName);
FieldInfo staticField;
dynamic definition;
if (CheckStaticField(type, out staticField) && CheckDefinitionPresent(type) && CheckParentDefinition(type, staticField, out definition) && CheckRegistration(type, definition) &&
CheckSubTypes(type, definition))
{
m_logger.Information(type.FullName + ": OK");
}
}
有没有一种方法可以使用 UnitTests 进行这种检查,并且每个 class 有一个结果(或者每个 class 有多个结果)?
您可以将失败的类型存储在临时集合中,然后对该集合执行所需的断言。
下面是一个例子:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var failedTypes = new List<Type>(); //to keep failed types
foreach (Type type in GetTypesToCheck())
{
FieldInfo staticField;
dynamic definition;
if (!CheckStaticField(type, out staticField)
|| !CheckDefinitionPresent(type)
|| !CheckParentDefinition(type, staticField, out definition)
|| !CheckRegistration(type, definition)
|| !CheckSubTypes(type, definition))
failedTypes.Add(type);
}
Assert.IsTrue(
failedTypes.Count == 0,
"Failed types: " + string.Join(", ", failedTypes)
);
}
}