Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parametric test with generic methods

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.

like image 553
Graviton Avatar asked Apr 29 '09 06:04

Graviton


People also ask

What are generic methods?

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

What is a generic type parameter in Java?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

How do you create a generic method?

Generic MethodsAll generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example). Each type parameter section contains one or more type parameters separated by commas.

Which of the following options best describe generic methods?

Which of these is an correct way of defining generic method? Explanation: The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method's return type. For static generic methods, the type parameter section must appear before the method's return type.


2 Answers

You can make custom 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);
    }

Here is implementation of 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);
    }
}
like image 67
R.Titov Avatar answered Oct 06 '22 01:10

R.Titov


Create a private method and call that:

    [Test]
    public void TypeATest()
    {
        MyTest<TypeA>();
    }

    [Test]
    public void TypeBTest()
    {
        MyTest<TypeB>();
    }

    private void MyTest<T>()
    {
        // do test.
    }
like image 36
mkaj Avatar answered Oct 06 '22 01:10

mkaj