I have several test projects written in C#. I need to create a new application (can be a console or WPF application) which needs to refer to the test projects and found out all test methods names dynamically.
So far I was able to find out all methods and properties names in all test projects, but I'm not able to filter out only the test methods names. I was hopping to be able to filter out the test methods by using TestMethodAttribute, because all test methods have [TestMethod] attribute. However it doesn't do the job correctly. Here is an extraction of the code
MethodInfo[] methodInfos = typeof(CodedUITest2).GetMethods();
Array.Sort(methodInfos,
delegate(MethodInfo methodInfo1, MethodInfo methodInfo2)
{return methodInfo1.Name.CompareTo(methodInfo2.Name);});
foreach (MethodInfo mi in methodInfos)
{
object[] al = mi.GetCustomAttributes(typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute), false);
if (al != null)
Console.WriteLine(mi.Name);
}
The output of the program is CodedUITestMethod3 Equals get_TestContext GetHashCode GetType set_TestContext ToString
So if I delete the following statements, the result is the same.
object[] al = mi.GetCustomAttributes(typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute), false); if (al != null)
So my question is after finding all methods names, how it is possible to filter the result and get only test method, in this example, it should print only "CodedUITestMethod3"?