Combinatorial and TestCaseSource or ValueSource

1,848 views
Skip to first unread message

BlueZora

unread,
Apr 24, 2012, 10:14:22 AM4/24/12
to nunit-...@googlegroups.com
Hi,
im writing some tests with nunit for about a month and now ive encountered my first big problem.

i want to test a method with 2 input parameters. i have to specify these parameters dynamicly using an Interface, because unfortunately they are dependent on which camera is connected. And i want to do combinatorial tests, so i can test each parameter against the other one.
but no matter what i tried it just didnt work...
no matter if i tried with valuesource or with testcasesource..

here is my code sofar:

namespace Projekt9
{
    [TestFixture]
    public class ExceptionHandlingTests
    {
       [Test,Combinatorial]
       public void GainSetdouble([ValueSource(typeof(Parameter),"TCGainSetDoubleGain")] Gain g,[ValueSource(typeof(Parameter),"TCGainSetDoubleDouble")] double     d)        
       {
             vfucam.GainSet(g, d);
       }
    }
}

namespace Projekt9
{
    class Parameter
    {
        KameraInit cam = new KameraInit();
        public bool tmpbool;
public IEnumerable TCGainSetDoubleGain  //(Overloaded) Gain control 
       {
           get
           {
               yield return new TestCaseData(Gain.Blue);
               yield return new TestCaseData(Gain.Global);
               yield return new TestCaseData(Gain.Green);
               yield return new TestCaseData(Gain.GreenBlue);
               yield return new TestCaseData(Gain.GreenRed);
               yield return new TestCaseData(Gain.Red);
           }
       }
       public IEnumerable TCGainSetDoubleDouble
       {
           get
           {
               VFU.SelfDescribingInformation.GainInfo ginfo = new VFU.SelfDescribingInformation.GainInfo();
               cam.vfucam.GainSet(Gain.Red, 10, out tmpbool, out ginfo);

               yield return new TestCaseData(ginfo.gainFactor.MinValue-1);
               yield return new TestCaseData(ginfo.gainFactor.MinValue);
               yield return new TestCaseData(ginfo.gainFactor.MaxValue / 2);
               yield return new TestCaseData(ginfo.gainFactor.MaxValue);
               yield return new TestCaseData(ginfo.gainFactor.MaxValue + 1);
           }
       }
   }
}


And the only thing Nunit Gui is indicating is 30 Tests which look like:
Projekt9.ExceptionHandlingTests.GainSetdouble(NUnit.Framework.TestCaseData,NUnit.Framework.TestCaseData)

So, do you have an idea what im doing wrong and how i can fix it?

Oh, Doing Combinatorial or TestCaseSource alone is working perfectly

Thank you in advance
BlueZora

Charlie Poole

unread,
Apr 24, 2012, 3:18:55 PM4/24/12
to nunit-...@googlegroups.com
As a first step to a solution, you should eliminate all the use of
TestCaseData in your code. A TestCaseData object is intended to return
a test case, that is, an object containing _all_ the arguments to a
function. It's only used in conjunction with TestCaseSource, when you
are creating test cases yourself. In your situation, you are asking
NUnit to create the test cases combinatorially.

Secondly, I'm a bit confused at what you are trying to do. You say you
need to specify the parameters dynamically, but they are actually
hard-coded in your example. Could you clarify this?

Charlie

> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nunit-discuss/-/a77l0emESmsJ.
> To post to this group, send email to nunit-...@googlegroups.com.
> To unsubscribe from this group, send email to
> nunit-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nunit-discuss?hl=en.

BlueZora

unread,
Apr 25, 2012, 3:19:54 AM4/25/12
to nunit-...@googlegroups.com
hi, thanks for your answer.

well that whats written there is my original, where i only used [Test,TestCaseSource(typeof(Parameter),"TCGainSetDoubleDouble")] mixed with some of my thoughts in how it could work with combinatorial, cause ive read something about valuesource an thought it might work.

The parameters are not it that specific way dynamic, but rather in that way, that they depend on the camera-type which is connected. the method im using to get those parameters is from an sdk. i first have to find out which camera is plugged in, this i do with an extra class from which i declare an object in [SetUp]. Because i have to do this first, it doesnt work if i get those parameters in, for example [TestCase( )]'s . But when i get my parameters from an interface, it works well, and nunit recognizes it.

heres what im doing if i want to test a method with just one input parameter:
[TestFixture]
public class ExceptionHandlingTests
{
    [Test, TestCaseSource(typeof(Parameter),"TCAutoBrightnessSpeedFactorSet")]
        public void AutoBrightnessSpeedFactorSet(double d)
        {  }
}

class Parameter
{
    public IEnumerable TCAutoBrightnessTargetBrightnessSet
        {
            get
            {
                yield return new TestCaseData(abtbinfo.TargetBrightness.MinValue - 1);
                yield return new TestCaseData(abtbinfo.TargetBrightness.MinValue);
                yield return new TestCaseData(abtbinfo.TargetBrightness.MaxValue / 2);
                yield return new TestCaseData(abtbinfo.TargetBrightness.MaxValue);
                yield return new TestCaseData(abtbinfo.TargetBrightness.MaxValue + 1);
            }
        }
}

But now i want to test a method with two input parameter. the first one is the Gain which can be several parameters and the second one is a double value. Because this two depend on the camera which is plugged in i have to get those parameters with interfaces.
And i want to do a combinatorial test with these two.

Hopefully i could make my aim more clear
BlueZora

> nunit-discuss+unsubscribe@googlegroups.com.

Charlie Poole

unread,
Apr 25, 2012, 1:32:34 PM4/25/12
to nunit-...@googlegroups.com
Hi BZ,

Let me try to clarify again: at the moment you decided to use
Combinatorial, you could no longer use
TestCase/TestCaseSource/TestCaseData.

The choice is between...
1) Creating combinations yourself
2) Having NUnit create combinations

Choosing #2, then NUnit will create test cases, using it's internal
equivalent of TestCaseData to do so.

So, your choice is now between ValuesAttribute and
ValueSourceAttribute. Here is a perfectly good example using
ValuesAttribute...

[TestFixture]
public class ExceptionHandlingTests
{
[Test, Combinatorial]
public void GainSetdouble(
[Values(Gain.Red, Gain.Blue, Gain.Green] Gain g,
[Values(2.0, 1.0, 0.0)]
{
vfucam.GainSet(g, d);
}
}

This, of course, does not solve your problem with multiple cameras,
but it would be your starting point, rather than the approach using
TestCaseData, _if_ Nunit is going to create all the combinations. From
this, you could move on to ValueSource and put some intelligence into
the selection of parameters, as you have tried to do.

Here is your basic choice, expressed in simple form...

1) Use ValueSource and make the generation of arguments dependent on a
camera selected at a higher level. Let NUnit combine them.
2) Use TestCaseSource and generate pairs of arguments dependent on a
camera selected at a higher level. NUnit doesn't need to combine args
because you have already done it.

The "higher level" I refer to in both cases has to be completely
outside your tests. This is because the arguments are generated long
before your tests are even executed. It could be something as simple
as setting an environment variable before running the tests.

When we talk about "dynamic" tests in NUnit, we mean test cases that
are _not_ generated in advance but at the time of executing the tests.
That's what you are really looking for but, unfortunately, it doesn't
exist. It's a feature planned for NUnit 3.0.

Charlie
>> > nunit-discus...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/nunit-discuss?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nunit-discuss/-/RnG0VQKY7fgJ.
>
> To post to this group, send email to nunit-...@googlegroups.com.
> To unsubscribe from this group, send email to
> nunit-discus...@googlegroups.com.

BlueZora

unread,
Apr 26, 2012, 3:05:07 AM4/26/12
to nunit-...@googlegroups.com
Hi charlie,

thank you very much for your help.
i will try later to do so.

even if its not possible yet, i am happy to know, so i dont have to search for it anymore and can concentrate on a solution which matchs best.

BlueZora

Reply all
Reply to author
Forward
0 new messages