source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/lib/NUnit/NUnit-2.5.10.11092/doc/testFixture.html@ 1146

Last change on this file since 1146 was 1146, checked in by Sam Habiel, 13 years ago

Initial Import of BMX4

File size: 13.3 KB
Line 
1<!-- saved from url=(0014)about:internet --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
2<html>
3<!-- Standard Head Part -->
4<head>
5<title>NUnit - TestFixture</title>
6<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
7<meta http-equiv="Content-Language" content="en-US">
8<link rel="stylesheet" type="text/css" href="nunit.css">
9<link rel="shortcut icon" href="favicon.ico">
10</head>
11<!-- End Standard Head Part -->
12
13<body>
14
15<!-- Standard Header for NUnit.org -->
16<div id="header">
17 <a id="logo" href="http://www.nunit.org"><img src="img/logo.gif" alt="NUnit.org" title="NUnit.org"></a>
18 <div id="nav">
19 <a href="http://www.nunit.org">NUnit</a>
20 <a class="active" href="index.html">Documentation</a>
21 </div>
22</div>
23<!-- End of Header -->
24
25<div id="content">
26
27<script language="JavaScript" src="codeFuncs.js" ></script> <!-- Do it this way for IE -->
28
29<h3>TestFixtureAttribute (NUnit 2.0 / 2.5)</h3>
30
31<p>This is the attribute that marks a class that contains tests and, optionally,
32 setup or teardown methods. NUnit 2.5 introduces parameterized and generic
33 test fixtures - see below.</p>
34
35<p>Most restrictions on a class that is used as a test fixture have now been
36 eliminated. As of NUnit 2.5.3, a test fixture class:
37 <ul>
38 <li>May be public, protected, private or internal.
39 <li>May be a static class in .NET 2.0 or later.
40 <li>May be generic, so long as any type parameters are provided or
41 can be inferred from the actual arguments.
42 <li>May not be abstract - although the attribute may be applied to an
43 abstract class intended to serve as a base class for test fixtures.
44 <li>If no arguments are provided with the TestFixtureAttribute, the class
45 must have a default constructor.
46 <li>If arguments are provided, they must match one of the constructors.
47 </ul>
48</p>
49
50<p>If any of these restrictions are violated, the class is not runnable
51 as a test and will display as an error.</p>
52
53<p>It is advisable that the constructor not have any side effects,
54 since NUnit may construct the object multiple times in the course of a session.</li>
55
56<p>Beginning with NUnit 2.5, the <b>TestFixture</b> attribute is optional
57 for non-parameterized, non-generic fixtures. So long as the class contains
58 at least one method marked with the <b>Test</b>, <b>TestCase</b> or
59 <b>TestCaseSource</b> attribute, it will be treated as a test fixture.
60
61<h4>Example:</h4>
62
63<div class="code">
64
65<div class="langFilter">
66 <a href="javascript:Show('DD1')" onmouseover="Show('DD1')"><img src="img/langFilter.gif" width="14" height="14" alt="Language Filter"></a>
67 <div id="DD1" class="dropdown" style="display: none;" onclick="Hide('DD1')">
68 <a href="javascript:ShowCS()">C#</a><br>
69 <a href="javascript:ShowVB()">VB</a><br>
70 <a href="javascript:ShowMC()">C++</a><br>
71 <a href="javascript:ShowJS()">J#</a><br>
72 </div>
73</div>
74
75<pre class="cs">namespace NUnit.Tests
76{
77 using System;
78 using NUnit.Framework;
79
80 [TestFixture]
81 public class SuccessTests
82 {
83 // ...
84 }
85}
86</pre>
87
88<pre class="vb">Imports System
89Imports Nunit.Framework
90
91Namespace Nunit.Tests
92
93 &lt;TestFixture()&gt; Public Class SuccessTests
94 ' ...
95 End Class
96End Namespace
97</pre>
98
99<pre class="mc">#using &lt;Nunit.Framework.dll&gt;
100using namespace System;
101using namespace NUnit::Framework;
102
103namespace NUnitTests
104{
105 [TestFixture]
106 public __gc class SuccessTests
107 {
108 // ...
109 };
110}
111
112#include "cppsample.h"
113
114namespace NUnitTests {
115 // ...
116}
117</pre>
118
119<pre class="js">package NUnit.Tests;
120
121import System.*;
122import NUnit.Framework.TestFixture;
123
124
125/** @attribute NUnit.Framework.TestFixture() */
126public class SuccessTests
127{
128 // ...
129}
130</pre>
131
132</div>
133
134<h3>Inheritance</h3>
135
136<p>The <b>TestFixtureAttribute</b> may be applied to a base class and is
137inherited by any derived classes. This includes any abstract base class,
138so the well-known Abstract Fixture pattern may be implemented if desired.
139
140<p>In order to facilitate use of generic and/or parameterized classes,
141where the derived class may require a different number of arguments (or
142type arguments) from the base class, superfluous <b>TestFixture</b>
143attributes are ignored, using the following rules:
144
145<ol>
146<li>If all TestFixture attributes provide constructor or type arguments,
147then all of them are used.
148<li>If some of the attributes provide arguments and others do not, then
149only those with arguments are used and those without arguments are ignored.
150<li>If none of the attributes provide arguments, one of them is selected
151for use by NUnit. It is not possible to predict which will be used, so
152this situation should generally be avoided.
153</ol>
154
155This permits code like the following, which would cause an error if the
156attribute on the base class were not ignored.
157
158<div class="code">
159<pre>[TestFixture]
160public class AbstractFixtureBase
161{
162 ...
163}
164
165[TestFixture(typeof(string))]
166public class DerivedFixture&lt;T&gt; : AbstractFixtureBase
167{
168 ...
169}
170</pre>
171</div>
172
173<h3>Parameterized Test Fixtures (NUnit 2.5)</h3>
174
175<p>Beginning with NUnit 2.5, test fixtures may take constructor arguments.
176 Argument values are specified as arguments to the <b>TestFixture</b>
177 attribute. NUnit will construct a separate instance of the fixture
178 for each set of arguments.
179
180<p>Individual fixture instances in a set of parameterized fixtures may be ignored.
181 Set the <b>Ignore</b> named parameter of the attribute to true or set
182 <b>IgnoreReason</b> to a non-empty string.
183
184<p>Individual fixture instances may be given categories as well. Set the <b>Category</b>
185 named parameter of the attribute to the name of the category or to a comma-separated
186 list of categories.
187
188<h4>Example</h4>
189
190<p>The following test fixture would be instantiated by NUnit three times,
191 passing in each set of arguments to the appropriate constructor. Note
192 that there are three different constructors, matching the data types
193 provided as arguments.
194
195<div class="code"><pre>
196[TestFixture("hello", "hello", "goodbye")]
197[TestFixture("zip", "zip")]
198[TestFixture(42, 42, 99)]
199public class ParameterizedTestFixture
200{
201 private string eq1;
202 private string eq2;
203 private string neq;
204
205 public ParameterizedTestFixture(string eq1, string eq2, string neq)
206 {
207 this.eq1 = eq1;
208 this.eq2 = eq2;
209 this.neq = neq;
210 }
211
212 public ParameterizedTestFixture(string eq1, string eq2)
213 : this(eq1, eq2, null) { }
214
215 public ParameterizedTestFixture(int eq1, int eq2, int neq)
216 {
217 this.eq1 = eq1.ToString();
218 this.eq2 = eq2.ToString();
219 this.neq = neq.ToString();
220 }
221
222 [Test]
223 public void TestEquality()
224 {
225 Assert.AreEqual(eq1, eq2);
226 if (eq1 != null &amp;&amp; eq2 != null)
227 Assert.AreEqual(eq1.GetHashCode(), eq2.GetHashCode());
228 }
229
230 [Test]
231 public void TestInequality()
232 {
233 Assert.AreNotEqual(eq1, neq);
234 if (eq1 != null &amp;&amp; neq != null)
235 Assert.AreNotEqual(eq1.GetHashCode(), neq.GetHashCode());
236 }
237}
238</pre></div>
239
240<h3>Generic Test Fixtures (NUnit 2.5)</h3>
241
242<p>Beginning with NUnit 2.5, you may also use a generic class as a test fixture.
243 In order for NUnit to instantiate the fixture, you must either specify the
244 types to be used as arguments to <b>TestFixtureAttribute</b> or use the
245 named parameter <b>TypeArgs=</b> to specify them. NUnit will construct a
246 separate instance of the fixture for each <b>TestFixtureAttribute</b>
247 you provide.
248
249<h4>Example</h4>
250
251<p>The following test fixture would be instantiated by NUnit twice,
252 once using an ArrayList and once using a List&lt;int&gt;.
253
254<div class="code"><pre>
255[TestFixture(typeof(ArrayList))]
256[TestFixture(typeof(List&lt;int&gt;))]
257public class IList_Tests&lt;TList&gt; where TList : IList, new()
258{
259 private IList list;
260
261 [SetUp]
262 public void CreateList()
263 {
264 this.list = new TList();
265 }
266
267 [Test]
268 public void CanAddToList()
269 {
270 list.Add(1); list.Add(2); list.Add(3);
271 Assert.AreEqual(3, list.Count);
272 }
273}</pre></div>
274
275<h3>Generic Test Fixtures with Parameters (NUnit 2.5)</h3>
276
277<p>If a Generic fixture, uses constructor arguments, there are three
278 approaches to telling NUnit which arguments are type parameters
279 and which are normal constructor parameters.
280 <ol>
281 <li>Specify both sets of parameters as arguments to the <b>TestFixtureAttribute</b>.
282 Leading <b>System.Type</b> arguments are used as type parameters, while
283 any remaining arguments are used to construct the instance. In the
284 following example, this leads to some obvious duplication...
285
286<div class="code"><pre>
287[TestFixture(typeof(double), typeof(int), 100.0, 42)]
288[TestFixture(typeof(int) typeof(double), 42, 100.0)]
289public class SpecifyBothSetsOfArgs&lt;T1, T2&gt;
290{
291 T1 t1;
292 T2 t2;
293
294 public SpecifyBothSetsOfArgs(T1 t1, T2 t2)
295 {
296 this.t1 = t1;
297 this.t2 = t2;
298 }
299
300 [TestCase(5, 7)]
301 public void TestMyArgTypes(T1 t1, T2 t2)
302 {
303 Assert.That(t1, Is.TypeOf&lt;T1&gt;());
304 Assert.That(t2, Is.TypeOf&lt;T2&gt;());
305 }
306}</pre></div>
307
308 <li>Specify normal parameters as arguments to <b>TestFixtureAttribute</b>
309 and use the named parameter <b>TypeArgs=</b> to specify the type
310 arguments. Again, for this example, the type info is duplicated, but
311 it is at least more cleanly separated from the normal arguments...
312
313<div class="code" style="width: 40em"><pre>
314[TestFixture(100.0, 42, TypeArgs=new Type[] {typeof(double), typeof(int) } )]
315[TestFixture(42, 100.0, TypeArgs=new Type[] {typeof(int), typeof(double) } )]
316public class SpecifyTypeArgsSeparately&lt;T1, T2&gt;
317{
318 T1 t1;
319 T2 t2;
320
321 public SpecifyTypeArgsSeparately(T1 t1, T2 t2)
322 {
323 this.t1 = t1;
324 this.t2 = t2;
325 }
326
327 [TestCase(5, 7)]
328 public void TestMyArgTypes(T1 t1, T2 t2)
329 {
330 Assert.That(t1, Is.TypeOf&lt;T1&gt;());
331 Assert.That(t2, Is.TypeOf&lt;T2&gt;());
332 }
333}</pre></div>
334
335 <li>In some cases, when the constructor makes use of all the type parameters
336 NUnit may simply be able to deduce them from the arguments provided.
337 That's the case here and the following is the preferred way to
338 write this example...
339
340<div class="code"><pre>
341[TestFixture(100.0, 42)]
342[TestFixture(42, 100.0)]
343public class DeduceTypeArgsFromArgs&lt;T1, T2&gt;
344{
345 T1 t1;
346 T2 t2;
347
348 public DeduceTypeArgsFromArgs(T1 t1, T2 t2)
349 {
350 this.t1 = t1;
351 this.t2 = t2;
352 }
353
354 [TestCase(5, 7)]
355 public void TestMyArgTypes(T1 t1, T2 t2)
356 {
357 Assert.That(t1, Is.TypeOf&lt;T1&gt;());
358 Assert.That(t2, Is.TypeOf&lt;T2&gt;());
359 }
360}</pre></div>
361 </ol>
362
363</div>
364
365<!-- Submenu -->
366<div id="subnav">
367<ul>
368<li><a href="index.html">NUnit 2.5.10</a></li>
369<ul>
370<li><a href="getStarted.html">Getting&nbsp;Started</a></li>
371<li><a href="assertions.html">Assertions</a></li>
372<li><a href="constraintModel.html">Constraints</a></li>
373<li><a href="attributes.html">Attributes</a></li>
374<ul>
375<li><a href="category.html">Category</a></li>
376<li><a href="combinatorial.html">Combinatorial</a></li>
377<li><a href="culture.html">Culture</a></li>
378<li><a href="datapoint.html">Datapoint(s)</a></li>
379<li><a href="description.html">Description</a></li>
380<li><a href="exception.html">Exception</a></li>
381<li><a href="explicit.html">Explicit</a></li>
382<li><a href="ignore.html">Ignore</a></li>
383<li><a href="maxtime.html">Maxtime</a></li>
384<li><a href="pairwise.html">Pairwise</a></li>
385<li><a href="platform.html">Platform</a></li>
386<li><a href="property.html">Property</a></li>
387<li><a href="random.html">Random</a></li>
388<li><a href="range.html">Range</a></li>
389<li><a href="repeat.html">Repeat</a></li>
390<li><a href="requiredAddin.html">RequiredAddin</a></li>
391<li><a href="requiresMTA.html">Requires&nbsp;MTA</a></li>
392<li><a href="requiresSTA.html">Requires&nbsp;STA</a></li>
393<li><a href="requiresThread.html">Requires&nbsp;Thread</a></li>
394<li><a href="sequential.html">Sequential</a></li>
395<li><a href="setCulture.html">SetCulture</a></li>
396<li><a href="setUICulture.html">SetUICulture</a></li>
397<li><a href="setup.html">Setup</a></li>
398<li><a href="setupFixture.html">SetupFixture</a></li>
399<li><a href="suite.html">Suite</a></li>
400<li><a href="teardown.html">Teardown</a></li>
401<li><a href="test.html">Test</a></li>
402<li><a href="testCase.html">TestCase</a></li>
403<li><a href="testCaseSource.html">TestCaseSource</a></li>
404<li id="current"><a href="testFixture.html">TestFixture</a></li>
405<li><a href="fixtureSetup.html">TestFixtureSetUp</a></li>
406<li><a href="fixtureTeardown.html">TestFixtureTearDown</a></li>
407<li><a href="theory.html">Theory</a></li>
408<li><a href="timeout.html">Timeout</a></li>
409<li><a href="values.html">Values</a></li>
410<li><a href="valueSource.html">ValueSource</a></li>
411</ul>
412<li><a href="runningTests.html">Running&nbsp;Tests</a></li>
413<li><a href="extensibility.html">Extensibility</a></li>
414<li><a href="releaseNotes.html">Release&nbsp;Notes</a></li>
415<li><a href="samples.html">Samples</a></li>
416<li><a href="license.html">License</a></li>
417</ul>
418</ul>
419</div>
420<!-- End of Submenu -->
421
422
423<!-- Standard Footer for NUnit.org -->
424<div id="footer">
425 Copyright &copy; 2010 Charlie Poole. All Rights Reserved.
426</div>
427<!-- End of Footer -->
428
429</body>
430</html>
Note: See TracBrowser for help on using the repository browser.