2

I have a method defined like this

public void test(string fileName, int totalObjects, params int[] objectsToTest)

I am trying to use it in my NUnit test class like this

[TestCase("test.doc", 1, 3)]
public void test(string fileName, int totalObjects, params int[] objectsToTest)

the code compiles just fine but when NUnit test runner tries to perform the test I get the following exception:

System.ArgumentException : Object of type 'System.Int32' cannot be converted to type 'System.Int32[]'.

How can I get rid of the error and keep the ability to use the TestCase syntax test for method parameters?

EDIT:

I know I can pass an array (and I don't need to declare last parameter with params keyword for this). I am trying to avoid explicit passing of the array.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
  • possible duplicate of [Passing single value to params argument in NUnit TestCase](http://stackoverflow.com/questions/9550056/passing-single-value-to-params-argument-in-nunit-testcase) – DonBoitnott Nov 25 '14 at 18:51
  • I've just made a project with the code provided by OP and the testCase works fine here. Using NUnit 2.6.3 – Anders Nov 25 '14 at 19:01

3 Answers3

1

Just tried the following:

[TestFixture]
public class tester
{
    [TestCase("test.doc", 1, 3)]
    [TestCase("test.doc", 1, 3, 4, 5, 6)]
    public void test(string filename, int totalObjects, params int[] objectsToTest)
    {

    }
}

And both tests passed, and breakpoints show the objects are passed correctly.

Using NUnit 2.6.3 and .NET 4.5 and extension NUnit Test Adapter to run.

Anders
  • 1,590
  • 1
  • 20
  • 37
1

There is no problem with your method. Actual problem is in TestCaseAttribute from NUnit framework: it's just not smart enough to overcome runtime limitations.

In CLR runtime (and in compiled code) there is no "params", just corresponding array parameter. Information about existence of "params" is saved by defining ParamArrayAttribute instance on corresponding method. So, NUnit tries to apply supplied parameters as arguments to method, but it does not check if ParamArrayAttribute is defined on method and, therefore, does not wrap last argument to array. So you have to explicitly declare an array as parameter, not single value.

UPD: Based on comments supplied, I think that there is good chance this bug was fixed in recent NUnit release

Aloraman
  • 1,389
  • 1
  • 21
  • 32
0

Did you try:

[TestCase("test.doc", 1, new int[] { 3 }]
Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63