Search code examples
c#.netseleniumnunitpairwise

Const collection for [Test, Pairwise]


Using c#, nunit, selenium for automation. I would like to use attribute [Test, Pairwise] for my test case to verify that object can be posted with any valid value. I have dictionary with all valid values, but [Values()] - requires const as parameter and ReadOnlyCollection(as it was suggested here) doesn't work for it. I'm having error: An attribute agrument must be a constant expressiom, typeof expression or array expression of an attribute parameter type.

class ObjectBaseCalls : ApiTestBase
{
    static ReadOnlyCollection<string> AllTypes = new ReadOnlyCollection<string>(new List<string>() { "Value1", "Value 2" });

    [Test, Pairwise]
    public void ObjectCanBePostedAndGeted([Values(AllTypes)] string type)
    {
        //My test
    }
}

Solution

  • I found the solution and it works for me. I created enum with parameter name and dictionary with parameter name and parameter value for each object parameter and use enum into my object constructor PairWiseAttribute.

    public class MyElement : BaseElement
    {  
        public enum Types { Type1, Type2, Type3, Type4}
        public Dictionary<Types, string> AllTypes = new Dictionary<Types, string>()
        {
            { Types.Type1, "Value 1" },
            { Types.Type2, "Value 2" },
            { Types.Type3, "Value 3" },
            { Types.Type4, "Value 4" },
        };
        public enum Category { Category1, Category2, Category3, Category4}
        public Dictionary<Category, string> Categories = new Dictionary<Category, string>()
        {
            { Category.Category1, "Value 1" },
            { Category.Category2, "Value 2" },
            { Category.Category3, "Value 3" },
            { Category.Category4, "Value 4" },
        };
        public MyElement(Types type, Category category)
        {
            type = AllTypes[type];
            category = Categories[category];
        }
    }
    public class Test
    {
        [Test, Pairwise]
        public void ValidBaseCheckElementCalls
        (
            [Values(Types.Type1, Types.Type2, Types.Type3, Types.Type4)] Types objType,
            [Values(Category.Category1, Category.Category2, Category.Category3, Category.Category4)] Category objCategory,
        )
    
        {
            MyElement element = new MyElement(objType, objCategory);
        }
    
    }