[Adapdev-commits] Adapdev/src/Adapdev.UnitTest.Core RunMethodCommand.cs,NONE,1.1 RunNonThreadedTestC
Status: Beta
Brought to you by:
intesar66
From: Sean M. <int...@us...> - 2005-10-26 05:28:00
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17136/src/Adapdev.UnitTest.Core Modified Files: AbstractTest.cs Adapdev.UnitTest.Core.csproj Adapdev.UnitTest.Core.csproj.user TestRunner.cs TestSuiteBuilder.cs TextFormatter.cs Added Files: RunMethodCommand.cs RunNonThreadedTestCommand.cs RunTestFixtureCommand.cs RunThreadedTestCommand.cs Log Message: Added multi-threading support to testing engine Index: TestSuiteBuilder.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core/TestSuiteBuilder.cs,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** TestSuiteBuilder.cs 23 Oct 2005 07:27:20 -0000 1.9 --- TestSuiteBuilder.cs 26 Oct 2005 05:27:45 -0000 1.10 *************** *** 143,146 **** --- 143,153 ---- tf.FullName = t.FullName; + if (TypeHelper.HasCustomAttribute(t, typeof (Adapdev.UnitTest.TestFixtureAttribute))) + { + Adapdev.UnitTest.TestFixtureAttribute tfa = (Adapdev.UnitTest.TestFixtureAttribute) TypeHelper.GetFirstCustomAttribute(t, typeof (Adapdev.UnitTest.TestFixtureAttribute)); + tf.IsMultiThreaded = tfa.IsMultiThreaded; + } + + this.ProcessCommonAttributes(t, tf); --- NEW FILE: RunMethodCommand.cs --- using System; using System.Reflection; using Adapdev.Commands; namespace Adapdev.UnitTest.Core { /// <summary> /// Summary description for RunMethodCommand. /// </summary> public class RunMethodCommand : ICommand { private MethodInfo _method = null; private object _o = null; public RunMethodCommand(MethodInfo method, object o) { this._method = method; this._o = o; } public void Execute() { this._method.Invoke(this._o, null); } } } Index: AbstractTest.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core/AbstractTest.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** AbstractTest.cs 5 Jun 2005 08:50:31 -0000 1.3 --- AbstractTest.cs 26 Oct 2005 05:27:45 -0000 1.4 *************** *** 60,63 **** --- 60,65 ---- protected string _fullName = String.Empty; private static int _globalid = 0; + private bool _isMultiThreaded = false; + public AbstractTest() *************** *** 160,163 **** --- 162,171 ---- } + public bool IsMultiThreaded + { + get { return _isMultiThreaded; } + set { _isMultiThreaded = value; } + } + public abstract int GetTestCount(); } Index: Adapdev.UnitTest.Core.csproj.user =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core/Adapdev.UnitTest.Core.csproj.user,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Adapdev.UnitTest.Core.csproj.user 21 Oct 2005 04:59:20 -0000 1.3 --- Adapdev.UnitTest.Core.csproj.user 26 Oct 2005 05:27:45 -0000 1.4 *************** *** 41,45 **** CopyProjectUncPath = "" CopyProjectOption = "0" ! ProjectView = "ProjectFiles" ProjectTrust = "0" /> --- 41,45 ---- CopyProjectUncPath = "" CopyProjectOption = "0" ! ProjectView = "ShowAllFiles" ProjectTrust = "0" /> --- NEW FILE: RunTestFixtureCommand.cs --- using System; using System.Collections; using System.Reflection; using System.Threading; using Adapdev.Commands; using Adapdev.Threading; namespace Adapdev.UnitTest.Core { /// <summary> /// Summary description for TestFixtureCommand. /// </summary> public class RunTestFixtureCommand : ICommand { private TestEventDispatcher _dispatcher = null; private TestFixture _testFixture = null; private Assembly _assembly = null; private string _className = String.Empty; private TestFixtureResult _testFixtureResult = null; private TestFixtureIteration _testFixtureIteration = null; private Type _type = null; private object _o = null; public RunTestFixtureCommand(TestEventDispatcher dispatcher, TestFixture tf, Assembly assembly, string className, TestFixtureResult testFixtureResult) { this._dispatcher = dispatcher; this._testFixture = tf; this._assembly = assembly; this._className = className; this._testFixtureResult = testFixtureResult; } public void Execute() { if(_dispatcher != null)_dispatcher.OnTestFixtureStarted(new TestFixtureEventArgs(this._testFixture)); for (int j = 1; j <= this._testFixture.RepeatCount; j++) { this._type = null; this._testFixtureIteration = null; if (this._testFixture.RepeatDelay > 0) Thread.Sleep(this._testFixture.RepeatDelay); this._testFixtureIteration = new TestFixtureIteration(); if(_dispatcher != null)this._dispatcher.OnTestFixtureIterationStarted(new TestFixtureEventArgs(this._testFixture)); this._o = this._assembly.CreateInstance(this._className); this._type = this._assembly.GetType(this._className, true); this.RunTestFixtureSetUps(this._testFixture, this._o, this._type); RunTests(); this.RunTestFixtureTearDowns(this._testFixture, this._o, this._type); this._testFixtureIteration.Iteration = j; this._testFixtureResult.AddIteration(this._testFixtureIteration); if(_dispatcher != null)_dispatcher.OnTestFixtureIterationCompleted(new TestFixtureIterationEventArgs(this._testFixtureIteration)); } if(_dispatcher != null)_dispatcher.OnTestFixtureCompleted(new TestFixtureResultEventArgs(this._testFixtureResult)); } protected void RunTestFixtureSetUps(TestFixture tf, object instance, Type type) { foreach (BaseTestHelper t in tf.GetFixtureSetUps()) { try { if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); MethodInfo m = type.GetMethod(t.Method); m.Invoke(instance, null); } catch (Exception e) { Console.Write(e); } } } protected void RunTestFixtureTearDowns(TestFixture tf, object instance, Type type) { foreach (BaseTestHelper t in tf.GetFixtureTearDowns()) { try { if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); MethodInfo m = type.GetMethod(t.Method); m.Invoke(instance, null); } catch (Exception e) { Console.Write(e); } } } private void RunTests() { if(this._testFixture.IsMultiThreaded) { // ArrayList threads = new ArrayList(); // foreach (Test test in this._testFixture.GetTests()) // { // RunNonThreadedTestCommand command = new RunNonThreadedTestCommand(this._dispatcher, test, this._testFixture, this._type, this._o, this._testFixtureIteration); // Thread thread = new Thread(new ThreadStart(command.Execute)); // threads.Add(thread); // thread.Start(); // } // foreach(Thread t in threads) t.Join(); using(ThreadPoolWait threadPool = new ThreadPoolWait()) { foreach (Test test in this._testFixture.GetTests()) { RunNonThreadedTestCommand command = new RunNonThreadedTestCommand(this._dispatcher, test, this._testFixture, this._type, this._o, this._testFixtureIteration); threadPool.QueueUserWorkItem(new WaitCallback(command.Execute)); } threadPool.WaitOne(); } } else { foreach (Test test in this._testFixture.GetTests()) { new RunNonThreadedTestCommand(this._dispatcher, test, this._testFixture, this._type, this._o, this._testFixtureIteration).Execute(); } } } } } --- NEW FILE: RunThreadedTestCommand.cs --- using System; using System.Reflection; using System.Threading; using Adapdev.Commands; namespace Adapdev.UnitTest.Core { /// <summary> /// Summary description for ThreadedRunTestCommand. /// </summary> public class RunThreadedTestCommand : ICommand { private TestEventDispatcher _dispatcher = null; private Test _test = null; private TestFixture _testFixture = null; private Type _type = null; private object o = null; private TestFixtureIteration _testFixtureIteration = null; private Thread _running = null; public RunThreadedTestCommand(TestEventDispatcher dispatcher, Test test, TestFixture tf, Type type, object o, TestFixtureIteration tfi) { this._dispatcher = dispatcher; this._test = test; this._testFixture = tf; this.o = o; this._testFixtureIteration = tfi; this._type = type; } public void Execute() { Thread thread = new Thread(new ThreadStart(Run)); thread.Priority = ThreadPriority.AboveNormal; this._running = thread; thread.Start(); } public Thread TestThread { get{return this._running;} } private void Run() { new RunNonThreadedTestCommand(this._dispatcher, this._test, this._testFixture, this._type, this.o, this._testFixtureIteration).Execute(); } } } Index: Adapdev.UnitTest.Core.csproj =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core/Adapdev.UnitTest.Core.csproj,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Adapdev.UnitTest.Core.csproj 23 Oct 2005 07:27:20 -0000 1.4 --- Adapdev.UnitTest.Core.csproj 26 Oct 2005 05:27:45 -0000 1.5 *************** *** 177,180 **** --- 177,200 ---- /> <File + RelPath = "RunMethodCommand.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "RunNonThreadedTestCommand.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "RunTestFixtureCommand.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "RunThreadedTestCommand.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File RelPath = "Test.cs" SubType = "Code" Index: TestRunner.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core/TestRunner.cs,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** TestRunner.cs 22 Oct 2005 17:15:38 -0000 1.8 --- TestRunner.cs 26 Oct 2005 05:27:45 -0000 1.9 *************** *** 65,71 **** private bool _infoMode = log.IsInfoEnabled; - /// <summary> - /// The list of assemblies to load - /// </summary> private IPerfTimer timer = PerfTimerFactory.GetPerfTimer(PerfTimerType.HIRESSECONDS); --- 65,68 ---- *************** *** 174,381 **** if(this._debugMode) log.Debug("Running TF " + tf.Name); ! if(_dispatcher != null)_dispatcher.OnTestFixtureStarted(new TestFixtureEventArgs(tf)); ! for (int j = 1; j <= tf.RepeatCount; j++) ! { ! if (tf.RepeatDelay > 0) Thread.Sleep(tf.RepeatDelay); ! TestFixtureIteration tfi = new TestFixtureIteration(); ! if(_dispatcher != null)this._dispatcher.OnTestFixtureIterationStarted(new TestFixtureEventArgs(tf)); ! ! o = a.CreateInstance(classname); ! Type type = a.GetType(classname, true); ! ! this.RunTestFixtureSetUps(tf, o, type); ! ! foreach (Test test in tf.GetTests()) ! { ! try ! { ! MethodInfo m = type.GetMethod(test.Method); ! TestResult tr = new TestResult(test); ! tr.TestId = test.Id; ! tr.Description = test.Description; ! ! if(this._debugMode) log.Debug("Running T " + test.Name); ! ! if (!test.Ignore && test.ShouldRun) ! { ! ! if(_dispatcher != null)_dispatcher.OnTestStarted(new TestEventArgs(test)); ! for (int i = 1; i <= test.RepeatCount; i++) ! { ! ! TextWriter consoleOut = Console.Out; ! TextWriter errorOut = Console.Error; ! StringWriter consoleWriter = new StringWriter(); ! StringWriter errorWriter = new StringWriter(); ! StringWriter debugWriter = new StringWriter(); ! StringWriter traceWriter = new StringWriter(); ! ! Console.SetOut(consoleWriter); ! Console.SetError(errorWriter); ! TextWriterTraceListener debug = new TextWriterTraceListener(debugWriter); ! TextWriterTraceListener error = new TextWriterTraceListener(traceWriter); ! Debug.Listeners.Add(debug); ! Trace.Listeners.Add(error); ! ! ! if (test.RepeatDelay > 0) ! { ! Thread.Sleep(test.RepeatDelay); ! Console.WriteLine("Sleeping..." + test.RepeatDelay); ! } ! TestIteration ti = new TestIteration(); ! ti.Name = test.Name; ! ti.Iteration = i; ! ti.Thread = AppDomain.GetCurrentThreadId().ToString(); ! long kStart = 0; ! long kEnd = 0; ! if(_dispatcher != null)this._dispatcher.OnTestIterationStarted(new TestEventArgs(test)); ! ! try ! { ! this.RunTestSetUps(tf, test.Name, o, type); ! kStart = Process.GetCurrentProcess().WorkingSet; ! timer.Start(); ! m.Invoke(o, null); ! timer.Stop(); ! ti.Duration = timer.Duration; ! kEnd = Process.GetCurrentProcess().WorkingSet; ! ti.MemoryUsed = (kEnd - kStart)/1024; ! ! if (test.MaxKMemory > 0 && test.MaxKMemory < ti.MemoryUsed) ! { ! ti.State = TestState.Fail; ! ti.Result = "Memory usage exceeded MaxK limit of " + test.MaxKMemory; ! } ! else if (test.MinOperationsPerSecond > 0 && ti.GetOpsPerSecond() < test.MinOperationsPerSecond) ! { ! ti.State = TestState.Fail; ! ti.Result = "Ops per second was less than minimum limit of " + test.MinOperationsPerSecond; ! } ! else if (test.ExpectedExceptionType != null && test.ExpectedExceptionType.Length > 0) ! { ! ti.State = TestState.Fail; ! ti.Result = "ExceptedException type " + test.ExpectedExceptionType + " was not thrown."; ! } ! else ! { ! ti.State = TestState.Pass; ! } ! ! this.RunTestTearDowns(tf, test.Name, o, type); ! } ! catch (Exception e) ! { ! timer.Stop(); ! ti.Duration = timer.Duration; ! kEnd = Process.GetCurrentProcess().WorkingSet; ! if (test.ExpectedExceptionType != null && test.ExpectedExceptionType.Length > 0) ! { ! Type t = null; ! foreach(Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) ! { ! try ! { ! t = ass.GetType(test.ExpectedExceptionType, true, true); ! break; ! } ! catch(Exception){} ! } ! ! if(t == null) throw new TypeLoadException("Unable to locate " + test.ExpectedExceptionType + ". Please make sure it is correct."); ! if (e.InnerException.GetType().IsSubclassOf(t) || e.InnerException.GetType() == t) ! { ! if(test.ExpectedExceptionMessage != null && test.ExpectedExceptionMessage.Length > 0) ! { ! if(test.ExpectedExceptionMessage.ToLower() == e.InnerException.Message.ToLower()) ! { ! ti.Result = "Expected Exception: " + test.ExpectedExceptionType + " was thrown. Message: " + e.InnerException.Message; ! ti.State = TestState.Pass; ! } ! else ! { ! ti.Result = "Expected Exception: " + test.ExpectedExceptionType + " was thrown, but wrong message. Message: " + e.InnerException.Message + " - Expected: " + test.ExpectedExceptionMessage; ! ti.State = TestState.Fail; ! } ! } ! else ! { ! ti.Result = "Expected Exception: " + test.ExpectedExceptionType + " was thrown. Message: " + e.InnerException.Message; ! ti.State = TestState.Pass; ! } ! } ! else ! { ! ti.Result = "Expected Exception: " + test.ExpectedExceptionType + " was NOT thrown. Message: " + e.InnerException.Message; ! ti.State = TestState.Fail; ! } ! ti.ExceptionType = e.InnerException.GetType().FullName; ! ti.FullStackTrace = e.InnerException.StackTrace; ! ti.MemoryUsed = (kEnd - kStart)/1024; ! ! } ! // TODO : Fix incrementing of tests in GUI when IgnoreException is thrown ! else if(e.InnerException.GetType() == typeof(IgnoreException) || e.InnerException.GetType() == typeof(IgnoreException)) ! { ! ti.Result = e.InnerException.Message; ! ti.State = TestState.ForcedIgnore; ! ti.ExceptionType = e.InnerException.GetType().FullName; ! ti.FullStackTrace = e.InnerException.StackTrace; ! } ! else ! { ! ti.Result = e.InnerException.Message; ! ti.ExceptionType = e.InnerException.GetType().FullName; ! ti.FullStackTrace = e.InnerException.StackTrace; ! ti.State = TestState.Fail; ! } ! this.RunTestTearDowns(tf, test.Name, o, type); ! } ! ! ti.ConsoleOutput = consoleWriter.ToString(); ! ti.ConsoleError = errorWriter.ToString(); ! ti.DebugOutput = debugWriter.ToString(); ! ti.TraceOutput = traceWriter.ToString(); ! ! tr.AddIteration(ti); ! if(_dispatcher != null)_dispatcher.OnTestIterationCompleted(new TestIterationEventArgs(ti)); ! ! Console.SetOut(consoleOut); ! Console.SetError(errorOut); ! Debug.Listeners.Remove(debug); ! Trace.Listeners.Remove(error); ! ! debug.Dispose(); ! error.Dispose(); ! } ! ! } ! else ! { ! TestIteration ti = new TestIteration(); ! ti.Name = test.Name; ! ti.State = TestState.Ignore; ! ti.Result = test.IgnoreReason; ! tr.AddIteration(ti); ! } ! ! if (tf.ShouldShow) tfi.AddTestResult(tr); ! if(_dispatcher != null)_dispatcher.OnTestCompleted(new TestResultEventArgs(tr)); ! } ! catch (Exception e) ! { ! Console.WriteLine(e.Message + " " + e.StackTrace); ! } ! finally ! { ! } ! } ! ! this.RunTestFixtureTearDowns(tf, o, type); ! tfi.Iteration = j; ! testFixtureResult.AddIteration(tfi); ! if(_dispatcher != null)_dispatcher.OnTestFixtureIterationCompleted(new TestFixtureIterationEventArgs(tfi)); ! } ! if(_dispatcher != null)_dispatcher.OnTestFixtureCompleted(new TestFixtureResultEventArgs(testFixtureResult)); } catch (Exception e) --- 171,175 ---- if(this._debugMode) log.Debug("Running TF " + tf.Name); ! RunTestFixture(tf, a, classname, testFixtureResult); } catch (Exception e) *************** *** 386,395 **** } ! // Not currently used...but meant to be for multi-threaded tests ! public double ThreadedTestRunner(MethodInfo method, object o) { ! IPerfTimer t = PerfTimerFactory.GetPerfTimer(PerfTimerType.HIRESSECONDS); ! method.Invoke(o, null); ! return 0.00; } --- 180,186 ---- } ! private void RunTestFixture(TestFixture tf, Assembly a, string classname, TestFixtureResult testFixtureResult) { ! new RunTestFixtureCommand(this._dispatcher, tf, a, classname, testFixtureResult).Execute(); } *************** *** 399,476 **** } - protected void RunTestFixtureSetUps(TestFixture tf, object instance, Type type) - { - foreach (BaseTestHelper t in tf.GetFixtureSetUps()) - { - try - { - if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); - MethodInfo m = type.GetMethod(t.Method); - m.Invoke(instance, null); - } - catch (Exception e) - { - Console.Write(e); - } - } - } - - protected void RunTestFixtureTearDowns(TestFixture tf, object instance, Type type) - { - foreach (BaseTestHelper t in tf.GetFixtureTearDowns()) - { - try - { - if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); - MethodInfo m = type.GetMethod(t.Method); - m.Invoke(instance, null); - } - catch (Exception e) - { - Console.Write(e); - } - } - } - - protected void RunTestSetUps(TestFixture tf, string name, object instance, Type type) - { - foreach (TestHelper t in tf.GetTestSetUps()) - { - try - { - // Console.WriteLine(t.Method); - if (t.Test.Length < 1 || t.Test.ToLower().Equals(name.ToLower())) - { - if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); - MethodInfo m = type.GetMethod(t.Method); - m.Invoke(instance, null); - } - } - catch (Exception e) - { - Console.Write(e); - } - } - } - protected void RunTestTearDowns(TestFixture tf, string name, object instance, Type type) - { - foreach (TestHelper t in tf.GetTestTearDowns()) - { - try - { - if (t.Test.Length < 1 || t.Test.ToLower().Equals(name.ToLower())) - { - if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); - MethodInfo m = type.GetMethod(t.Method); - m.Invoke(instance, null); - } - } - catch (Exception e) - { - Console.Write(e); - } - } - } public void SetConsole(TextWriter tw) --- 190,194 ---- Index: TextFormatter.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.UnitTest.Core/TextFormatter.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** TextFormatter.cs 22 Oct 2005 17:15:38 -0000 1.6 --- TextFormatter.cs 26 Oct 2005 05:27:45 -0000 1.7 *************** *** 88,92 **** sb.AppendFormat(this.GetNesting(nesting + 1) + this.GetMarker(tar) + "{0} - {1}\r\n", tar.Name, tar.State); //sb.AppendFormat(this.GetNesting(nesting + 1) + this.GetMarker(tar) + "Time: {0}s\r\n", tar.GetTotalDuration()); ! if((tar is TestResult) && ((tar as TestResult).Description.Length > 0)) sb.AppendFormat(this.GetNesting(nesting + 2) + "Description: {0}\r\n", (tar as TestResult).Description); if(showFailure && (ati is TestIteration)) --- 88,95 ---- sb.AppendFormat(this.GetNesting(nesting + 1) + this.GetMarker(tar) + "{0} - {1}\r\n", tar.Name, tar.State); //sb.AppendFormat(this.GetNesting(nesting + 1) + this.GetMarker(tar) + "Time: {0}s\r\n", tar.GetTotalDuration()); ! if((tar is TestResult) && ((tar as TestResult).Description.Length > 0)) ! { ! sb.AppendFormat(this.GetNesting(nesting + 2) + "Description: {0}\r\n", (tar as TestResult).Description); ! } if(showFailure && (ati is TestIteration)) *************** *** 97,100 **** --- 100,109 ---- } + if(ati is TestIteration) + { + sb.Append(this.GetNesting(nesting + 2) + "Thread Id: " + (ati as TestIteration).Thread + Environment.NewLine); + sb.Append(this.GetNesting(nesting + 2) + "Time: " + (ati as TestIteration).Duration + Environment.NewLine); + } + if(showOutput && ati.ConsoleOutput.Length > 0) { --- NEW FILE: RunNonThreadedTestCommand.cs --- using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading; using Adapdev.Commands; using Adapdev.Diagnostics; using log4net; namespace Adapdev.UnitTest.Core { /// <summary> /// Summary description for NonThreadedRunTestCommand. /// </summary> public class RunNonThreadedTestCommand : ICommand { private MethodInfo _method = null; private object _o = null; // create the logger private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // setup the logging levels private bool _debugMode = log.IsDebugEnabled; private bool _infoMode = log.IsInfoEnabled; private IPerfTimer timer = PerfTimerFactory.GetPerfTimer(PerfTimerType.HIRESSECONDS); private TestEventDispatcher _dispatcher = null; private Test _test = null; private TestFixture _testFixture = null; private Type _type = null; private object o = null; private TestFixtureIteration _testFixtureIteration = null; public RunNonThreadedTestCommand(TestEventDispatcher dispatcher, Test test, TestFixture tf, Type type, object o, TestFixtureIteration tfi) { this._dispatcher = dispatcher; this._test = test; this._testFixture = tf; this.o = o; this._testFixtureIteration = tfi; this._type = type; } public void Execute() { try { MethodInfo m = _type.GetMethod(_test.Method); TestResult tr = new TestResult(_test); tr.TestId = _test.Id; tr.Description = _test.Description; if(this._debugMode) log.Debug("Running T " + _test.Name); if (!_test.Ignore && _test.ShouldRun) { if(_dispatcher != null)_dispatcher.OnTestStarted(new TestEventArgs(_test)); for (int i = 1; i <= _test.RepeatCount; i++) { TextWriter consoleOut = Console.Out; TextWriter errorOut = Console.Error; StringWriter consoleWriter = new StringWriter(); StringWriter errorWriter = new StringWriter(); StringWriter debugWriter = new StringWriter(); StringWriter traceWriter = new StringWriter(); //Console.SetOut(consoleWriter); Console.SetError(errorWriter); TextWriterTraceListener debug = new TextWriterTraceListener(debugWriter); TextWriterTraceListener error = new TextWriterTraceListener(traceWriter); Debug.Listeners.Add(debug); Trace.Listeners.Add(error); if (_test.RepeatDelay > 0) { Thread.Sleep(_test.RepeatDelay); Console.WriteLine("Sleeping..." + _test.RepeatDelay); } TestIteration ti = new TestIteration(); ti.Name = _test.Name; ti.Iteration = i; ti.Thread = AppDomain.GetCurrentThreadId().ToString(); long kStart = 0; long kEnd = 0; if(_dispatcher != null)_dispatcher.OnTestIterationStarted(new TestEventArgs(_test)); try { this.RunTestSetUps(_dispatcher, _testFixture, _test.Name, o, _type); kStart = Process.GetCurrentProcess().WorkingSet; timer.Start(); new RunMethodCommand(m, o).Execute(); timer.Stop(); ti.Duration = timer.Duration; kEnd = Process.GetCurrentProcess().WorkingSet; ti.MemoryUsed = (kEnd - kStart)/1024; if (_test.MaxKMemory > 0 && _test.MaxKMemory < ti.MemoryUsed) { ti.State = TestState.Fail; ti.Result = "Memory usage exceeded MaxK limit of " + _test.MaxKMemory; } else if (_test.MinOperationsPerSecond > 0 && ti.GetOpsPerSecond() < _test.MinOperationsPerSecond) { ti.State = TestState.Fail; ti.Result = "Ops per second was less than minimum limit of " + _test.MinOperationsPerSecond; } else if (_test.ExpectedExceptionType != null && _test.ExpectedExceptionType.Length > 0) { ti.State = TestState.Fail; ti.Result = "ExceptedException type " + _test.ExpectedExceptionType + " was not thrown."; } else { ti.State = TestState.Pass; } this.RunTestTearDowns(_dispatcher, _testFixture, _test.Name, o, _type); } catch (Exception e) { timer.Stop(); ti.Duration = timer.Duration; kEnd = Process.GetCurrentProcess().WorkingSet; if (_test.ExpectedExceptionType != null && _test.ExpectedExceptionType.Length > 0) { Type t = null; foreach(Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) { try { t = ass.GetType(_test.ExpectedExceptionType, true, true); break; } catch(Exception){} } if(t == null) throw new TypeLoadException("Unable to locate " + _test.ExpectedExceptionType + ". Please make sure it is correct."); if (e.InnerException.GetType().IsSubclassOf(t) || e.InnerException.GetType() == t) { if(_test.ExpectedExceptionMessage != null && _test.ExpectedExceptionMessage.Length > 0) { if(_test.ExpectedExceptionMessage.ToLower() == e.InnerException.Message.ToLower()) { ti.Result = "Expected Exception: " + _test.ExpectedExceptionType + " was thrown. Message: " + e.InnerException.Message; ti.State = TestState.Pass; } else { ti.Result = "Expected Exception: " + _test.ExpectedExceptionType + " was thrown, but wrong message. Message: " + e.InnerException.Message + " - Expected: " + _test.ExpectedExceptionMessage; ti.State = TestState.Fail; } } else { ti.Result = "Expected Exception: " + _test.ExpectedExceptionType + " was thrown. Message: " + e.InnerException.Message; ti.State = TestState.Pass; } } else { ti.Result = "Expected Exception: " + _test.ExpectedExceptionType + " was NOT thrown. Message: " + e.InnerException.Message; ti.State = TestState.Fail; } ti.ExceptionType = e.InnerException.GetType().FullName; ti.FullStackTrace = e.InnerException.StackTrace; ti.MemoryUsed = (kEnd - kStart)/1024; } // TODO : Fix incrementing of tests in GUI when IgnoreException is thrown else if(e.InnerException.GetType() == typeof(IgnoreException) || e.InnerException.GetType() == typeof(IgnoreException)) { ti.Result = e.InnerException.Message; ti.State = TestState.ForcedIgnore; ti.ExceptionType = e.InnerException.GetType().FullName; ti.FullStackTrace = e.InnerException.StackTrace; } else { ti.Result = e.InnerException.Message; ti.ExceptionType = e.InnerException.GetType().FullName; ti.FullStackTrace = e.InnerException.StackTrace; ti.State = TestState.Fail; } this.RunTestTearDowns(_dispatcher, _testFixture, _test.Name, o, _type); } ti.ConsoleOutput = consoleWriter.ToString(); ti.ConsoleError = errorWriter.ToString(); ti.DebugOutput = debugWriter.ToString(); ti.TraceOutput = traceWriter.ToString(); lock(this){tr.AddIteration(ti);} if(_dispatcher != null)_dispatcher.OnTestIterationCompleted(new TestIterationEventArgs(ti)); Console.SetOut(consoleOut); Console.SetError(errorOut); Debug.Listeners.Remove(debug); Trace.Listeners.Remove(error); debug.Dispose(); error.Dispose(); } } else { TestIteration ti = new TestIteration(); ti.Name = _test.Name; ti.State = TestState.Ignore; ti.Result = _test.IgnoreReason; tr.AddIteration(ti); } if (_testFixture.ShouldShow) _testFixtureIteration.AddTestResult(tr); if(_dispatcher != null)_dispatcher.OnTestCompleted(new TestResultEventArgs(tr)); } catch (Exception e) { Console.WriteLine(e.Message + " " + e.StackTrace); } finally { } } protected void RunTestSetUps(TestEventDispatcher _dispatcher, TestFixture tf, string name, object instance, Type type) { foreach (TestHelper t in tf.GetTestSetUps()) { try { // Console.WriteLine(t.Method); if (t.Test.Length < 1 || t.Test.ToLower().Equals(name.ToLower())) { if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); MethodInfo m = type.GetMethod(t.Method); m.Invoke(instance, null); } } catch (Exception e) { Console.Write(e); } } } protected void RunTestTearDowns(TestEventDispatcher _dispatcher, TestFixture tf, string name, object instance, Type type) { foreach (TestHelper t in tf.GetTestTearDowns()) { try { if (t.Test.Length < 1 || t.Test.ToLower().Equals(name.ToLower())) { if(_dispatcher != null)_dispatcher.OnBaseTestHelperStarted(new BaseTestHelperEventArgs(t)); MethodInfo m = type.GetMethod(t.Method); m.Invoke(instance, null); } } catch (Exception e) { Console.Write(e); } } } public void Execute(object o){this.Execute();} } } |