Domanda

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"?

È stato utile?

Soluzione

The following code works on my box,

Type type = typeof(CodedUITest2);
IEnumerable<MethodInfo> testMethods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public).Where(m => m.IsDefined(typeof(TestMethodAttribute)));

Altri suggerimenti

On the MSDN site I found reference to the following part of VSTest.Console.exe Command-Line Options. Maybe this would help?

http://msdn.microsoft.com/en-us/library/jj155796.aspx

/ListTests:[ file name ] Lists discovered tests from the given test container.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top