Parameterized fixtures and categories

73 views
Skip to first unread message

Oleg

unread,
Dec 14, 2010, 1:36:04 PM12/14/10
to NUnit-Discuss
Hello,

I am looking for a way to segregate fixtures based on categories, but
cannot get this to work. Ideal code:

[TestFixture("string1"), Category("A")]
[TestFixture("string2"), Category("B")]

It seems that NUnit is using whatever category "works". For example,
in NUnit GUI runner, selecting category A or B runs tests from both A
and B.

Any tips on how to make this work? I would like a simple way to
control data and be able to select what to execute based on
categories. Thanks!

Charlie Poole

unread,
Dec 14, 2010, 2:39:40 PM12/14/10
to nunit-...@googlegroups.com
Hi Oleg,

The problem is that the C# syntax appears to suggest that the pairs
of attributes are associated in some way, but they actually aren't.

This...


[TestFixture("string1"), Category("A")]
[TestFixture("string2"), Category("B")]

is equivalent to this...
[TestFixture("string1")]
[Category("A")]
[TestFixture("string2")]
[Category("B")]
and also to this...
[TestFixture("string1")]
[TestFixture("string2")]
[Category("A")]
[Category("B")]
or even this...
[Category("B")]
[TestFixture("string1")]
[TestFixture("string2")]
[Category("A")]

All the attributes are applied to the element that follow them - the
class itself. So, the entire class gets both category attributes and the
two instances that NUnit creates using the TestFixture attributes also
have both categories. There is no way NUnit could do it any differently,
because this is simply how attributes work.

We could, however, provide a property on TestFixtureAttribute to
allow you to set the category for each instance separately. Then
you would use a syntax like...
[TestFixture("string1", Category="A")]
[TestFixture("string2", Category="B")]
We don't have such a property right now, but we have others on both
TestFixtureAttribute and TestCaseAttribute. If you would like to request
it as a feature at http://bugs.launchpad.net/nunitv2, we'll see about
adding it in a future release.

Charlie

> --
> You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
> 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.
>
>

David Jeske

unread,
Dec 14, 2010, 8:15:20 PM12/14/10
to nunit-...@googlegroups.com
I get this done today using a combination of different namespaces, different classnames, and prefixes on the two. 

For example, my classes are generally named A##_TestClass, while the tests are named T##_TestFunction. The ## are numbers, to get them to group in an order that makes sense in the NUnit UI. 

The tests are not dependent on being run in this order, but I find it convenient for "simpler" test to run and fail first, because more complicated tests that will obviously fail if the simple ones failed. 


On Tue, Dec 14, 2010 at 10:36 AM, Oleg <oleg.g...@gmail.com> wrote:

Charlie Poole

unread,
Dec 14, 2010, 8:31:42 PM12/14/10
to nunit-...@googlegroups.com
Hi David,

I'm not sure how this relates to the issue of Category selection.

Charlie

David Jeske

unread,
Dec 14, 2010, 8:35:50 PM12/14/10
to nunit-...@googlegroups.com
On Tue, Dec 14, 2010 at 5:31 PM, Charlie Poole <nuni...@gmail.com> wrote:
I'm not sure how this relates to the issue of Category selection.

[TestFixture("string1")]
class A02_CategoryA_Tests {
}

Charlie Poole

unread,
Dec 14, 2010, 8:44:11 PM12/14/10
to nunit-...@googlegroups.com
Nope, that doesn't do the job. Oleg wanted to use the same fixture
twice, with different arguments and different categories. Of course,
he could copy the class code and create a separate one for
category B, but that doesn't scale very well. :-)

Charlie

David Jeske

unread,
Dec 14, 2010, 8:51:31 PM12/14/10
to nunit-...@googlegroups.com
On Tue, Dec 14, 2010 at 5:44 PM, Charlie Poole <nuni...@gmail.com> wrote:
Nope, that doesn't do the job. Oleg wanted to use the same fixture
twice, with different arguments and different categories.

Ahh! Thanks for clearing that up. I see now I didn't understand his original email. 
 
Of course, he could copy the class code and create a separate one for
category B, but that doesn't scale very well. :-)

Actually, I have run into this problem before, and for me it was more than just wanting it listed in a different category. I actually want to run all the same tests in a different context. 

Is it possible to use a common base-class without the TestFixture to solve this? For example:

class TestBase {
  [Test]
  void TestA() { .. };
}

[TestFixture]
class TestCategoryA : TestBase { }

[TestFixture]
class TestCategoryB : TestBase { }


Charlie Poole

unread,
Dec 14, 2010, 9:08:42 PM12/14/10
to nunit-...@googlegroups.com
Hi David,

Yes, I'd do that if the differences in context were not easily
parameterized. You'll find that the use of parameters is somewhat
weaker for test fixtures than it is for test methods, so this is
often needed.

Charlie

Oleg

unread,
Dec 16, 2010, 10:52:25 AM12/16/10
to NUnit-Discuss
Hi Charlie,
Thanks very much for that detailed answer. Exactly what i was looking
for. I filed a request per your instructions:
https://bugs.launchpad.net/nunitv2/+bug/691129
> it as a feature athttp://bugs.launchpad.net/nunitv2, we'll see about

Kenneth Xu

unread,
Dec 27, 2010, 4:22:27 PM12/27/10
to nunit-...@googlegroups.com
A little late response, but my work around for now is:

abstract class TheTestClass
{
    protected MyParameteredTest(string s) {}

    ... ....

    [Category("A")] class ForString1 : TheTestClass { public ForString1() : base("string1") {} }
    [Category("B")] class ForString2 : TheTestClass { public ForString2() : base("string2") {} }
}

My 2 cents,
Kenneth
Reply all
Reply to author
Forward
0 new messages