Comment 1 for bug 1317605

Revision history for this message
Charlie Poole (charlie.poole) wrote : 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>
> /// 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.
>
> ** Affects: nunit-vs-adapter
> Importance: Undecided
> Status: New
>
> --
> You received this bug notification because you are subscribed to NUnit
> Extended Testing Platform.
> https://bugs.launchpad.net/bugs/1317605
>
> Title:
> Cannot see custom Attributes in Test Explorer
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunit-vs-adapter/+bug/1317605/+subscriptions