Make CategoryAttribute inherited

Bug #655882 reported by Edward McTighe
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit Framework
Fix Released
Critical
Charlie Poole
NUnit V2
Fix Released
Medium
Charlie Poole

Bug Description

The attribute Category (used for making tests as being in a particular category) should be inherited. This would allow setting up an abstract base class for a certain type of test, marking it as [Category("Whatever")], and having NUnit recognize that every other test that inherits from that class is also in category "Whatever".

As of 2.5.7.10213, CategoryAttribute is not inherited.

Related branches

Revision history for this message
Charlie Poole (charlie.poole) wrote : Re: [Bug 655882] [NEW] Make CategoryAttribute inherited

While there are some use patterns where you would want this, there are
others where you would not. We can look into this, but first please try
the following workaround first.

Derive your own attribute from CategoryAttribute... e.g.: InheritableCategory.
Allow your attribute to be inherited.
Use it on the base class

Please let me know if this works for you.

Charlie

On Wed, Oct 6, 2010 at 11:20 AM, Edward McTighe
<email address hidden> wrote:
> Public bug reported:
>
> The attribute Category (used for making tests as being in a particular
> category) should be inherited. This would allow setting up an abstract
> base class for a certain type of test, marking it as
> [Category("Whatever")], and having NUnit recognize that every other test
> that inherits from that class is also in category "Whatever".
>
> As of 2.5.7.10213, CategoryAttribute is not inherited.
>
> ** Affects: nunitv2
>     Importance: Undecided
>         Status: New
>
>
> ** Tags: feature
>
> --
> Make CategoryAttribute inherited
> https://bugs.launchpad.net/bugs/655882
> You received this bug notification because you are a member of NUnit
> Developers, which is subscribed to NUnit V2.
>

Revision history for this message
Edward McTighe (edwardcmctighe) wrote :

Thanks for the suggestion; that actually works out quite well, so I'm going to go with that and consider this problem solved. It does seem like something that would be nice to have available in NUnit, but the workaround is easy enough. Thanks again!

Revision history for this message
Charlie Poole (charlie.poole) wrote :

I'm going to move this to the 3.0 project as a wishlist item.

Changed in nunitv2:
status: New → Triaged
importance: Undecided → Wishlist
affects: nunitv2 → nunit-3.0
tags: added: framework
Revision history for this message
Alex Franz (afranz409) wrote :

Hi, I've run into this situation too. I've found that the suggested solution works out fine in Rsharper's NUnit testing framework, but with the normal UI and command line nunit runners the inherited categories are not picked up by the tool. Any ideas why or where to look on this?

Revision history for this message
Charlie Poole (charlie.poole) wrote :

Can you provide a code snippet illustrating the problem?

Revision history for this message
Alex Franz (afranz409) wrote :

I'm using a 'InheritableCategoryAttribute', as below:

using System;
using NUnit.Framework;

namespace Apt.Lib.Product.Data.Tests
{
 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
 public class InheritableCategoryAttribute : CategoryAttribute
 {
  public InheritableCategoryAttribute(string name) : base(name)
  {
  }
 }
}

This attribute is applied to an abstract class, which provides some general functionality needed by all of my test fixtures:

    [TestFixture]
    [InheritableCategory("MyCategory")]
    public abstract class MyAbstractTests

What I would expect, is that any class extending MyAbstractTests should be found in MyCategory. In practice, this is the case in the Resharper's unit testing interface, but not in the NUnit GUI and Command line. As a result, I'm having trouble using this in a CI environment with Hudson

Revision history for this message
Charlie Poole (charlie.poole) wrote : Re: [Bug 655882] Re: Make CategoryAttribute inherited

You're correct. There is no workaround because NUnit does not look at
inherited attributes
with a few exceptions - TestFixture for example.

I'll raise the question of changing this behavior globally on the mail
list. If folks see no
problem with it, we can make the change.

Charlie

On Wed, Nov 3, 2010 at 12:04 PM, Alex Franz <email address hidden> wrote:
> I'm using a 'InheritableCategoryAttribute', as below:
>
> using System;
> using NUnit.Framework;
>
> namespace Apt.Lib.Product.Data.Tests
> {
>        [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
>        public class InheritableCategoryAttribute : CategoryAttribute
>        {
>                public InheritableCategoryAttribute(string name) : base(name)
>                {
>                }
>        }
> }
>
>
> This attribute is applied to an abstract class, which provides some general functionality needed by all of my test fixtures:
>
>    [TestFixture]
>    [InheritableCategory("MyCategory")]
>    public abstract class MyAbstractTests
>
>
> What I would expect, is that any class extending MyAbstractTests should be found in MyCategory.  In practice, this is the case in the Resharper's unit testing interface, but not in the NUnit GUI and Command line.  As a result, I'm having trouble using this in a CI environment with Hudson
>
> --
> Make CategoryAttribute inherited
> https://bugs.launchpad.net/bugs/655882
> You received this bug notification because you are subscribed to NUnit
> Framework.
>

Revision history for this message
Charlie Poole (charlie.poole) wrote :
Revision history for this message
Charlie Poole (charlie.poole) wrote :

I'm adding this back to NUnitV2 as well as 3.0. We'll implement it in the 2.5.9 release.

Planned changes...

* All NUnit attributes will have the AttributeUsageAttribute Inherited named parameter set explicitly.
    * Attributes intended to provide a unique value will use Inherited=false. For example, NUnit
      can only deal with one description per test, so Description will not be inheritable.
    * Attributes that can have multiple values, like Category, will use Inherited=true
    * Attributes that can be reused with no problem, like Explicit, will use Inherited=true
    * In spite of the above, any attributes for which there is a possible breaking problem, will use Inherited=false

* NUnit will fetch attributes using GetCustomAttributes(true), which will retrieve any base class
  attributes that have Inherited=true and ignore those with Inherited=false. (This is a change,
  so it's the place where we will see any breaking problems.)

Charlie

Changed in nunitv2:
status: New → Triaged
importance: Undecided → Medium
Changed in nunit-3.0:
importance: Wishlist → Medium
Changed in nunitv2:
assignee: nobody → Charlie Poole (charlie.poole)
milestone: none → 2.5.9
Changed in nunitv2:
status: Triaged → Fix Committed
Changed in nunit-3.0:
importance: Medium → Critical
Changed in nunitv2:
status: Fix Committed → Fix Released
Changed in nunit-3.0:
milestone: none → 2.9.6
Changed in nunit-3.0:
assignee: nobody → Charlie Poole (charlie.poole)
Changed in nunit-3.0:
status: Triaged → In Progress
Changed in nunit-3.0:
status: In Progress → Fix Committed
Changed in nunit-3.0:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.