Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test attributes with MsTest using C#?

How to test the presence of Class Attributes and Method Attributes with MsTest using C#?

like image 764
Jader Dias Avatar asked Jan 10 '11 15:01

Jader Dias


People also ask

Which is better NUnit or MSTest?

The main difference is the ability of MsTest to execute in parallel at the method level. Also, the tight integration of MsTest with Visual Studio provides advantages in both speed and robustness when compared to NUnit. As a result, I recommend MsTest.

Is MSTest a unit testing framework?

MSTest is a number-one open-source test framework that is shipped along with the Visual Studio IDE. It is also referred to as the Unit Testing Framework. However, MSTest is the same within the developer community. MSTest is used to run tests.


1 Answers

C# Extension method for checking attributes

public static bool HasAttribute<TAttribute>(this MemberInfo member) 
    where TAttribute : Attribute
{
    var attributes = 
        member.GetCustomAttributes(typeof(TAttribute), true);

    return attributes.Length > 0;
}
like image 147
Robert Harvey Avatar answered Sep 23 '22 20:09

Robert Harvey