views:

408

answers:

1

In NUnit 2.5 you can do this:

[TestCase(1,5,7)]
public void TestRowTest(int i, int j, int k)
{
  Assert.AreEqual(13, i+j+k);
}

You can do Parametric test.

But I wonder whether you can do this or not, Parametric test with generic test method?, i.e.,

[TestCase <int>("Message")]
public void TestRowTestGeneric<T>(string msg)
{
  Assert.AreEqual(5, ConvertStrToGenericParameter<T>(msg));
}

Or something similar.

+3  A: 

Here is the quote from the release note of NUnit 2.5 link text

Parameterized test methods may be generic. NUnit will deduce the correct implementation to use based on the types of the parameters provided. Generic test methods are supported in both generic and non-generic clases.

According to this, it is possible to have generic test method in non-generic class. How?

I don't quite understand Jeff's comment. In .net generics is both compile-time and run-time. We can use the reflection to find out the test case attribute associated with a method, find out the generic parameter, and again use reflection to call the generic method. It will work, no?

Update: OK, I now know how and hope it is not too late. You need the generic type to be in the parameter list. For example:

[TestCase((int)5, "5")]
[TestCase((double)2.3, "2.3")]
public void TestRowTestGeneric<T>(T value, string msg)
{
  Assert.AreEqual(value, ConvertStrToGenericParameter<T>(msg));
}
Kenneth Xu