blob: 19f656f04c421813a0076f7ec2982381cfc1fe44 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
namespace NUnit.Runner {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using NUnit.Framework;
/// <summary>
///
/// </summary>
public class ReflectionUtils{
/// <summary>
///
/// </summary>
/// <param name="testClass"></param>
/// <returns></returns>
public static bool HasTests(Type testClass) {
PropertyInfo suiteProperty= null;
suiteProperty = testClass.GetProperty("Suite", new Type[0]);
if (suiteProperty == null ) {
// try to extract a test suite automatically
bool result = false;
TestSuite dummy = new TestSuite(testClass, ref result);
return result;
}
ITest test= null;
try {
// static property
test= (ITest)suiteProperty.GetValue(null, new Type[0]);
if (test == null)
return false;
} catch(Exception) {
return false;
}
return true;
}
/// <summary>
///
/// </summary>
/// <param name="assemblyName"></param>
/// <returns></returns>
public static StringCollection GetAssemblyClasses(string assemblyName){
StringCollection classNames = new StringCollection ();
try {
Assembly testAssembly = Assembly.LoadFrom(assemblyName);
foreach(Type testType in testAssembly.GetExportedTypes()){
if(testType.IsClass && HasTests(testType)){
classNames.Add(testType.FullName);
}
}
}catch(ReflectionTypeLoadException rcle){
Type[] loadedTypes = rcle.Types;
Exception[] exceptions = rcle.LoaderExceptions;
int exceptionCount = 0;
for ( int i =0; i < loadedTypes.Length; i++ ){
Console.Error.WriteLine("Unable to load a type because {0}", exceptions[exceptionCount] );
exceptionCount++;
}
}catch(FileNotFoundException fnfe){
Console.Error.WriteLine(fnfe.Message);
}catch(Exception e){
Console.Error.WriteLine("Error reading file {0}: {1}", assemblyName, e.Message);
}
return classNames;
}
}
}
|