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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
namespace NUnit.Runner {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;
using NUnit.Framework;
/// <summary>Base class for all test runners.</summary>
/// <remarks>This class was born live on stage in Sardinia during
/// XP2000.</remarks>
public abstract class BaseTestRunner: ITestListener {
/// <summary>
///
/// </summary>
public static string SUITE_PROPERTYNAME="Suite";
static NameValueCollection fPreferences = new NameValueCollection();
static int fgMaxMessageLength = 500;
static bool fgFilterStack = true;
bool fLoading = true;
/// <summary>
///
/// </summary>
public BaseTestRunner() {
fPreferences = new NameValueCollection();
fPreferences.Add("loading", "true");
fPreferences.Add("filterstack", "true");
ReadPreferences();
fgMaxMessageLength = GetPreference("maxmessage", fgMaxMessageLength);
}
/// <summary>
///
/// </summary>
/// <param name="test"></param>
/// <param name="t"></param>
public abstract void AddError(ITest test, Exception t);
/// <summary>
///
/// </summary>
/// <param name="test"></param>
/// <param name="t"></param>
public abstract void AddFailure(ITest test, AssertionFailedError t);
/// <summary>
/// Clears the status message.
/// </summary>
protected virtual void ClearStatus() { // Belongs in the GUI TestRunner class.
}
/// <summary>
///
/// </summary>
/// <param name="test"></param>
public abstract void EndTest(ITest test);
/// <summary>
/// Returns the formatted string of the elapsed time.
/// </summary>
public static string ElapsedTimeAsString(long runTime) {
return ((double)runTime/1000).ToString();
}
/// <summary>
/// Extract the class name from a string in VA/Java style
/// </summary>
public static string ExtractClassName(string className) {
if(className.StartsWith("Default package for"))
return className.Substring(className.LastIndexOf(".")+1);
return className;
}
static bool FilterLine(string line) {
string[] patterns = new string[] {
"NUnit.Framework.TestCase",
"NUnit.Framework.TestResult",
"NUnit.Framework.TestSuite",
"NUnit.Framework.Assertion." // don't filter AssertionFailure
};
for (int i = 0; i < patterns.Length; i++) {
if (line.IndexOf(patterns[i]) > 0)
return true;
}
return false;
}
/// <summary>
/// Filters stack frames from internal NUnit classes
/// </summary>
public static string FilterStack(string stack) {
string pref = GetPreference("filterstack");
if (((pref != null) && !GetPreference("filterstack").Equals("true")) || fgFilterStack == false)
return stack;
StringWriter sw = new StringWriter();
StringReader sr = new StringReader(stack);
string line;
try {
while ((line = sr.ReadLine()) != null) {
if (!FilterLine(line))
sw.WriteLine(line);
}
} catch (Exception) {
return stack; // return the stack unfiltered
}
return sw.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static string GetFilteredTrace(Exception t) {
return BaseTestRunner.FilterStack(t.StackTrace);
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetPreference(string key) {
return fPreferences.Get(key);
}
private static int GetPreference(String key, int dflt)
{
String value= GetPreference(key);
int intValue= dflt;
if (value == null)
return intValue;
try {
intValue= int.Parse(value);
}
catch (FormatException) {
}
return intValue;
}
private static FileStream GetPreferencesFile() {
return new IsolatedStorageFileStream("NUnit.Prefs",
FileMode.OpenOrCreate);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual ITestSuiteLoader GetLoader() {
if (UseReloadingTestSuiteLoader())
return new ReloadingTestSuiteLoader();
return new StandardTestSuiteLoader();
}
/// <summary>
/// Returns the ITest corresponding to the given suite. This is
/// a template method, subclasses override RunFailed(), ClearStatus().
/// </summary>
public ITest GetTest(string suiteClassName) {
if (suiteClassName.Length <= 0) {
ClearStatus();
return null;
}
Type testClass= null;
try {
testClass = LoadSuiteClass(suiteClassName);
} catch (TypeLoadException e) {
RunFailed(e.Message);
return null;
} catch (Exception e) {
RunFailed("Error: " + e.ToString());
return null;
}
PropertyInfo suiteProperty= null;
suiteProperty = testClass.GetProperty(SUITE_PROPERTYNAME, new Type[0]);
if (suiteProperty == null ) {
// try to extract a test suite automatically
ClearStatus();
return new TestSuite(testClass);
}
ITest test= null;
try {
// static property
test= (ITest)suiteProperty.GetValue(null, new Type[0]);
if (test == null)
return test;
} catch(Exception e) {
RunFailed("Could not get the Suite property. " + e);
return null;
}
ClearStatus();
return test;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static bool InVAJava() {
return false;
}
/// <summary>
/// Returns the loaded Class for a suite name.
/// </summary>
protected Type LoadSuiteClass(string suiteClassName) {
return GetLoader().Load(suiteClassName);
}
private static void ReadPreferences() {
FileStream fs= null;
try {
fs= GetPreferencesFile();
fPreferences= new NameValueCollection(fPreferences);
ReadPrefsFromFile(ref fPreferences, fs);
} catch (IOException) {
try {
if (fs != null)
fs.Close();
} catch (IOException) {
}
}
}
private static void ReadPrefsFromFile(ref NameValueCollection prefs, FileStream fs) {
// Real method reads name/value pairs, populates, or maybe just
// deserializes...
}
/// <summary>
/// Override to define how to handle a failed loading of a test suite.
/// </summary>
protected abstract void RunFailed(String message);
/// <summary>
/// Truncates a String to the maximum length.
/// </summary>
public static String Truncate(String s) {
if (fgMaxMessageLength != -1 && s.Length > fgMaxMessageLength)
s= s.Substring(0, fgMaxMessageLength)+"...";
return s;
}
/// <summary>
///
/// </summary>
/// <param name="test"></param>
public abstract void StartTest(ITest test);
/// <summary>
///
/// </summary>
/// <param name="args"></param>
/// <param name="wait"></param>
/// <returns></returns>
protected string ProcessArguments(string[] args, ref bool wait) {
string suiteName="";
wait = false;
foreach (string arg in args) {
if (arg.Equals("/noloading"))
SetLoading(false);
else if (arg.Equals("/nofilterstack"))
fgFilterStack = false;
else if (arg.Equals("/wait"))
wait = true;
else if (arg.Equals("/c"))
suiteName= ExtractClassName(arg);
else if (arg.Equals("/v")){
Console.Error.WriteLine("NUnit "+NUnit.Runner.Version.id()
+ " by Philip Craig");
Console.Error.WriteLine("ported from JUnit 3.6 by Kent Beck"
+ " and Erich Gamma");
} else
suiteName = arg;
}
return suiteName;
}
/// <summary>
/// Sets the loading behaviour of the test runner
/// </summary>
protected void SetLoading(bool enable) {
fLoading = false;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected bool UseReloadingTestSuiteLoader() {
return GetPreference("loading").Equals("true") && fLoading;
}
}
}
|