Cannot see custom Attributes in Test Explorer

Bug #1317605 reported by Ryan Ternier
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit Test Adapter
Invalid
Undecided
Unassigned

Bug Description

I've tried to create a custom attribute based on the NUnit Category Attribute:

// <summary>
        /// Attribute used to apply a requirement to a test
        /// </summary>
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Assembly, AllowMultiple = true, Inherited = true)]
        public class RequirementAttribute : Attribute
        {
            /// <summary>
            /// The name of the requirement
            /// </summary>
            protected string requirementName;

            /// <summary>
            /// Construct attribute for a given category based on
            /// a name. The name may not contain the characters ',',
            /// '+', '-' or '!'. However, this is not checked in the
            /// constructor since it would cause an error to arise at
            /// as the test was loaded without giving a clear indication
            /// of where the problem is located. The error is handled
            /// in NUnitFramework.cs by marking the test as not
            /// runnable.
            /// </summary>
            /// <param name="name">The name of the requirement</param>
            public RequirementAttribute(string name)
            {
                this.requirementName = name.Trim();
            }

            /// <summary>
            /// Protected constructor uses the Type name as the name
            /// of the category.
            /// </summary>
            protected RequirementAttribute()
            {
                this.requirementName = this.GetType().Name;
                if (requirementName.EndsWith("Attribute"))
                    requirementName = requirementName.Substring(0, requirementName.Length - 9);
            }

            /// <summary>
            /// The name of the requirement
            /// </summary>
            public string Name
            {
                get { return requirementName; }
            }
        }

This compiles, but it doesn't display in VS 2013's test Explorer.

I've then tried:

 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public class RequirementsAttribute : NUnit.Framework.PropertyAttribute
    {
        public RequirementsAttribute(string[] requirements)
            : base(requirements)
        {
        }

    }

Which does display in the Test Explorer, but it only shows:

Requirement[String[]] (5) rather than the string value.

Revision history for this message
Charlie Poole (charlie.poole) wrote : Re: [Bug 1317605] [NEW] Cannot see custom Attributes in Test Explorer
Download full text (4.2 KiB)

Your first code sample isn't really an NUnit attribute at all... it's
a copy of one. NUnit will not recognize it at all.

The second sample uses PropertyAttribute as the base. That's an NUnit
attribute, which is designed to be inherited from, so NUnit recognizes
it. Of course, it's not a CategoryAttribute, so it would not work for
selecting tests outside of Visual Studio. It does show up in
TestExplorer, because the NUnit adapter translates all NUnit
Properties to Traits for VS. NUnit uses ToString() to provide VS with
a value for the Trait, which is where String[] (5) comes from.

I see two alternatives for you...

1. Inherit from CategoryAttribute, since what you want is actually a
category anyway. Allow multiple attributes, just as category attribute
does, rather than trying to handle many requirements in a single
attribute.

2. If you prefer to stick with a PropertyAttribute, create multiple
values for the property in the constructor, adding each one separately
to the Properties of the attribute rather than allowing the base class
to do it.

I'm rejecting this as an nunit-adapter bug because (1) the behavior
you are seeing is part of NUnit, not the adapter and (2) it's by
design.

Charlie

On Thu, May 8, 2014 at 10:49 AM, Ryan Ternier <email address hidden> wrote:
> Public bug reported:
>
> I've tried to create a custom attribute based on the NUnit Category
> Attribute:
>
> // <summary>
> /// Attribute used to apply a requirement to a test
> /// </summary>
> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Assembly, AllowMultiple = true, Inherited = true)]
> public class RequirementAttribute : Attribute
> {
> /// <summary>
> /// The name of the requirement
> /// </summary>
> protected string requirementName;
>
> /// <summary>
> /// Construct attribute for a given category based on
> /// a name. The name may not contain the characters ',',
> /// '+', '-' or '!'. However, this is not checked in the
> /// constructor since it would cause an error to arise at
> /// as the test was loaded without giving a clear indication
> /// of where the problem is located. The error is handled
> /// in NUnitFramework.cs by marking the test as not
> /// runnable.
> /// </summary>
> /// <param name="name">The name of the requirement</param>
> public RequirementAttribute(string name)
> {
> this.requirementName = name.Trim();
> }
>
> /// <summary>
> /// Protected constructor uses the Type name as the name
> /// of the category.
> /// </summary>
> protected RequirementAttribute()
> {
> this.requirementName = this.GetType().Name;
> if (requirementName.EndsWith("Attribute"))
> requirementName = requirementName.Substring(0, requirementName.Length - 9);
> }
>
> /// <summary>
> /// The name of the requir...

Read more...

Changed in nunit-vs-adapter:
status: New → Invalid
Revision history for this message
Charlie Poole (charlie.poole) wrote :

I initially replied by email. Now that I see you filed this bug on Launchpad, I'd like to point out that we no longer handle bugs here.

See https://launchpad.net/nunit-vs-adapter

Revision history for this message
Ryan Ternier (ryan-ternier) wrote :
Download full text (8.7 KiB)

Hey Charlie,
Thanks for the reply on this.
I've done what you suggested, the issue that I see is in the Test Explorer, it groups these Requirements under the "Category" name, rather than the "Requirement" name I've specified in the attribute.

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class RequirementAttribute : CategoryAttribute
{

    protected string requirementName;

    public RequirementAttribute(string name)
    {
        this.requirementName = name.Trim();
    }

    protected RequirementAttribute()
    {
        this.requirementName = this.GetType().Name;
        if (requirementName.EndsWith("Attribute"))
            requirementName = requirementName.Substring(0, requirementName.Length - 9);
    }

    public new string Name
    {
        get { return requirementName; }
    }
}

Shows the following in the Test Explorer:
Category["1234"] (1)Category["abcd"] (1)etc.
rather than:
Requirement ["1234"] (1)etc.
Thoughts?
/RT

Ryan Ternier

> Date: Thu, 8 May 2014 23:05:21 +0000
> From: <email address hidden>
> To: <email address hidden>
> Subject: Re: [Bug 1317605] [NEW] Cannot see custom Attributes in Test Explorer
>
> Your first code sample isn't really an NUnit attribute at all... it's
> a copy of one. NUnit will not recognize it at all.
>
> The second sample uses PropertyAttribute as the base. That's an NUnit
> attribute, which is designed to be inherited from, so NUnit recognizes
> it. Of course, it's not a CategoryAttribute, so it would not work for
> selecting tests outside of Visual Studio. It does show up in
> TestExplorer, because the NUnit adapter translates all NUnit
> Properties to Traits for VS. NUnit uses ToString() to provide VS with
> a value for the Trait, which is where String[] (5) comes from.
>
> I see two alternatives for you...
>
> 1. Inherit from CategoryAttribute, since what you want is actually a
> category anyway. Allow multiple attributes, just as category attribute
> does, rather than trying to handle many requirements in a single
> attribute.
>
> 2. If you prefer to stick with a PropertyAttribute, create multiple
> values for the property in the constructor, adding each one separately
> to the Properties of the attribute rather than allowing the base class
> to do it.
>
> I'm rejecting this as an nunit-adapter bug because (1) the behavior
> you are seeing is part of NUnit, not the adapter and (2) it's by
> design.
>
> Charlie
>
> On Thu, May 8, 2014 at 10:49 AM, Ryan Ternier <email address hidden> wrote:
> > Public bug reported:
> >
> > I've tried to create a custom attribute based on the NUnit Category
> > Attribute:
> >
> > // <summary>
> > /// Attribute used to apply a requirement to a test
> > /// </summary>
> > [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Assembly, AllowMultiple = true, Inherited = true)]
> > public class RequirementAttribute : Attribute
> > {
> > /// <summary>
> > /// The name of the requirement
> > /// </summary>
> > protected string requirementName;
> >
> > /// <summary>
> >...

Read more...

Revision history for this message
Terje Sandstrom (b-tfrje-j) wrote :
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.