1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright © 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright © 2000-2003 Philip A. Craig
7 ' This software is provided 'as-is', without any express or implied warranty. In no
8 ' event will the authors be held liable for any damages arising from the use of this
11 ' Permission is granted to anyone to use this software for any purpose, including
12 ' commercial applications, and to alter it and redistribute it freely, subject to the
13 ' following restrictions:
15 ' 1. The origin of this software must not be misrepresented; you must not claim that
16 ' you wrote the original software. If you use this software in a product, an
17 ' acknowledgment (see the following) in the product documentation is required.
19 ' Portions Copyright © 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright © 2000-2003 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
34 using System
.Reflection
;
37 /// Summary description for TestCase.
39 public abstract class TemplateTestCase
: TestCase
41 private MethodInfo method
;
43 public TemplateTestCase(Type fixtureType
, MethodInfo method
) : base(fixtureType
.FullName
, method
.Name
)
45 this.fixtureType
= fixtureType
;
49 public TemplateTestCase(object fixture
, MethodInfo method
) : base(fixture
.GetType().FullName
, method
.Name
)
51 this.Fixture
= fixture
;
52 this.fixtureType
= fixture
.GetType();
56 public override void Run(TestCaseResult testResult
)
60 bool doParentSetUp
= false;
63 doParentSetUp
= !Parent
.IsSetUp
;
70 Parent
.DoSetUp( testResult
);
72 if ( Fixture
== null && Parent
!= null)
73 Fixture
= Parent
.Fixture
;
75 if ( !testResult
.IsFailure
)
80 if ( ex
is NunitException
)
81 ex
= ex
.InnerException
;
83 if ( ex
is NUnit
.Framework
.IgnoreException
)
84 testResult
.NotRun( ex
.Message
);
86 RecordException( ex
, testResult
);
91 Parent
.DoTearDown( testResult
);
96 testResult
.NotRun(this.IgnoreReason
);
101 /// The doRun method is used to run a test internally.
102 /// It assumes that the caller is taking care of any
103 /// TestFixtureSetUp and TestFixtureTearDown needed.
105 /// <param name="testResult">The result in which to record success or failure</param>
106 public void doRun( TestCaseResult testResult
)
108 DateTime start
= DateTime
.Now
;
112 Reflect
.InvokeSetUp( this.Fixture
);
113 doTestCase( testResult
);
117 if ( ex
is NunitException
)
118 ex
= ex
.InnerException
;
120 if ( ex
is NUnit
.Framework
.IgnoreException
)
121 testResult
.NotRun( ex
.Message
);
123 RecordException( ex
, testResult
);
127 doTearDown( testResult
);
129 DateTime stop
= DateTime
.Now
;
130 TimeSpan span
= stop
.Subtract(start
);
131 testResult
.Time
= (double)span
.Ticks
/ (double)TimeSpan
.TicksPerSecond
;
135 #region Invoke Methods by Reflection, Recording Errors
137 private void doTearDown( TestCaseResult testResult
)
141 Reflect
.InvokeTearDown( this.Fixture
);
145 if ( ex
is NunitException
)
146 ex
= ex
.InnerException
;
147 RecordException(ex
, testResult
, true);
151 private void doTestCase( TestCaseResult testResult
)
155 Reflect
.InvokeMethod( this.method
, this.Fixture
);
156 ProcessNoException(testResult
);
158 catch( Exception ex
)
160 if ( ex
is NunitException
)
161 ex
= ex
.InnerException
;
163 if ( ex
is NUnit
.Framework
.IgnoreException
)
164 testResult
.NotRun( ex
.Message
);
166 ProcessException(ex
, testResult
);
172 #region Record Info About An Exception
174 protected void RecordException( Exception exception
, TestCaseResult testResult
)
176 RecordException( exception
, testResult
, false );
179 protected void RecordException( Exception exception
, TestCaseResult testResult
, bool inTearDown
)
181 StringBuilder msg
= new StringBuilder();
182 StringBuilder st
= new StringBuilder();
186 msg
.Append( testResult
.Message
);
187 msg
.Append( Environment
.NewLine
);
188 msg
.Append( "TearDown : " );
189 st
.Append( testResult
.StackTrace
);
190 st
.Append( Environment
.NewLine
);
191 st
.Append( "--TearDown" );
192 st
.Append( Environment
.NewLine
);
195 msg
.Append( BuildMessage( exception
) );
196 st
.Append( BuildStackTrace( exception
) );
197 testResult
.Failure( msg
.ToString(), st
.ToString() );
200 private string BuildMessage(Exception exception
)
202 StringBuilder sb
= new StringBuilder();
203 if ( exception
is NUnit
.Framework
.AssertionException
)
204 sb
.Append( exception
.Message
);
206 sb
.AppendFormat( "{0} : {1}", exception
.GetType().ToString(), exception
.Message
);
208 Exception inner
= exception
.InnerException
;
209 while( inner
!= null )
211 sb
.Append( Environment
.NewLine
);
212 sb
.AppendFormat( " ----> {0} : {1}", inner
.GetType().ToString(), inner
.Message
);
213 inner
= inner
.InnerException
;
216 return sb
.ToString();
219 private string BuildStackTrace(Exception exception
)
221 if(exception
.InnerException
!=null)
222 return exception
.StackTrace
+ Environment
.NewLine
+
223 "--" + exception
.GetType().Name
+ Environment
.NewLine
+
224 BuildStackTrace(exception
.InnerException
);
226 return exception
.StackTrace
;
231 #region Abstract Methods
233 protected internal abstract void ProcessNoException(TestCaseResult testResult
);
235 protected internal abstract void ProcessException(Exception exception
, TestCaseResult testResult
);