Skip to content

TestCase (+ TestCaseSource) throw exception when passing arguments to params array in generic method #5061

@stevenaw

Description

@stevenaw

I've noticed that the following will throw an exception on NUnit 4.4 and below

[TestCase(1, 2, 3, 4)]
public void HandlesParamsArrayForGenerics<T>(T s1, T s2, params T[] array)
{
}

System.NotSupportedException : Type is not supported.

Stack Trace: 
Array.InternalCreate(RuntimeType elementType, Int32 rank, Int32* pLengths, Int32* pLowerBounds)
Array.CreateInstance(Type elementType, Int32 length)
TestCaseAttribute.GetParametersForTestCase(IMethodInfo method)

This can also now be reproduced in main with TestCaseSource as they use a common helper for this:

 [TestCase(1, 2, 3, 4)]
 [TestCaseSource(nameof(ParamsArrayFourStringArguments))]
 public void HandlesParamsArrayForGenerics<T>(T s1, T s2, params T[] array)
 {
 }

 private static IEnumerable ParamsArrayFourStringArguments
 {
     get
     {
         yield return new TestCaseData("a", "b", "c", "d").SetArgDisplayNames("new TestCaseData(\"a\", \"b\", \"c\", \"d\")");
         yield return new string[] { "a", "b", "c", "d" };
     }
 }

The exception is currently thrown from https://github.com/nunit/nunit/blob/main/src/NUnitFramework/framework/Internal/TestCaseParameters.cs#L140

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions