'How to get the TestCategory with in the CustomTestMethod attribute

I have create the Custom Test Method attribute.

public class CustomTestMethodAttribute : TestMethodAttribute
{
    public CareSourceTestMethodAttribute(string category, string name, string id) : base(name)
    {
        testID = id;
        testCategory = category;
        testName = name;
    }

    public string exception { get; set; }
    public string testCategory { get; set; }
    public string testName { get; set; }
    public string testID { get; set; }

    public override TestResult[] Execute(ITestMethod testMethod)
    {
        TestResult[] result = base.Execute(testMethod);

        foreach(TestResult testResult in result)
        {
            //Storing exception message in Attribute, in order to use it in Extent Report.
            if (testResult.Outcome == UnitTestOutcome.Failed)
            {
                exception = testResult.TestFailureException?.Message;
                break;
            }
        }

        return result;
    }
}

I had tried to assign the category to testcategory , what we got from the testmethod. But no luck.

thanks for the Info. Actually , while assign the test in the Method. This what i implemented [CustomAttribut("Regression","Login with Valid Usernamee and Password","1")].

Actual we write as [TestMethod("Login with valid username and Password"] [TestCategory("Regression")].

So i want to assigned the category in Customattribute but i am unable to do.. Anybody help me



Solution 1:[1]

Not sure if this is what you are asking, but you can use ITestMethod.GetAttributes(Boolean) Method:

IEnumerable<string> categories = 
    testMethod.GetAttributes<TestCategoryAttribute>(true)
    .SelectMany(x => x.TestCategories);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peska