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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<!-- Standard Head Part -->
<head>
<title>NUnit - SetupTeardown</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="Content-Language" content="en-US">
<link rel="stylesheet" type="text/css" href="nunit.css">
<link rel="shortcut icon" href="favicon.ico">
<style type="text/css"><!--
--></style>
</head>
<!-- End Standard Head Part -->
<body>
<!-- Standard Header for NUnit.org -->
<div id="header">
<a id="logo" href="index.html"><img src="img/logo.gif" alt="NUnit.org" title="NUnit.org"></a>
<div id="nav">
<a href="index.html">Home</a>
<a href="download.html">Download</a>
<a href="docHome.html" class="active">Documentation</a>
<a href="community.html">Community</a>
<a href="resources.html">Resources</a>
<a href="contactUs.html">Contact Us</a>
</div>
</div>
<!-- End of Header -->
<!-- Main Content -->
<div id="content">
<script language="JavaScript">
function Show( id ) {
document.getElementById(id).style.display = "";
}
function Hide( id ) {
document.getElementById(id).style.display = "none";
}
function ShowCS()
{
Show('CS1'); Hide('VB1'); Hide('MC1'); Hide('JS1');
Show('CS2'); Hide('VB2'); Hide('MC2'); Hide('JS2');
}
function ShowVB()
{
Hide('CS1'); Show('VB1'); Hide('MC1'); Hide('JS1');
Hide('CS2'); Show('VB2'); Hide('MC2'); Hide('JS2');
}
function ShowMC()
{
Hide('CS1'); Hide('VB1'); Show('MC1'); Hide('JS1');
Hide('CS2'); Hide('VB2'); Show('MC2'); Hide('JS2');
}
function ShowJS()
{
Hide('CS1'); Hide('VB1'); Hide('MC1'); Show('JS1');
Hide('CS2'); Hide('VB2'); Hide('MC2'); Show('JS2');
}
</script>
<h3>SetUp and TearDown Attributes</h3>
<p>These attributes are used to construct the test environment prior to running the
tests (<a href="#TestFixtureSetUp/TestFixtureTearDown">
TestFixtureSetUpAttribute</a> and <a href="#SetUp/TearDown">SetUpAttribute</a>)
and also to restore the environment after the tests are completed (<a href="#TestFixtureSetUp/TestFixtureTearDown">
TestFixtureTearDownAttribute</a> and <a href="#SetUp/TearDown">TearDownAttribute</a>)</p>
<h4><a name="TestFixtureSetUp/TestFixtureTearDown"> TestFixtureSetUp/TestFixtureTearDown</a> (NUnit 2.1)</h4>
<p>These two attributes are used inside a TestFixture to provide a single set of
functions that are performed once (TestFixtureSetUp) prior to executing any of
the tests in the fixture and once after (TestFixtureTearDown) all tests are
completed. A TestFixture can have only one TestFixtureSetUp method and only one
TestFixtureTearDown method. If more than one of each type is defined the
TestFixture will not be run. It will compile however.</p>
<h4>Example:</h4>
<div class="code">
<div class="langFilter">
<a href="javascript:Show('DD1')" onmouseover="Show('DD1')"><img src="img/langFilter.gif" width="14" height="14" alt="Language Filter"></a>
<div id="DD1" class="dropdown" style="display: none;" onclick="Hide('DD1')">
<a href="javascript:ShowCS()">C#</a><br>
<a href="javascript:ShowVB()">VB</a><br>
<a href="javascript:ShowMC()">C++</a><br>
<a href="javascript:ShowJS()">J#</a><br>
</div>
</div>
<pre id="CS1" style="display:">namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests
{
[TestFixtureSetUp] public void Init()
{ /* ... */ }
[TestFixtureTearDown] public void Dispose()
{ /* ... */ }
[Test] public void Add()
{ /* ... */ }
}
}
</pre>
<pre id="VB1" style="display:none">Imports System
Imports Nunit.Framework
Namespace Nunit.Tests
<TestFixture()> Public Class SuccessTests
<TestFixtureSetUp()> Public Sub Init()
' ...
End Sub
<TestFixtureTearDown()> Public Sub Dispose()
' ...
End Sub
<Test()> Public Sub Add()
' ...
End Sub
End Class
End Namespace
</pre>
<pre id="MC1" style="display:none">#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;
namespace NUnitTests
{
[TestFixture]
public __gc class SuccessTests
{
[TestFixtureSetUp] void Init();
[TestFixtureTearDown] void Dispose();
[Test] void Add();
};
}
#include "cppsample.h"
namespace NUnitTests {
// ...
}
</pre>
<pre id="JS1" style="display:none">package NUnit.Tests;
import System.*;
import NUnit.Framework.TestFixture;
/** @attribute NUnit.Framework.TestFixture() */
public class SuccessTests
{
/** @attribute NUnit.Framework.TestFixtureSetUp() */
public void Init()
{ /* ... */ }
/** @attribute NUnit.Framework.TestFixtureTearDown() */
public void Dispose()
{ /* ... */ }
/** @attribute NUnit.Framework.Test() */
public void Add()
{ /* ... */ }
}
</pre>
</div>
<h4><a name="SetUp/TearDown">SetUp/TearDown</a> (NUnit 2.0)</h4>
<p>These two attributes are used inside a TestFixture to provide a common set of
functions that are performed prior (SetUp) and after (TearDown) a test method
is called. A TestFixture can have only one SetUp method and only one TearDown
method. If more than one of each type is defined the TestFixture will not be
run. It will compile however.</p>
<h4>Example:</h4>
<div class="code">
<div class="langFilter">
<a href="javascript:Show('DD2')" onmouseover="Show('DD2')"><img src="img/langFilter.gif" width="14" height="14" alt="Language Filter"></a>
<div id="DD2" class="dropdown" style="display: none" onclick="Hide('DD2')">
<a href="javascript:ShowCS()">C#</a><br>
<a href="javascript:ShowVB()">VB</a><br>
<a href="javascript:ShowMC()">C++</a><br>
<a href="javascript:ShowJS()">J#</a><br>
</div>
</div>
<pre id="CS2" style="display:">namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests
{
[SetUp] public void Init()
{ /* ... */ }
[TearDown] public void Dispose()
{ /* ... */ }
[Test] public void Add()
{ /* ... */ }
}
}
</pre>
<pre id="VB2" style="display:none">Imports System
Imports Nunit.Framework
Namespace Nunit.Tests
<TestFixture()> Public Class SuccessTests
<SetUp()> Public Sub Init()
' ...
End Sub
<TearDown()> Public Sub Dispose()
' ...
End Sub
<Test()> Public Sub Add()
' ...
End Sub
End Class
End Namespace
</pre>
<pre id="MC2" style="display:none">#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;
namespace NUnitTests
{
[TestFixture]
public __gc class SuccessTests
{
[SetUp] void Init();
[TearDown] void Dispose();
[Test] void Add();
};
}
#include "cppsample.h"
namespace NUnitTests {
// ...
}
</pre>
<pre id="JS2" style="display:none">package NUnit.Tests;
import System.*;
import NUnit.Framework.TestFixture;
/** @attribute NUnit.Framework.TestFixture() */
public class SuccessTests
{
/** @attribute NUnit.Framework.SetUp() */
public void Init()
{ /* ... */ }
/** @attribute NUnit.Framework.TearDown() */
public void Dispose()
{ /* ... */ }
/** @attribute NUnit.Framework.Test() */
public void Add()
{ /* ... */ }
}
</pre>
</div>
<h4>SetUp/TearDown Inheritance</h4>
<p>These two attributes are inherited from base classes. Therefore, if a base
class has defined a <code>SetUp</code> method that method will be called prior
to execution of test methods in the derived class. If you wish to add more
SetUp/TearDown functionality in a derived class you need to mark the method
with the appropriate attribute and then call the base classes method.</p>
</div>
<!-- End of Main Content -->
<!-- Submenu -->
<div id="subnav">
<ul>
<li><a href="docHome.html">NUnit 2.2.3</a></li>
<ul>
<li><a href="getStarted.html">Getting Started</a></li>
<li><a href="installation.html">Installation</a></li>
<li><a href="features.html">Features</a></li>
<ul>
<li><a href="assertions.html">Assertions</a></li>
<li><a href="attributes.html">Attributes</a></li>
<ul>
<li><a href="testFixture.html">Test Fixture</a></li>
<li><a href="test.html">Test</a></li>
<li id="current"><a href="setupTeardown.html">Setup/Teardown</a></li>
<li><a href="exception.html">Expected Exception</a></li>
<li><a href="platform.html">Platform</a></li>
<li><a href="category.html">Category</a></li>
<li><a href="explicit.html">Explicit</a></li>
<li><a href="suite.html">Suite</a></li>
<li><a href="ignore.html">Ignore</a></li>
</ul>
<li><a href="configFiles.html">Configuration Files</a></li>
<li><a href="multiAssembly.html">Multiple Assemblies</a></li>
<li><a href="vsSupport.html">Visual Studio Support</a></li>
</ul>
<li><a href="nunit-console.html">NUnit-Console</a></li>
<li><a href="nunit-gui.html">NUnit-Gui</a></li>
<li><a href="releaseNotes.html">Release Notes</a></li>
<li><a href="samples.html">Samples</a></li>
<li><a href="license.html">License</a></li>
</ul>
</ul>
</div>
<!-- End of Submenu -->
<!-- Standard Footer for NUnit.org -->
<div id="footer">
Copyright © 2002-2005. All Rights Reserved.
</div>
<!-- End of Footer -->
</body>
</html>
|