3 using System.Diagnostics;
\r
4 using System.Text.RegularExpressions;
\r
5 using System.Collections;
\r
6 using System.Collections.Specialized;
\r
10 namespace NUnit.ConsoleRunner
\r
13 /// Summary description for EventCollector.
\r
15 public class EventCollector : MarshalByRefObject, EventListener
\r
17 private int testRunCount;
\r
18 private int testIgnoreCount;
\r
19 private int failureCount;
\r
22 private ConsoleOptions options;
\r
23 private TextWriter outWriter;
\r
24 private TextWriter errorWriter;
\r
26 StringCollection messages;
\r
28 private bool progress = false;
\r
29 private string currentTestName;
\r
31 private ArrayList unhandledExceptions = new ArrayList();
\r
33 public EventCollector( ConsoleOptions options, TextWriter outWriter, TextWriter errorWriter )
\r
36 this.options = options;
\r
37 this.outWriter = outWriter;
\r
38 this.errorWriter = errorWriter;
\r
39 this.currentTestName = string.Empty;
\r
40 this.progress = !options.xmlConsole && !options.labels && !options.nodots;
\r
42 AppDomain.CurrentDomain.UnhandledException +=
\r
43 new UnhandledExceptionEventHandler(OnUnhandledException);
\r
46 public bool HasExceptions
\r
48 get { return unhandledExceptions.Count > 0; }
\r
51 public void WriteExceptions()
\r
53 Console.WriteLine();
\r
54 Console.WriteLine("Unhandled exceptions:");
\r
56 foreach( string msg in unhandledExceptions )
\r
57 Console.WriteLine( "{0}) {1}", index++, msg );
\r
60 public void RunStarted(string name, int testCount)
\r
64 public void RunFinished(TestResult result)
\r
68 public void RunFinished(Exception exception)
\r
72 public void TestFinished(TestCaseResult testResult)
\r
74 if(testResult.Executed)
\r
78 if(testResult.IsFailure)
\r
85 messages.Add( string.Format( "{0}) {1} :", failureCount, testResult.Test.TestName.FullName ) );
\r
86 messages.Add( testResult.Message.Trim( Environment.NewLine.ToCharArray() ) );
\r
88 string stackTrace = StackTraceFilter.Filter( testResult.StackTrace );
\r
89 if ( stackTrace != null && stackTrace != string.Empty )
\r
91 string[] trace = stackTrace.Split( System.Environment.NewLine.ToCharArray() );
\r
92 foreach( string s in trace )
\r
94 if ( s != string.Empty )
\r
96 string link = Regex.Replace( s.Trim(), @".* in (.*):line (.*)", "$1($2)");
\r
97 messages.Add( string.Format( "at\n{0}", link ) );
\r
108 Console.Write("N");
\r
111 currentTestName = string.Empty;
\r
114 public void TestStarted(TestName testName)
\r
116 currentTestName = testName.FullName;
\r
118 if ( options.labels )
\r
119 outWriter.WriteLine("***** {0}", currentTestName );
\r
122 Console.Write(".");
\r
125 public void SuiteStarted(TestName testName)
\r
127 if ( level++ == 0 )
\r
129 messages = new StringCollection();
\r
131 testIgnoreCount = 0;
\r
133 Trace.WriteLine( "################################ UNIT TESTS ################################" );
\r
134 Trace.WriteLine( "Running tests in '" + testName.FullName + "'..." );
\r
138 public void SuiteFinished(TestSuiteResult suiteResult)
\r
140 if ( --level == 0)
\r
142 Trace.WriteLine( "############################################################################" );
\r
144 if (messages.Count == 0)
\r
146 Trace.WriteLine( "############## S U C C E S S #################" );
\r
150 Trace.WriteLine( "############## F A I L U R E S #################" );
\r
152 foreach ( string s in messages )
\r
154 Trace.WriteLine(s);
\r
158 Trace.WriteLine( "############################################################################" );
\r
159 Trace.WriteLine( "Executed tests : " + testRunCount );
\r
160 Trace.WriteLine( "Ignored tests : " + testIgnoreCount );
\r
161 Trace.WriteLine( "Failed tests : " + failureCount );
\r
162 Trace.WriteLine( "Unhandled exceptions : " + unhandledExceptions.Count);
\r
163 Trace.WriteLine( "Total time : " + suiteResult.Time + " seconds" );
\r
164 Trace.WriteLine( "############################################################################");
\r
168 private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
\r
170 if (e.ExceptionObject.GetType() != typeof(System.Threading.ThreadAbortException))
\r
172 this.UnhandledException((Exception)e.ExceptionObject);
\r
177 public void UnhandledException( Exception exception )
\r
179 // If we do labels, we already have a newline
\r
180 unhandledExceptions.Add(currentTestName + " : " + exception.ToString());
\r
181 //if (!options.labels) outWriter.WriteLine();
\r
182 string msg = string.Format("##### Unhandled Exception while running {0}", currentTestName);
\r
183 //outWriter.WriteLine(msg);
\r
184 //outWriter.WriteLine(exception.ToString());
\r
186 Trace.WriteLine(msg);
\r
187 Trace.WriteLine(exception.ToString());
\r
190 public void TestOutput( TestOutput output)
\r
192 switch ( output.Type )
\r
194 case TestOutputType.Out:
\r
195 outWriter.Write( output.Text );
\r
197 case TestOutputType.Error:
\r
198 errorWriter.Write( output.Text );
\r
204 public override object InitializeLifetimeService()
\r