在NUnit 2.5中,您可以执行以下操作:
[TestCase(1,5,7)]
public void TestRowTest(int i, int j, int k)
{
Assert.AreEqual(13, i+j+k);
}你可以做参数测试。
但是我想知道你能不能做到这一点,用一般的检验方法进行参数检验?即:
[TestCase <int>("Message")]
public void TestRowTestGeneric<T>(string msg)
{
Assert.AreEqual(5, ConvertStrToGenericParameter<T>(msg));
}或者类似的东西。
发布于 2009-06-07 13:47:49
以下是NUnit 2.5 link text发行说明中的引语
参数化测试方法可以是泛型的。NUnit将根据所提供的参数类型推断出要使用的正确实现。泛型测试方法在泛型类和非泛型类中都受支持。
因此,在非泛型类中使用泛型测试方法是可能的。多么?
我不太明白杰夫的话。在.net中,泛型既是编译时也是运行时。我们可以使用反射来找出与方法关联的测试用例属性,找出泛型参数,然后再次使用反射来调用泛型方法。它会起作用的,不是吗?
更新:好了,我现在知道怎么做了,希望现在还不算太晚。您需要泛型类型在参数列表中。例如:
[TestCase((int)5, "5")]
[TestCase((double)2.3, "2.3")]
public void TestRowTestGeneric<T>(T value, string msg)
{
Assert.AreEqual(value, ConvertStrToGenericParameter<T>(msg));
}发布于 2016-11-16 04:42:44
您可以自定义GenericTestCaseAttribute
[Test]
[GenericTestCase(typeof(MyClass) ,"Some response", TestName = "Test1")]
[GenericTestCase(typeof(MyClass1) ,"Some response", TestName = "Test2")]
public void MapWithInitTest<T>(string expectedResponse)
{
// Arrange
// Act
var response = MyClassUnderTest.MyMethod<T>();
// Assert
Assert.AreEqual(expectedResponse, response);
}下面是GenericTestCaseAttribute的实现
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class GenericTestCaseAttribute : TestCaseAttribute, ITestBuilder
{
private readonly Type _type;
public GenericTestCaseAttribute(Type type, params object[] arguments) : base(arguments)
{
_type = type;
}
IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite)
{
if (method.IsGenericMethodDefinition && _type != null)
{
var gm = method.MakeGenericMethod(_type);
return BuildFrom(gm, suite);
}
return BuildFrom(method, suite);
}
}发布于 2011-05-24 13:57:29
创建一个私有方法并调用该方法:
[Test]
public void TypeATest()
{
MyTest<TypeA>();
}
[Test]
public void TypeBTest()
{
MyTest<TypeB>();
}
private void MyTest<T>()
{
// do test.
}https://stackoverflow.com/questions/801153
复制相似问题