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

让人想犯罪 __ 提交于 2019-12-07 13:44:46

问题


I'm linking all our our System Tests to test cases and to our Requirements. Every requirement has an ID. Every Test Case / System Tests tests a variety of requirements. Every module of code links to multiple requirements.

I'm trying to find the best way to link every system test to its driving requirements.

I was hoping to do something like:

    [NUnit.Framework.Property("Release", "6.0.0")]
    [NUnit.Framework.Property("Requirement", "FR50082")]
    [NUnit.Framework.Property("Requirement", "FR50084")]
    [NUnit.Framework.Property("Requirement", "FR50085")]
    [TestCase(....)]
    public void TestSomething(string a, string b...)

However, that will break because Property is a Key-Value pair. The system will not allow me to have multiple Properties with the same key.

The reason I'm wanting this is to be able to test specific requirements in our system if a module changes that touches these requirements.

Rather than run over 1,000 system tests on every build, this would allow us to target what to test based on changes done to our code.

Some system tests run upwards of 5 minutes (Enterprise healthcare system), so "Just run all of them" isn't a viable solution. We do that, but only before promoting through our environments.

Thoughts?


回答1:


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);
}



回答2:


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.



来源:https://stackoverflow.com/questions/22187229/nunit-multiple-properties-of-the-same-name-linking-to-requirements

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