I am building a utility to run the unit test cases, where the user will be provided with the option to select the test they want to run. I am using reflection to get the methods. But it is not working
var assembly = Assembly.Load("TestingAssembly");
var classes = assembly.GetTypes().Where(m => m.GetCustomAttributes(typeof(TestClassAttribute), false).Length > 0);
foreach (var type in classes)
{
var methods= type.GetTypes().Where(m1 => m1.GetCustomAttributes(typeof(TestMethodAttribute), false).Length > 0);
}
The classes object is always empty. If I iterate through the assembly.GetTypes and get the custom attributes it is returning the count. But still, I am not able to match it. In the below code the attributes contains an item with is TestClassAttribute. But when am matching the attribute's type to TestClassAttribute it fails.
foreach (Type type in assembly.GetTypes())
{
if (type.GetCustomAttributes().Count() > 0)
{
var attributes = type.GetCustomAttributes();
foreach (var item in attributes)
{
if (item.GetType() == Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute)
{
//Do something
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire