NUnit - Multiple properties of the same name? Linking to requirements

梦想与她 提交于 2019-12-05 21:54:05

Have you considered a custom property attribute derived from NUnit.Framework.Property?

Something like the following seems like it might work for you judging by a LINQPad 4 "query" with Language set to C# Program and a reference to nunit.framework.dll (version 2.4.8) added:

// main method to exercise a our PoC test case
void Main()
{
    TestSomething("a", "b");
}

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

// a test case using our custom property attribute to relate it to multiple requirements
[Requirements(new string[] { "FR50082", "FR50084" })]
[TestCase("PoCTest")]
public void TestSomething(string a, string b)
{
    // blah, blah, blah

    Assert.AreNotEqual(a, b);
}

For some reason I could not add the link to the google discussion post to the comment above, so I have added the post here too. (The link is https://groups.google.com/forum/#!topic/nunit-discuss/ndV3VTPndck)

  1. A Category is always displayed in TE as "Category [xxxxxxx]", where xxxxx is whatever string you send in, if you don't specify any, it will be the name of the class derived from CategoryAttribute.

  2. If you want to use Category, you should, as Charlie said on the google post, use one entry per requirement. If you add the string Requirement to the value, it can look pretty good, but most follow the rules in (1) above, it could be like:

Category[Requirement:FR12345]

Code:

public class RequirementAttribute : CategoryAttribute
{
    public RequirementAttribute(string s)
        : base("Requirement:" + s)
    { }
}
  1. If you want it to display like : Requirement[FR12345], then you MUST use a Property, but you can't have multiple Keys, so only one such per test.

Code:

public class RequirementAttribute : PropertyAttribute
{
    public RequirementAttribute(string s)
        : base(s)
    {}
}

4: If you want to have multiple requirements per test and still have something like the display in (3), you must make the keys unique. It doesn't need to look too bad. In the code below I have just added a counter to it. It will display as :

Requirement-1[FR12345]

Requirement-2[FR23456]

Code:

 public class RequirementAttribute : PropertyAttribute
    {
       public RequirementAttribute(string[] array)
       {
           int i = 0;
           foreach (var s in array)
           {
               Properties.Add("Requirement-" + i, s);
               i++;
           }
       }
    }

and you use it like:

 [Requirement(new[] { "1234", "2345" })]
   [Test]
   public void Test()
   { }

(And if you want a syntax without the "new", the previous answer show that syntax with the param instead.)

Option 4 will not group by requirement number, but by the counter. If you want to group by requirement number you can use option 5:

5. Add the requirement number to the key, but leave the value blank. It will look like:

Requirement-FR12345

In this way, you also skip the prefix and have each requirement as its own kind of category in the TE.

Code:

public class RequirementAttribute : PropertyAttribute
{
    public RequirementAttribute(string[] array)
    {
        foreach (var s in array)
        {
            Properties.Add("Requirement-" +  s,"");
        }
    }
}

And, you could of course also skip the prefix altogether.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!