获取所有测试方法名称 [英] Getting All Test Methodes Names
本文介绍了获取所有测试方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在包含许多项目的整个解决方案中获取所有测试方法名称
意味着我想以编程方式从解决方案层次中提取测试方法名称
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
推荐答案
首先,您需要定义用于区分测试方法与其他方法的标准。
如果您使用的是特殊工具/用于测试的库,如NUnit或VisualStudio.TestTools.UnitTesting,那么这可能相对容易;如果你已经创建了自己的测试库,那么你可能会创建一个自定义属性来装饰测试方法。
如果你使用的话没有提供轻松获取所有方法的方法......我认为这就是这种情况......你将选择.dll或.dll,并使用递归递归搜索符合条件的方法。
递归搜索会有这种流:
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:"
// 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:
public class YourTestMethodAttribute : Attribute
{
private static int _id;
protected int ID
{
set { _id = value; }
get { return _id; }
}
public YourTestMethodAttribute()
{
ID++;
}
}
这篇关于获取所有测试方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文