Menu

[r37]: / osmb / trunk / build / cruisecontrol / apache-ant-1.7.0 / docs / antlibs / antunit / index.html  Maximize  Restore  History

Download this file

333 lines (303 with data), 16.0 kB

  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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<!-- GENERATED FILE, DO NOT EDIT, EDIT THE XML FILE IN xdocs INSTEAD! -->
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Apache Ant - AntUnit</title>
<link type="text/css" href="../../page.css" rel="stylesheet">
</head>
<body>
<p class="navpath">
<script src="../../breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
</p>
<div class="logobar">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left"><img border="0" alt="Apache Ant site" src="../../images/group-logo.gif"></td>
<td align="center" width="100%"><img alt="Apache Ant logo" border="0" src="../../images/project-logo.gif"></td>
<td align="right">
<form target="_blank" onsubmit="q.value = query.value + ' site:ant.apache.org'" action="http://www.google.com/search" method="get">
<table summary="search" border="0" cellspacing="0" cellpadding="0" bgcolor="#4C6C8F">
<tr>
<td colspan="3"><img height="10" width="1" alt="" src="../../images/spacer.gif"></td>
</tr>
<tr>
<td><img height="1" width="1" alt="" src="../../images/spacer.gif"></td>
<td nowrap="nowrap" class="searchcaption">
<input name="q" type="hidden">
<input size="15" id="query" type="text">
<img height="1" width="5" alt="" src="../../images/spacer.gif">
<input name="Search" value="Search" type="submit">
<br>
the Apache Ant site
</td>
<td><img height="1" width="1" alt="" src="../../images/spacer.gif"></td>
</tr>
<tr>
<td><img alt="" border="0" height="10" width="9" src="../../images/search-left.gif"></td>
<td><img height="1" width="1" alt="" src="../../images/spacer.gif"></td>
<td><img alt="" border="0" height="10" width="9" src="../../images/search-right.gif"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</div>
<div class="tab">
<table summary="tab bar" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="5"><img alt="" height="8" width="8" src="../../images/spacer.gif"></td><td valign="bottom">
<table summary="non selected tab" style="height: 1.4em" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" width="5" bgcolor="#B2C4E0"><img height="5" width="5" alt="" src="../../images/tab-left.gif"></td><td valign="middle" bgcolor="#B2C4E0"><a href="../../index.html"><font size="2" face="Arial, Helvetica, Sans-serif">Home</font></a></td><td valign="top" width="5" bgcolor="#B2C4E0"><img height="5" width="5" alt="" src="../../images/tab-right.gif"></td>
</tr>
</table>
</td>
<td width="8"><img alt="" height="5" width="8" src="../../images/spacer.gif"></td><td valign="bottom">
<table summary="selected tab" style="height: 1.5em" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" width="5" bgcolor="#4C6C8F"><img height="5" width="5" alt="" src="../../images/tabSel-left.gif"></td><td valign="middle" bgcolor="#4C6C8F"><font color="#ffffff" size="2" face="Arial, Helvetica, Sans-serif"><b>Projects</b></font></td><td valign="top" width="5" bgcolor="#4C6C8F"><img height="5" width="5" alt="" src="../../images/tabSel-right.gif"></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div class="bluebar"></div>
<div class="menucontainer">
<div class="menu">
<ul>
<li class="menuheader">Projects
<ul>
<li>
<a href="../../projects/index.html">Welcome</a>
</li>
</ul>
</li>
<li class="menuheader">Ant Libraries
<ul>
<li>
<a href="../../antlibs/index.html">Introduction</a>
</li>
<li>
<a href="../../antlibs/charter.html">Charter</a>
</li>
<li>
<a href="../../antlibs/proper.html">Ant Libraries</a>
</li>
<li>
<a href="../../antlibs/sandbox.html">Sandbox Ant Libraries</a>
</li>
<li>
<a href="http://ant.apache.org/antlibs/bindownload.cgi">Binary Distributions</a>
</li>
<li>
<a href="http://ant.apache.org/antlibs/srcdownload.cgi">Source Distributions</a>
</li>
</ul>
</li>
</ul>
</div>
<img style="float: left" height="10" width="10" border="0" alt="" src="../../images/menu-left.gif">
<img style="float: right" height="10" width="10" border="0" alt="" src="../../images/menu-right.gif">
</div>
<div class="lightbluebar">&nbsp;</div>
<div class="main">
<div class="content">
<h1 class="title">AntUnit</h1>
<h3 class="section">
<a name="AntUnit 1.0Beta2"></a>
AntUnit 1.0Beta2
</h3>
<h3>October 29, 2006 - Apache AntUnit 1.0Beta2 Available</h3>
<p>Apache AntUnit 1.0Beta2 is now available for download as <a href="http://ant.apache.org/antlibs/bindownload.cgi">binary</a>
or <a href="http://ant.apache.org/antlibs/srcdownload.cgi">source</a>
release.</p>
<h3 class="section">
<a name="Idea"></a>
Idea
</h3>
<p>Initially all tests for Ant tasks were written as individual
<a href="http://www.junit.org/">JUnit</a> test cases. Pretty
soon it was clear that most tests needed to perform common tasks
like reading a build file, initializing a project instance with
it and executing a target. At this point <a href="http://svn.apache.org/viewcvs.cgi/ant/core/trunk/src/testcases/org/apache/tools/ant/BuildFileTest.java">BuildFileTest</a>
was invented, a base class for almost all task test cases.</p>
<p>BuildFileTest works fine and in fact has been picked up by <a href="http://ant-contrib.sf.net/">the Ant-Contrib Project</a>
and others as well.</p>
<p>Over time a new pattern evolved, more and more tests only
executed a target and didn't check any effects. Instead that
target contained the assertions as a <code>&lt;fail&gt;</code>
task. This is an example taken from the build file for the
ANTLR task (using Ant 1.7 features):</p>
<pre class="code">
&lt;target name=&quot;test3&quot; depends=&quot;setup&quot;&gt;
&lt;antlr target=&quot;antlr.g&quot; outputdirectory=&quot;${tmp.dir}&quot;/&gt;
&lt;fail&gt;
&lt;condition&gt;
&lt;!-- to prove each of these files exists;
ANTLR &gt;= 2.7.6 leaves behind new (.smap) files as well. --&gt;
&lt;resourcecount when=&quot;ne&quot; count=&quot;5&quot;&gt;
&lt;fileset dir=&quot;${tmp.dir}&quot;&gt;
&lt;include name=&quot;CalcParserTokenTypes.txt&quot; /&gt;
&lt;include name=&quot;CalcParserTokenTypes.java&quot; /&gt;
&lt;include name=&quot;CalcLexer.java&quot; /&gt;
&lt;include name=&quot;CalcParser.java&quot; /&gt;
&lt;include name=&quot;CalcTreeWalker.java&quot; /&gt;
&lt;/fileset&gt;
&lt;/resourcecount&gt;
&lt;/condition&gt;
&lt;/fail&gt;
&lt;/target&gt;
</pre>
<p>where the corresponding JUnit testcase has been reduced
to</p>
<pre class="code">
...
public class ANTLRTest extends BuildFileTest {
private final static String TASKDEFS_DIR = &quot;src/etc/testcases/taskdefs/optional/antlr/&quot;;
public ANTLRTest(String name) {
super(name);
}
public void setUp() {
configureProject(TASKDEFS_DIR + &quot;antlr.xml&quot;);
}
public void tearDown() {
executeTarget(&quot;cleanup&quot;);
}
public void test3() {
executeTarget(&quot;test3&quot;);
}
...
}
</pre>
<p>This approach has a couple of advantages, one of them is that
it is very easy to translate an example build file from a bug
report into a test case. If you ask a user for a testcase for a
given bug in Ant, he now doesn't need to understand JUnit or how
to fit a test into Ant's existing tests any more.</p>
<p>AntUnit takes this approach to testing even further, it
removes JUnit completely and it comes with a set of predefined
<code>&lt;assert&gt;</code> tasks in order to reuse common kind
of checks.</p>
<p>It turns out that AntUnit lends itself as a solution to other
problems as well. The assertions are an easy way to validate a
setup before even starting the build process, for example.
AntUnit could also be used for functional and integration tests
outside of the scope of Ant tasks (assert contents of databases
after running an application, assert contents of HTTP responses
...). This is an area that will need more research.</p>
<h3 class="section">
<a name="Concepts"></a>
Concepts
</h3>
<h4 class="subsection">
<a name="antunit Task"></a>
antunit Task
</h4>
<p>The &lt;antunit&gt; task drives the tests much like
&lt;junit&gt; does for JUnit tests.</p>
<p>When called on a build file, the task will start a new Ant
project for that build file and scan for targets with names
that start with "test". For each such target it then will</p>
<ol>
<li>Execute the target named setUp, if there is one.</li>
<li>Execute the target itself - if this target depends on
other targets the normal Ant rules apply and the dependent
targets are executed first.</li>
<li>Execute the target names tearDown, if there is one.</li>
</ol>
<h4 class="subsection">
<a name="Assertions"></a>
Assertions
</h4>
<p>The base task is <code>&lt;assertTrue&gt;</code>. It
accepts a single nested condition and throws a subclass of
BuildException named AssertionFailedException if that
condition evaluates to false.</p>
<p>This task could have been implemented using
<code>&lt;macrodef&gt;</code> and <code>&lt;fail&gt;</code>,
but in fact it is a "real" task so that it is possible to
throw a subclass of BuildException. The
<code>&lt;antunit&gt;</code> task catches this exception and
marks the target as failed, any other type of Exception
(including other BuildException) are test errors.</p>
<p>Together with <code>&lt;assertTrue&gt;</code> there are
many predefined assertions for common conditions, most of
these are only macros.</p>
<h4 class="subsection">
<a name="Other Tasks"></a>
Other Tasks
</h4>
<p>The <code>&lt;logcapturer&gt;</code> captures all messages
that pass Ant's logging system and provides them via a
reference inside of the project. If you want to assert
certain log messages, you need to start this task (prior to
your target under test) and use the
<code>&lt;assertLogContains&gt;</code> assertion.</p>
<p><code>&lt;expectFailure&gt;</code> is a task container that
catches any BuildException thrown by tasks nested into it. If
no exception has been thrown it will cause a test failure (by
throwing an AssertionFailedException).</p>
<h4 class="subsection">
<a name="AntUnitListener"></a>
AntUnitListener
</h4>
<p>Part of the library is the <code>AntUnitListener</code>
interface that can be used to record test results. The
&lt;antunit&gt; task accepts arbitrary many listeners and
relays test results to them.</p>
<p>Currently two implementations -
<code>&lt;plainlistener&gt;</code> and <code>xmllistener</code>
modelled after the "plain" and "xml"
JUnit listeners - are bundled with the library.</p>
<h3 class="section">
<a name="Examples"></a>
Examples
</h3>
<p>This is a way to test that <code>&lt;touch&gt;</code>
actually creates a file if it doesn't exist:</p>
<pre class="code">
&lt;project xmlns:au=&quot;antlib:org.apache.ant.antunit&quot;&gt;
&lt;!-- is called prior to the test --&gt;
&lt;target name=&quot;setUp&quot;&gt;
&lt;property name=&quot;foo&quot; value=&quot;foo&quot;/&gt;
&lt;/target&gt;
&lt;!-- is called after the test, even if that caused an error --&gt;
&lt;target name=&quot;tearDown&quot;&gt;
&lt;delete file=&quot;${foo}&quot; quiet=&quot;true&quot;/&gt;
&lt;/target&gt;
&lt;!-- the actual test case --&gt;
&lt;target name=&quot;testTouchCreatesFile&quot;&gt;
&lt;au:assertFileDoesntExist file=&quot;${foo}&quot;/&gt;
&lt;touch file=&quot;${foo}&quot;/&gt;
&lt;au:assertFileExists file=&quot;${foo}&quot;/&gt;
&lt;/target&gt;
&lt;/project&gt;
</pre>
<p>When running a task like</p>
<pre class="code">
&lt;au:antunit&gt;
&lt;fileset dir=&quot;.&quot; includes=&quot;touch.xml&quot;/&gt;
&lt;au:plainlistener/&gt;
&lt;/au:antunit&gt;
</pre>
<p>from a buildfile of its own you'll get a result that looks like</p>
<pre class="code">
[au:antunit] Build File: /tmp/touch.xml
[au:antunit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.249 sec
[au:antunit] Target: testTouchCreatesFile took 0.183 sec
BUILD SUCCESSFUL
Total time: 1 second
</pre>
</div>
</div>
<p class="copyright">
<script type="text/javascript" language="JavaScript"><!--
document.write(" - "+"Last Published: " + document.lastModified);
// -->
</script>
</p>
</body>
</html>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.