Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i get all test methods names in an entire solution having many projects
meaning i want to extract test methods names from solution hierarchy programmatically
Posted
Comments
[no name] 23-Jan-16 10:33am    
How is about this?
Google[^]
Sergey Alexandrovich Kryukov 23-Jan-16 11:34am    
From solution of from the assemblies? Define "text methods". What are those methods?
—SA

1 solution

First of all, you need to define the criteria by which you will distinguish test methods from other methods.

If you are using a special tool/library for testing, like NUnit, or VisualStudio.TestTools.UnitTesting, then this may be relatively easy; if you have created your own testing library, then you would probably create a Custom Attribute to adorn test methods with.

In the case whatever you use doesn't provide a way to get at all the methods easily ... I assume that's the case here ... you are going to select a .dll, or .dll's, and search recursively, using recursion, for Methods that meet your criteria.

A recursive search is going to have this kind of "flow:"
C#
// this is pseudo-code:
foreach FileInfo theFileInfo in AWholeBunchOfDLLFiles

Assembly theAssembly = Assembly.LoadFrom(theFileInfo.FullName);

foreach Type theType in theAssembly.GetTypes()
foreach MethodInfo theMethod in theType.GetMethods()
foreach Attribute attr in Attribute.GetCustomAttributes(theMethod)

// this is an example from working code:
if (attr.GetType() == typeof (YourTestMethodAttribute))
{
    // Bingo !

    YourTestMethodAttribute mta = attr as MyTestMethodAttribute;

    Console.WriteLine
    (
        "Method {0} with MyTestMethodAttribute ID: {1}",
        mInfo.Name,
        mta.ID
    );
}
This is an example of an actual Custom Attribute that would work with the code shown above:
C#
public class YourTestMethodAttribute : Attribute
{
    private static int _id;

    protected int ID
    {
        set { _id = value; }
        get { return _id; }
    }

    public YourTestMethodAttribute()
    {
        ID++;
    }
}
 
Share this answer
 
Comments
oula alsheikh 30-Jan-16 3:01am    
thanks for your answer
but i found this solution most helpful

var MyProceduresNames = new List<string>();
var fileEntries = Directory.GetFiles(@"D", "*.*", SearchOption.AllDirectories).ToList().Where(s => s.EndsWith(".AnyDesiredsuffix"));
foreach (string fileName in fileEntries)
{
ResXResourceReader rr = new ResXResourceReader(fileName);
foreach (DictionaryEntry entry in rr)
{
MyProceduresNames.Add(entry.Key.ToString()) ;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900