Beta 2.5 and TestCase with array parameters

437 views
Skip to first unread message

Mark

unread,
Jan 12, 2009, 2:08:33 PM1/12/09
to NUnit-Discuss
Hello,
I have a question about the Beta of 2.5.

I am attempting to pass an array of objects to my paramaterized test
so that the parameter of the test is an array of objects but I can't
quite seem to figure out how it is supposed to work.

Example
[TestCase(1, "s" , Result = "Int32;String;")]
[TestCase(1, "s" , true, Result = "Int32;String;Boolean")]
[TestCase(1, "s", 1.1, false , Result =
"Int32;String;Double;Boolean")]
public object ParametersArePassedCorrectly(object[] args)
{
var f = new Function();
return f.Call(2, args);
}

I have even tried something like
[TestCase(new object[] { 1, "s" } , Result = "Int32;String;")]
public object ParametersArePassedCorrectly(object[] args)
{
var f = new Function();
return f.Call(2, args);
}

In all cases NUnit indicates the test was not executed becase "Wrong
number of arguments provided".

Any ideas or is this a bug/limitation of the api?

Charlie Poole

unread,
Jan 12, 2009, 2:30:43 PM1/12/09
to nunit-...@googlegroups.com
Hi Mark,

> I am attempting to pass an array of objects to my
> paramaterized test so that the parameter of the test is an
> array of objects but I can't quite seem to figure out how it
> is supposed to work.
>
> Example
> [TestCase(1, "s" , Result = "Int32;String;")]
> [TestCase(1, "s" , true, Result = "Int32;String;Boolean")]
> [TestCase(1, "s", 1.1, false , Result =
> "Int32;String;Double;Boolean")]
> public object ParametersArePassedCorrectly(object[] args)
> {
> var f = new Function();
> return f.Call(2, args);
> }

This should not work, because you are passing, respectively, two,
three and four arguments to a method that only takes one.

> I have even tried something like
> [TestCase(new object[] { 1, "s" } , Result = "Int32;String;")]
> public object ParametersArePassedCorrectly(object[] args)
> {
> var f = new Function();
> return f.Call(2, args);
> }

This is intended to work, so we have a bug. I'm guessing that
new object[] { new object[] { 1, "s"} } will do what you want,
but that's not a satisfactory solution.

Could you file a bug on this - I'd like to fix it before releasing
the second beta. If you try the above workaround and let me know
what happens, that will be helpful.

Charlie

Mark

unread,
Jan 12, 2009, 4:23:14 PM1/12/09
to NUnit-Discuss
Hi Charlie,

> > I have even tried something like
> >         [TestCase(new object[] { 1, "s" } , Result = "Int32;String;")]
> >         public object ParametersArePassedCorrectly(object[] args)
> >         {
> >             var f = new Function();
> >             return f.Call(2, args);
> >         }
>
> This is intended to work, so we have a bug. I'm guessing that
> new object[] { new object[] { 1, "s"} } will do what you want,
> but that's not a satisfactory solution.
>
> Could you file a bug on this - I'd like to fix it before releasing
> the second beta. If you try the above workaround and let me know
> what happens, that will be helpful.

This workaround does work.

I tried to enter a bug on SF but it appears that anon reports are not
allowed and I don't have the time right now to setup a SF account.

-mark

Simone Busoli

unread,
Jan 12, 2009, 4:30:11 PM1/12/09
to nunit-...@googlegroups.com
I'd like to give this a look, I think it depends on the way the params keyword works in C#. I tried creating a TestCase with an object[] but the NUnit GUI marks that as invalid. I don't know the code except for the framework, any hits on where to look?

Charlie Poole

unread,
Jan 12, 2009, 4:37:20 PM1/12/09
to nunit-...@googlegroups.com
Hi Mark,

> > Could you file a bug on this - I'd like to fix it before
> releasing the
> > second beta. If you try the above workaround and let me know what
> > happens, that will be helpful.
>
> This workaround does work.

OK, then it's clear why it happens.

> I tried to enter a bug on SF but it appears that anon reports
> are not allowed and I don't have the time right now to setup
> a SF account.

I'll file it then. Thanks for the info.

Charlie

> -mark
> >
>



Charlie Poole

unread,
Jan 12, 2009, 4:43:59 PM1/12/09
to nunit-...@googlegroups.com
Sure... it's in the constructor of TestAttribute, which has overloads:
 
1:  TestAttribute( params object[] )
2:  TestAttribute( object )
3:  TestAttribute( object, object )
4:  TestAttribute( object, object, object )
 
2, 3 and 4 are for languages that don't know about params
 
In addition, 2 was intended to pick up the case where a single object[] parameter
was passed in. Obviously, it ain't working, which is understandable since
object[] is a closer match for object[] than object is.
 
I suggest looking at how RowAttribute handles this, because I remember that
Andreas had to deal with exactly the same issue. Then, write a test, make
it fail, make it pass. :-)
 
If you have time for this, I'm glad to leave it for you!
 
Charlie


From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of Simone Busoli
Sent: Monday, January 12, 2009 1:30 PM
To: nunit-...@googlegroups.com
Subject: [nunit-discuss] Re: Beta 2.5 and TestCase with array parameters

Charlie Poole

unread,
Jan 12, 2009, 4:45:36 PM1/12/09
to nunit-...@googlegroups.com
PS: If you are fixing this, please file the bug as well, so we remember to put it into the list of bugs fixed.
 
Charlie


From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of Charlie Poole
Sent: Monday, January 12, 2009 1:44 PM

Charlie Poole

unread,
Jan 12, 2009, 4:47:07 PM1/12/09
to nunit-...@googlegroups.com
Mark wrote:

> I tried to enter a bug on SF but it appears that anon reports
> are not allowed and I don't have the time right now to setup
> a SF account.

Note to self: allow anon bugs on our next bug tracker system :-)

Charlie

Simone Busoli

unread,
Jan 12, 2009, 4:47:53 PM1/12/09
to nunit-...@googlegroups.com
I was there already, what I see is the gui not being able to load that test.

Simone Busoli

unread,
Jan 12, 2009, 4:48:13 PM1/12/09
to nunit-...@googlegroups.com
I think sf does that already if you enable it.

Olof Bjarnason

unread,
Jan 12, 2009, 4:54:51 PM1/12/09
to nunit-...@googlegroups.com
Maybe choose an easier-to-use-issue tracker is the real deal ;)

2009/1/12 Simone Busoli <simone...@gmail.com>:
--
Min blogg:
http://olofb.wordpress.com
[My blog, in Swedish]

Olof Bjarnason

unread,
Jan 12, 2009, 4:55:40 PM1/12/09
to nunit-...@googlegroups.com
2009/1/12 Olof Bjarnason <olof.bj...@gmail.com>:
> Maybe choose an easier-to-use-issue tracker is the real deal ;)

And with that I simply mean I think sf.net is hard to use.. are there
any plans on changing to another issue-tracking-system? Eg.
launchpads?

Kelly Anderson

unread,
Jan 12, 2009, 5:12:17 PM1/12/09
to nunit-...@googlegroups.com
I'll second that one... :-)

-Kelly

Simone Busoli

unread,
Jan 12, 2009, 6:41:15 PM1/12/09
to nunit-...@googlegroups.com
I think the constructor which accepts a parameter array should be removed because once you're there you cannot distinguish between several arguments and a single argument of type object[].
Removing that constructor fixes the reported issue but breaks one test which supplies more than 3 arguments to the TestCaseAttribute constructor.

Simone Busoli

unread,
Jan 12, 2009, 7:09:17 PM1/12/09
to nunit-...@googlegroups.com
Here's the sourceforge issue: https://sourceforge.net/tracker2/?func=detail&aid=2503011&group_id=10749&atid=110749
Attached to the issue is a patch. The resolution ha not been to remove the constructor accepting a parameter array, but instead coalesce the attribute parameters in an object[] in case the test method has an argument of type object[].

Charlie Poole

unread,
Jan 12, 2009, 7:36:03 PM1/12/09
to nunit-...@googlegroups.com
I just enabled it. Of course, if we start getting spam, I'll have to revert to requiring login.
 
Charlie


From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of Simone Busoli
Sent: Monday, January 12, 2009 1:48 PM

To: nunit-...@googlegroups.com
Subject: [nunit-discuss] Re: Beta 2.5 and TestCase with array parameters

Charlie Poole

unread,
Jan 12, 2009, 7:51:41 PM1/12/09
to nunit-...@googlegroups.com
Hi Simone,
 
I'll have to take a look at your fix. We may need to do more if other tests are breaking.
 
If I remember correctly, we want to take a simple object[] as an argument by itself, IFF
the method takes an object[]. However - again IIRC - TestCaseAttribute doesn't know
anything about the method being called, so it can't do that. I'll look deeper.
 
Charlie


From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of Simone Busoli
Sent: Monday, January 12, 2009 4:09 PM

To: nunit-...@googlegroups.com
Subject: [nunit-discuss] Re: Beta 2.5 and TestCase with array parameters

Simone Busoli

unread,
Jan 13, 2009, 3:17:16 AM1/13/09
to nunit-...@googlegroups.com
No other tests are breaking :) Actually, with the approach I followed we get the benefit that even if you specify arguments individually in the constructor of the attribute and you have a test method which accepts an obejct[], the arguments are wrapped in the array. I added some tests to show this.

Charlie Poole

unread,
Jan 13, 2009, 4:24:51 AM1/13/09
to nunit-...@googlegroups.com
OK - I must have misunderstood you then. I'll look again. I see what you're saying about being able
to specify it either way.
 
Charlie


From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of Simone Busoli
Sent: Tuesday, January 13, 2009 12:17 AM

Simone Busoli

unread,
Jan 13, 2009, 4:38:59 AM1/13/09
to nunit-...@googlegroups.com
Oh I see what you mean,  previously I meant that I had to comment a test which supplied 4 arguments to the attribute constructor, because in the first place I had removed the constructor which takes a parameters array, but now it's all fine.

Charlie Poole

unread,
Jan 13, 2009, 12:04:53 PM1/13/09
to nunit-...@googlegroups.com
It's checked in. Thanks.
 
Charlie
 


From: nunit-...@googlegroups.com [mailto:nunit-...@googlegroups.com] On Behalf Of Simone Busoli
Sent: Tuesday, January 13, 2009 1:39 AM

Simone Busoli

unread,
Jan 13, 2009, 12:06:32 PM1/13/09
to nunit-...@googlegroups.com
Did you have to make any changes?

Charlie Poole

unread,
Jan 13, 2009, 12:27:16 PM1/13/09
to nunit-...@googlegroups.com
Hi Simone,

Yes, I changed

if (method.GetParameters().Length == 1 &&
method.GetParameters()[0].ParameterType == typeof(object[]))
parms.Arguments = new object[]{parms.Arguments};

to this

if (argsNeeded == 1 && method.GetParameters()[0].ParameterType ==
typeof(object[]))
{
if (parms.Arguments.Length > 1 ||
parms.Arguments.Length == 1 && parms.Arguments[0].GetType() !=
typeof(object[]))
{
parms.Arguments = new object[] { parms.Arguments };
}
}

making this new test pass as well...

[TestCase(new object[] { 1, "two", 3.0 })]
[TestCase(new object[] { "zip" })]
public void CanPassObjectArrayAsFirstArgument(object[] a)
{
}

The test does nothing but proves we can load and execute it
without an error. I like the convenience of your approach,
but some folks may insist on creating the array themselves.

Charlie
Reply all
Reply to author
Forward
0 new messages