How to extract all classes with "TestClass" attribute and all its methods with "TestMethods" attributes into an external file like .txt or excel types without using TFS?
Asked
Active
Viewed 215 times
-1
-
you mean by clicking some sort of menu item or by executing self written code? – nozzleman Aug 11 '14 at 10:01
-
I actually dont know. You can suggest what you know – Mikk Aug 11 '14 at 10:02
-
I don't understand why down vote. It is a valid question, I want to extract all Test classes and its test methods. What's up with it? – Mikk Aug 11 '14 at 10:05
-
That's the thing, I don't know where to even start. I did some googling but couldn't find anything. You know where there's no effort? Judging someone by downvoting a question when it is valid and assuming the person asking didn't do any effort. – Mikk Aug 11 '14 at 10:09
-
[*find all classes by attribute c#*](http://stackoverflow.com/questions/607178/how-enumerate-all-classes-with-custom-class-attribute), [*find all methods by attribute c#*](http://stackoverflow.com/questions/3467765/get-method-details-using-reflection-and-decorated-attribute), [*write text to excel file c#*](https://bytescout.com/products/developer/spreadsheetsdk/read-write-excel.html). 3 search queries that gave me everything I need to know in 2 minutes. – Jeroen Vannevel Aug 11 '14 at 10:15
-
extract test cases in visual studio 2013, export test explorer vs2013 certainly didn't give anything for me – Mikk Aug 11 '14 at 10:20
2 Answers
2
There are probably tools but here is a simple implementation using reflection:
var assembly = Assembly.LoadFile("xxx.dll");
var testClasses = assembly.GetTypes()
.Where(c => c.GetCustomAttribute<TestClassAttribute>() != null);
foreach (var testClass in testClasses)
{
Console.WriteLine("Found test class " + testClass.FullName);
var testMethods = testClass.GetMethods().Where(m => m.GetCustomAttribute<TestMethodAttribute>() != null);
foreach (var testMethod in testMethods)
{
Console.WriteLine("Found test method " + testMethod.Name);
}
}

Stefano Altieri
- 4,550
- 1
- 24
- 41
0
[TestMethod]
public void list__all_unit_tests()
{
string sDLL = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
string sFilePath = string.Format("{0:s}\\{1:s}.DLL", System.IO.Directory.GetCurrentDirectory(), sDLL);
var assembly = System.Reflection.Assembly.LoadFile(sFilePath);
var testClasses = assembly.GetTypes()
.Where(m => m.GetCustomAttributes(typeof(TestClassAttribute),false)!=null);
foreach (var testClass in testClasses)
{
var testMethods = testClass.GetMethods()
.Where(m => m.GetCustomAttributes(typeof(TestMethodAttribute),true).Length !=0);
foreach (var testMethod in testMethods)
{
Debug.Print(string.Format("class,{0:s}, method,{1:s} ", testClass.FullName , testMethod.Name));
}
}
}

Rick Smith
- 9,031
- 15
- 81
- 85

Todd Harvey
- 309
- 3
- 4