Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestCase.cs « NUnitCore « src « nunit « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e2e746394bf951c98c1dc086aab66d5eb4849a0e (plain)
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
namespace NUnit.Framework 
{
	using System;
	using System.Reflection;

	/// <summary>A test case defines the fixture to run multiple Tests.</summary>
	/// <remarks> To define a test case
	/// <list type="number">
	/// <item><description>implement a subclass of TestCase</description></item>
	/// <item><description>define instance variables that store the state
	/// of the fixture</description></item>
	/// <item><description>initialize the fixture state by overriding
	/// <c>SetUp</c></description></item>
	/// <item><description>clean-up after a test by overriding
	/// <c>TearDown</c></description></item>
	/// </list>
	/// Each test runs in its own fixture so there can be no side effects 
	/// among test runs.
	/// <example>Here is an example:
	/// <code>
	/// public class MathTest: TestCase {
	///   protected double fValue1;
	///   protected double fValue2;
	///
	///   public MathTest(string name) : base(name) {}
	///
	///   protected override void SetUp() {
	///     fValue1= 2.0;
	///     fValue2= 3.0;
	///   }
	/// }</code>
	/// </example>
	///
	/// For each test implement a method which interacts with the fixture.
	/// Verify the expected results with Assertions specified by calling 
	/// <c>Assert</c> with a bool.
	/// <code>
	///    protected void TestAdd() {
	///        double result= fValue1 + fValue2;
	///        Assert(result == 5.0);
	///    }
	/// </code>
	/// Once the methods are defined you can run them. The framework supports
	/// both a static type safe and more dynamic way to run a test.
	/// In the static way you override the runTest method and define the method
	/// to be invoked.
	/// <code>
	/// protected class AddMathTest: TestCase {
	///   public void AddMathTest(string name) : base(name) {}
	///   protected override void RunTest() { TestAdd(); }
	/// }
	///
	/// test test= new AddMathTest("Add");
	/// test.Run();
	/// </code>
	/// The dynamic way uses reflection to implement <c>RunTest</c>. It
	/// dynamically finds and invokes a method. In this case the name of the
	/// test case has to correspond to the test method to be run.
	/// <code>
	/// test= new MathTest("TestAdd");
	/// test.Run();
	/// </code>
	/// The Tests to be run can be collected into a <see cref="TestSuite"/>.
	/// NUnit provides different test runners which can run a test suite
	/// and collect the results.
	/// A test runner either expects a static property <c>Suite</c> as the entry
	/// point to get a test to run or it will extract the suite automatically.
	/// <code>
	/// public static ITest Suite {
	///    get {
	///      suite.AddTest(new MathTest("TestAdd"));
	///      suite.AddTest(new MathTest("TestDivideByZero"));
	///      return suite;
	///    }
	///  }
	/// </code></remarks>
	/// <seealso cref="TestResult"/>
	/// <seealso cref="TestSuite"/>
	public abstract class TestCase: Assertion, ITest
	{
		#region Instance Variables
		/// <summary>the name of the test case.</summary>
		private readonly string fName;
		#endregion

		#region Constructors
		/// <summary>Constructs a test case with no name.</summary>
		public TestCase() : this(String.Empty){}
		
		/// <summary>Constructs a test case with the given name.</summary>
		public TestCase(string testName) 
		{
			this.fName = testName;
		}
		#endregion

		#region Properties
		/// <value>Counts the number of test cases executed by
		/// Run(TestResult result).</value>
		public int CountTestCases 
		{
			get { return 1; }
		}

		/// <value>The name of the test case.</value>
		public string Name 
		{
			get { return this.fName; }
		}
		#endregion

		#region Utility Methods
		/// <summary>Creates a default <see cref="TestResult"/> object.</summary>
		protected TestResult CreateResult() 
		{
			return new TestResult();
		}
		#endregion

		#region Run Methods
		/// <summary>A convenience method to run this test, collecting the
		/// results with a default <see cref="TestResult"/> object.</summary>
		public TestResult Run() 
		{
			TestResult result = this.CreateResult();
			this.Run(result);
			return result;
		}

		/// <summary>Runs the test case and collects the results in
		/// TestResult.</summary>
		public void Run(TestResult result) 
		{
			result.Run(this);
		}

		/// <summary>Runs the bare test sequence.</summary>
		public void RunBare() 
		{
			this.SetUp();
			try 
			{
				this.RunTest();
			}
			finally 
			{
				this.TearDown();
			}
		}

		/// <summary>Override to run the test and Assert its state.</summary>
		protected virtual void RunTest() 
		{
			MethodInfo runMethod = GetType().GetMethod(this.Name, new Type[0]);
			if (runMethod == null)
				Assertion.Fail("Method \""+this.Name+"\" not found");
          
			if (runMethod != null && !runMethod.IsPublic) 
			{
				Assertion.Fail("Method \""+this.Name+"\" should be public");
			}

			object[] exa = 
				runMethod.GetCustomAttributes(typeof(ExpectExceptionAttribute),true);

			try 
			{
				runMethod.Invoke(this, null);
			}
			catch (AssertionFailedError e) 
			{
				throw new NUnitException("Run Error: ", e);
			}
			catch (TargetInvocationException e) 
			{
				Exception inner = e.InnerException;
				if (inner is AssertionFailedError) 
				{
					throw new NUnitException("Run Error: ", inner);
				}
				if (exa.Length>0)
				{
					foreach (ExpectExceptionAttribute ex in exa) 
					{
						if (ex.ExceptionExpected.IsAssignableFrom(inner.GetType()))
							return;
					}
					Assertion.Fail("Unexpected Exception thrown: " + inner);
				} 
				else
				{
					throw new NUnitException("Run Error: ", inner);
				}
			}
			catch (MemberAccessException e) 
			{
				throw new NUnitException("", e);
			}

			if (exa.Length > 0) 
			{
				System.Text.StringBuilder sb =
					new System.Text.StringBuilder
					("One of these exceptions should have been thrown: ");
				bool first = true;
				foreach (ExpectExceptionAttribute ex in exa) 
				{
					if(first)
						first = false;
					else
						sb.Append(", ");
					sb.Append(ex);
				}
				Assertion.Fail(sb.ToString());
			}
		}

		/// <summary>
		/// Sets up the fixture, for example, open a network connection.
		/// This method is called before a test is executed.
		/// </summary>
		protected virtual void SetUp() {}
		/// <summary>
		/// Tears down the fixture, for example, close a network
		/// connection. This method is called after a test is executed.
		/// </summary>
		protected virtual void TearDown() {}
		#endregion

		#region Overrides
		/// <summary>
		/// Returns a string representation of the test case.
		/// </summary>
		public override string ToString() 
		{
			return this.Name+"("+this.GetType().ToString()+")";
		}
		#endregion

	}
}