using TestCase attribute with arrays doesn't produce helpful output

Asked by cliff v.

If i have the following:

        [Test]
        [TestCase(new[]{2,1,2.1})]
        public void WithArray(params double [] prices)
        {
        }

It would be nice if NUnit could loop through the array and output it's contents and produce something like the following:

TestCase 'NUnitBugs.Tests.SampleTests.WithArray(System.Double[]{2,1,2.1})'
failed: TearDown : NUnit.Framework.AssertionException :
  Expected: 0.1m
  But was: 1.1m

instead of:

TestCase 'NUnitBugs.Tests.SampleTests.WithArray(System.Double[])'
failed: TearDown : NUnit.Framework.AssertionException :
  Expected: 0.1m
  But was: 1.1m

thanks

cliff

Question information

Language:
English Edit question
Status:
Answered
For:
NUnit Framework Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Charlie Poole (charlie.poole) said :
#1

I converted this to a question because it seems like you may have misunderstood how to use TestCase. Of course, we can reinstate the bug if I misunderstood what you want.

If you make the argument to your test method an array, then NUnit will pass the array to it. OTOH, if you want NUnit to call your method multiple times with different arguments, you should use double as the arg type and either have multiple TestCase attributes(1) or use a ValuesAttribute on the parameter(2)

(1)
[TestCase(2)]
[TestCase(1)]
[TestCase(1.1)]
public void MyTest(double price) { ... }

(2)
public void MyTest(
   [Values(2,1,1.1)] double price ) { ... }

Revision history for this message
cliff v. (powerdude) said :
#2

Charlie,

I guess i should have included a better example. This is what I would have:

       [Test]
       [TestCase(new[]{2,1,2.1})]
       [TestCase(new[]{1,2,3,4,5})]
       public void WithArray(double [] prices)
       {
       }

i would be writing code that actually needed a list of numbers to process,
so your examples wouldn't help me.

hope this helps clear up my request.

cliff

On Fri, Oct 30, 2009 at 6:41 PM, Charlie Poole <
<email address hidden>> wrote:

> Your question #87575 on NUnit Framework changed:
> https://answers.launchpad.net/nunit-3.0/+question/87575
>
> Charlie Poole posted a new comment:
> I converted this to a question because it seems like you may have
> misunderstood how to use TestCase. Of course, we can reinstate the bug
> if I misunderstood what you want.
>
> If you make the argument to your test method an array, then NUnit will
> pass the array to it. OTOH, if you want NUnit to call your method
> multiple times with different arguments, you should use double as the
> arg type and either have multiple TestCase attributes(1) or use a
> ValuesAttribute on the parameter(2)
>
> (1)
> [TestCase(2)]
> [TestCase(1)]
> [TestCase(1.1)]
> public void MyTest(double price) { ... }
>
> (2)
> public void MyTest(
> [Values(2,1,1.1)] double price ) { ... }
>
> --
> You received this question notification because you are a direct
> subscriber of the question.
>

--
thanks

cliff

Revision history for this message
cliff v. (powerdude) said :
#3

a bit more, NUnit does pass the arrays fine, but when the test fails, i have
no idea which test case failed. I know I could output the array parameter
myself using a Debug.WriteLine() or constructing a failure message, but
that'll just cloud up the output by seeing a bunch of messages for
non-failures or add extra info to a failure message that I would prefer not
to add.

cliff

On Sat, Oct 31, 2009 at 10:18 AM, cliff <email address hidden> wrote:

> Charlie,
>
> I guess i should have included a better example. This is what I would
> have:
>
>
> [Test]
> [TestCase(new[]{2,1,2.1})]
> [TestCase(new[]{1,2,3,4,5})]
> public void WithArray(double [] prices)
> {
> }
>
> i would be writing code that actually needed a list of numbers to process,
> so your examples wouldn't help me.
>
> hope this helps clear up my request.
>
> cliff
>
>
> On Fri, Oct 30, 2009 at 6:41 PM, Charlie Poole <
> <email address hidden>> wrote:
>
>> Your question #87575 on NUnit Framework changed:
>> https://answers.launchpad.net/nunit-3.0/+question/87575
>>
>> Charlie Poole posted a new comment:
>> I converted this to a question because it seems like you may have
>> misunderstood how to use TestCase. Of course, we can reinstate the bug
>> if I misunderstood what you want.
>>
>> If you make the argument to your test method an array, then NUnit will
>> pass the array to it. OTOH, if you want NUnit to call your method
>> multiple times with different arguments, you should use double as the
>> arg type and either have multiple TestCase attributes(1) or use a
>> ValuesAttribute on the parameter(2)
>>
>> (1)
>> [TestCase(2)]
>> [TestCase(1)]
>> [TestCase(1.1)]
>> public void MyTest(double price) { ... }
>>
>> (2)
>> public void MyTest(
>> [Values(2,1,1.1)] double price ) { ... }
>>
>> --
>> You received this question notification because you are a direct
>> subscriber of the question.
>>
>
>
>
> --
> thanks
>
> cliff
>

--
thanks

cliff

Revision history for this message
Charlie Poole (charlie.poole) said :
#4

How are you running the tests - gui or console?

Revision history for this message
cliff v. (powerdude) said :
#5

TD.NET

On Sat, Oct 31, 2009 at 2:22 PM, Charlie Poole <
<email address hidden>> wrote:

> Your question #87575 on NUnit Framework changed:
> https://answers.launchpad.net/nunit-3.0/+question/87575
>
> Status: Open => Answered
>
> Charlie Poole proposed the following answer:
> How are you running the tests - gui or console?
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
>
> https://answers.launchpad.net/nunit-3.0/+question/87575/+confirm?answer_id=3
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https://answers.launchpad.net/nunit-3.0/+question/87575
>
> You received this question notification because you are a direct
> subscriber of the question.
>

--
thanks

cliff

Revision history for this message
Charlie Poole (charlie.poole) said :
#6

OK, I thought about it some more and I understand the problem now. I suggest you just provide a separate TestName on the TestCaseAttribute... for example...

  [TestCase(new double[](2,1,2.1), TestName="Case1")]

or even

  [TestCase(new double[](2,1,2.1), TestName="2,1,2.1")]

An alternative might be to use a params array as an argument, but I strongly suspect that NUnit won't handle it.

Can you help with this problem?

Provide an answer of your own, or ask cliff v. for more information if necessary.

To post a message you must log in.