-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunner.cs
204 lines (172 loc) · 6.69 KB
/
Runner.cs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//-----------------------------------------------------------------------
// ExpressTest. A stand-in for MSTest when using Visual Studio 2010 Express.
//-----------------------------------------------------------------------
namespace ExpressTest
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using ExpressTest.UnitTesting;
public class Runner
{
public void run()
{
/*
try
{
Directory.Delete(new TestContext().TestDir, true);
}
catch (DirectoryNotFoundException dnfe)
{
// Ignore
}
*/
Assembly thisAsm = Assembly.GetCallingAssembly();
List<Type> types = thisAsm.GetTypes().Where(t => t.IsClass && !t.IsAbstract).ToList();
string assemblyName = thisAsm.FullName.Substring(0, thisAsm.FullName.IndexOf(','));
List<string> includes = ReadTestSet(thisAsm, assemblyName);
List<string> fails = new List<string>();
int testCount = 0;
foreach (Type t in types)
{
object[] attribs = t.GetCustomAttributes(true);
foreach (object a in attribs)
{
if (a is TestClass)
{
ProcessClass(t, fails, includes, ref testCount);
continue;
}
}
}
Trace.WriteLineIf(fails.Count > 0, "WARNING: There were " + fails.Count + " failed tests!!");
if (fails.Count > 0)
{
using(TextWriter tw = new StreamWriter(new TestContext().TestDir + "/" + assemblyName+".testSet.xml", false))
{
tw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
tw.WriteLine("<tests>");
Trace.WriteLine("Summary of failure:");
foreach (var fail in fails)
{
Trace.WriteLine("FAILED: " + fail);
tw.WriteLine(" <include>"+fail+"</include>");
}
tw.WriteLine("</tests>");
}
}
Trace.WriteLine("Pass rate: " + (testCount - fails.Count) + "/" + testCount);
Trace.WriteLineIf(fails.Count == 0, "YESSSS!!");
}
private List<string> ReadTestSet(Assembly asm, string asmName)
{
string xmlRes = asmName + ".res.express." + asmName + ".testSet.xml";
List<string> includes = new List<string>();
using (Stream s = asm.GetManifestResourceStream(xmlRes))
{
if (s == null)
{
return null;
}
using (XmlReader reader = XmlReader.Create(s))
{
while (reader.ReadToFollowing("include"))
{
includes.Add(reader.ReadElementContentAsString());
}
}
}
string[] names = asm.GetManifestResourceNames();
return includes.Count > 0? includes : null;
}
private void ProcessClass(Type t, List<string> fails, List<string> includes, ref int testCount)
{
MethodInfo[] methods = t.GetMethods();
MethodInfo init = null;
MethodInfo cleanup = null;
List<MethodInfo> tests = new List<MethodInfo>();
Dictionary<MethodInfo, Type> expectedExceptions = new Dictionary<MethodInfo, Type>();
foreach (MethodInfo m in methods)
{
object[] attribs = m.GetCustomAttributes(true);
foreach (object a in attribs)
{
if (a is TestInitialize)
{
/* TODO: Can you have multiple inits? */
init = m;
}
if (a is TestCleanup)
{
/* TODO: Can you have multiple cleanups? */
cleanup = m;
}
if (a is TestMethod)
{
if (includes == null || includes.Contains(t.Name+"."+m.Name))
{
tests.Add(m);
}
}
if (a is ExpectedException)
{
expectedExceptions.Add(m, ((ExpectedException)a).Type);
}
}
}
ConstructorInfo ci = t.GetConstructor(new Type[] { });
object testObject = ci.Invoke(new Object[] { });
foreach (MethodInfo method in tests)
{
try
{
SetProperty(t, "TestContext", new TestContext() { TestName = method.Name }, testObject);
}
catch (Exception)
{
Trace.WriteLine("No test context was set.");
}
if (init != null)
{
Call(t, init, testObject);
}
bool passed = true;
try
{
testCount++;
Call(t, method, testObject);
}
catch (Exception e)
{
if (!(expectedExceptions.ContainsKey(method) && e.InnerException.GetType() == expectedExceptions[method]))
{
fails.Add(t.Name + "." + method.Name);
passed = false;
Debug.WriteLine("FAIL: "+method.Name+"; "+e.InnerException.Message);
}
}
Trace.WriteLineIf(passed, "OK: "+method.Name);
if (cleanup != null)
{
Call(t, cleanup, testObject);
}
}
}
private static void SetProperty(Type t, string name, object val, object testObject)
{
t.InvokeMember(name,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
Type.DefaultBinder, testObject, new object[] { val });
}
private static void Call(Type t, MethodInfo method, object testObject)
{
t.InvokeMember(method.Name,
BindingFlags.Default | BindingFlags.InvokeMethod,
null, testObject, new Object[] { });
}
}
}