You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.html 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <html lang="en">
  17. <!-- GENERATED FILE, DO NOT EDIT, EDIT THE XML FILE IN xdocs INSTEAD! -->
  18. <head>
  19. <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  20. <title>Apache Ant - Apache AntUnit</title>
  21. <link type="text/css" href="../../page.css" rel="stylesheet">
  22. </head>
  23. <body>
  24. <p class="navpath">
  25. <script src="../../breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
  26. </p>
  27. <div class="logobar">
  28. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  29. <tr>
  30. <td align="left"><img border="0" alt="Apache Ant site" src="../../images/group-logo.gif"></td>
  31. <td align="center" width="100%"><img alt="Apache Ant logo" border="0" src="../../images/project-logo.gif"></td>
  32. <td align="right">
  33. <form target="_blank" onsubmit="q.value = query.value + ' site:ant.apache.org'" action="http://www.google.com/search" method="get">
  34. <table summary="search" border="0" cellspacing="0" cellpadding="0" bgcolor="#4C6C8F">
  35. <tr>
  36. <td colspan="3"><img height="10" width="1" alt="" src="../../images/spacer.gif"></td>
  37. </tr>
  38. <tr>
  39. <td><img height="1" width="1" alt="" src="../../images/spacer.gif"></td>
  40. <td nowrap="nowrap" class="searchcaption">
  41. <input name="q" type="hidden">
  42. <input size="15" id="query" type="text">
  43. <img height="1" width="5" alt="" src="../../images/spacer.gif">
  44. <input name="Search" value="Search" type="submit">
  45. <br>
  46. the Apache Ant site
  47. </td>
  48. <td><img height="1" width="1" alt="" src="../../images/spacer.gif"></td>
  49. </tr>
  50. <tr>
  51. <td><img alt="" border="0" height="10" width="9" src="../../images/search-left.gif"></td>
  52. <td><img height="1" width="1" alt="" src="../../images/spacer.gif"></td>
  53. <td><img alt="" border="0" height="10" width="9" src="../../images/search-right.gif"></td>
  54. </tr>
  55. </table>
  56. </form>
  57. </td>
  58. </tr>
  59. </table>
  60. </div>
  61. <div class="tab">
  62. <table summary="tab bar" border="0" cellpadding="0" cellspacing="0">
  63. <tr>
  64. <td width="5"><img alt="" height="8" width="8" src="../../images/spacer.gif"></td><td valign="bottom">
  65. <table summary="non selected tab" style="height: 1.4em" border="0" cellpadding="0" cellspacing="0">
  66. <tr>
  67. <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>
  68. </tr>
  69. </table>
  70. </td>
  71. <td width="8"><img alt="" height="5" width="8" src="../../images/spacer.gif"></td><td valign="bottom">
  72. <table summary="selected tab" style="height: 1.5em" border="0" cellpadding="0" cellspacing="0">
  73. <tr>
  74. <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>
  75. </tr>
  76. </table>
  77. </td>
  78. </tr>
  79. </table>
  80. </div>
  81. <div class="bluebar"></div>
  82. <div class="menucontainer">
  83. <div class="menu">
  84. <ul>
  85. <li class="menuheader">Projects
  86. <ul>
  87. <li>
  88. <a href="../../projects/index.html">Welcome</a>
  89. </li>
  90. </ul>
  91. </li>
  92. <li class="menuheader">Apache Ant Libraries
  93. <ul>
  94. <li>
  95. <a href="../../antlibs/index.html">Introduction</a>
  96. </li>
  97. <li>
  98. <a href="../../antlibs/charter.html">Charter</a>
  99. </li>
  100. <li>
  101. <a href="../../antlibs/proper.html">Ant Libraries</a>
  102. </li>
  103. <li>
  104. <a href="../../antlibs/sandbox.html">Sandbox Ant Libraries</a>
  105. </li>
  106. <li>
  107. <a href="http://ant.apache.org/antlibs/bindownload.cgi">Binary Distributions</a>
  108. </li>
  109. <li>
  110. <a href="http://ant.apache.org/antlibs/srcdownload.cgi">Source Distributions</a>
  111. </li>
  112. </ul>
  113. </li>
  114. <li class="menuheader">Apache Ivy
  115. <ul>
  116. <li>
  117. <a href="../../projects/ivy.html">Introduction</a>
  118. </li>
  119. <li>
  120. <a href="http://ant.apache.org/ivy/">Homepage</a>
  121. </li>
  122. <li>
  123. <a href="http://ant.apache.org/ivy/download.cgi">Distributions</a>
  124. </li>
  125. <li>
  126. <a href="http://cwiki.apache.org/IVY/">Wiki</a>
  127. </li>
  128. </ul>
  129. </li>
  130. <li class="menuheader">Apache IvyDE
  131. <ul>
  132. <li>
  133. <a href="../../projects/ivyde.html">Introduction</a>
  134. </li>
  135. <li>
  136. <a href="http://ant.apache.org/ivy/ivyde">Homepage</a>
  137. </li>
  138. <li>
  139. <a href="http://ant.apache.org/ivy/ivyde/screenshots.html">Screenshots</a>
  140. </li>
  141. <li>
  142. <a href="http://ant.apache.org/ivy/ivyde/download.cgi">Distributions</a>
  143. </li>
  144. </ul>
  145. </li>
  146. </ul>
  147. </div>
  148. <center>
  149. <a href="http://www.apache.org/events/current-event.html"><img border="0" title="ApacheCon" alt="ApacheCon - logo" src="http://www.apache.org/events/current-event-125x125.png" style="width: 125px;height: 125px;"></a>
  150. </center>
  151. <img style="float: left" height="10" width="10" border="0" alt="" src="../../images/menu-left.gif">
  152. <img style="float: right" height="10" width="10" border="0" alt="" src="../../images/menu-right.gif">
  153. </div>
  154. <div class="lightbluebar">&nbsp;</div>
  155. <div class="main">
  156. <div class="content">
  157. <h1 class="title">Apache AntUnit</h1>
  158. <h3 class="section">
  159. <a name="Apache AntUnit&amp;#x2122;"></a>
  160. Apache AntUnit&#x2122;
  161. </h3>
  162. <p>Apache AntUnit is an Antlib that provides a test framework
  163. for Apache Ant tasks and types.</p>
  164. <h3 class="section">
  165. <a name="Apache AntUnit 1.1"></a>
  166. Apache AntUnit 1.1
  167. </h3>
  168. <h3>September 26, 2008 - Apache AntUnit 1.1 Released</h3>
  169. <p>Apache AntUnit 1.1 is now available for download as <a href="http://ant.apache.org/antlibs/bindownload.cgi">binary</a>
  170. or <a href="http://ant.apache.org/antlibs/srcdownload.cgi">source</a>
  171. release.</p>
  172. <p>In addition to a few bugfixes and some new assertions AntUnit
  173. 1.1 allows test listeners to receive the log output of the
  174. project under test. Both plainlistener and xmllistener have
  175. an option that makes them echo the project's output into their
  176. respective logs.</p>
  177. <h3 class="section">
  178. <a name="AntUnit 1.1 Beta 1"></a>
  179. AntUnit 1.1 Beta 1
  180. </h3>
  181. <h3>September 3, 2008 - Apache AntUnit 1.1 Beta 1 Available</h3>
  182. <h3 class="section">
  183. <a name="AntUnit 1.0"></a>
  184. AntUnit 1.0
  185. </h3>
  186. <h3>January 8, 2007 - Apache AntUnit 1.0 Available</h3>
  187. <p>Apache AntUnit 1.0 is now available for download as <a href="http://ant.apache.org/antlibs/bindownload.cgi">binary</a>
  188. or <a href="http://ant.apache.org/antlibs/srcdownload.cgi">source</a>
  189. release.</p>
  190. <h3 class="section">
  191. <a name="Idea"></a>
  192. Idea
  193. </h3>
  194. <p>Initially all tests for Apache Ant tasks were written as individual
  195. <a href="http://www.junit.org/">JUnit</a> test cases. Pretty
  196. soon it was clear that most tests needed to perform common tasks
  197. like reading a build file, initializing a project instance with
  198. it and executing a target. At this point <a href="http://svn.apache.org/viewvc/ant/core/trunk/src/testcases/org/apache/tools/ant/BuildFileTest.java">BuildFileTest</a>
  199. was invented, a base class for almost all task test cases.</p>
  200. <p>BuildFileTest works fine and in fact has been picked up by <a href="http://ant-contrib.sf.net/">the Ant-Contrib Project</a>
  201. and others as well.</p>
  202. <p>Over time a new pattern evolved, more and more tests only
  203. executed a target and didn't check any effects. Instead that
  204. target contained the assertions as a <code>&lt;fail&gt;</code>
  205. task. This is an example taken from the build file for the
  206. ANTLR task (using Ant 1.7 features):</p>
  207. <pre class="code">
  208. &lt;target name=&quot;test3&quot; depends=&quot;setup&quot;&gt;
  209. &lt;antlr target=&quot;antlr.g&quot; outputdirectory=&quot;${tmp.dir}&quot;/&gt;
  210. &lt;fail&gt;
  211. &lt;condition&gt;
  212. &lt;!-- to prove each of these files exists;
  213. ANTLR &gt;= 2.7.6 leaves behind new (.smap) files as well. --&gt;
  214. &lt;resourcecount when=&quot;ne&quot; count=&quot;5&quot;&gt;
  215. &lt;fileset dir=&quot;${tmp.dir}&quot;&gt;
  216. &lt;include name=&quot;CalcParserTokenTypes.txt&quot; /&gt;
  217. &lt;include name=&quot;CalcParserTokenTypes.java&quot; /&gt;
  218. &lt;include name=&quot;CalcLexer.java&quot; /&gt;
  219. &lt;include name=&quot;CalcParser.java&quot; /&gt;
  220. &lt;include name=&quot;CalcTreeWalker.java&quot; /&gt;
  221. &lt;/fileset&gt;
  222. &lt;/resourcecount&gt;
  223. &lt;/condition&gt;
  224. &lt;/fail&gt;
  225. &lt;/target&gt;
  226. </pre>
  227. <p>where the corresponding JUnit testcase has been reduced
  228. to</p>
  229. <pre class="code">
  230. ...
  231. public class ANTLRTest extends BuildFileTest {
  232. private final static String TASKDEFS_DIR = &quot;src/etc/testcases/taskdefs/optional/antlr/&quot;;
  233. public ANTLRTest(String name) {
  234. super(name);
  235. }
  236. public void setUp() {
  237. configureProject(TASKDEFS_DIR + &quot;antlr.xml&quot;);
  238. }
  239. public void tearDown() {
  240. executeTarget(&quot;cleanup&quot;);
  241. }
  242. public void test3() {
  243. executeTarget(&quot;test3&quot;);
  244. }
  245. ...
  246. }
  247. </pre>
  248. <p>This approach has a couple of advantages, one of them is that
  249. it is very easy to translate an example build file from a bug
  250. report into a test case. If you ask a user for a testcase for a
  251. given bug in Ant, he now doesn't need to understand JUnit or how
  252. to fit a test into Ant's existing tests any more.</p>
  253. <p>AntUnit takes this approach to testing even further, it
  254. removes JUnit completely and it comes with a set of predefined
  255. <code>&lt;assert&gt;</code> tasks in order to reuse common kind
  256. of checks.</p>
  257. <p>It turns out that AntUnit lends itself as a solution to other
  258. problems as well. The assertions are an easy way to validate a
  259. setup before even starting the build process, for example.
  260. AntUnit could also be used for functional and integration tests
  261. outside of the scope of Ant tasks (assert contents of databases
  262. after running an application, assert contents of HTTP responses
  263. ...). This is an area that will need more research.</p>
  264. <h3 class="section">
  265. <a name="Concepts"></a>
  266. Concepts
  267. </h3>
  268. <h4 class="subsection">
  269. <a name="antunit Task"></a>
  270. antunit Task
  271. </h4>
  272. <p>The &lt;antunit&gt; task drives the tests much like
  273. &lt;junit&gt; does for JUnit tests.</p>
  274. <p>When called on a build file, the task will start a new Ant
  275. project for that build file and scan for targets with names
  276. that start with "test". For each such target it then will</p>
  277. <ol>
  278. <li>Execute the target named setUp, if there is one.</li>
  279. <li>Execute the target itself - if this target depends on
  280. other targets the normal Ant rules apply and the dependent
  281. targets are executed first.</li>
  282. <li>Execute the target names tearDown, if there is one.</li>
  283. </ol>
  284. <h4 class="subsection">
  285. <a name="Assertions"></a>
  286. Assertions
  287. </h4>
  288. <p>The base task is <code>&lt;assertTrue&gt;</code>. It
  289. accepts a single nested condition and throws a subclass of
  290. BuildException named AssertionFailedException if that
  291. condition evaluates to false.</p>
  292. <p>This task could have been implemented using
  293. <code>&lt;macrodef&gt;</code> and <code>&lt;fail&gt;</code>,
  294. but in fact it is a "real" task so that it is possible to
  295. throw a subclass of BuildException. The
  296. <code>&lt;antunit&gt;</code> task catches this exception and
  297. marks the target as failed, any other type of Exception
  298. (including other BuildException) are test errors.</p>
  299. <p>Together with <code>&lt;assertTrue&gt;</code> there are
  300. many predefined assertions for common conditions, most of
  301. these are only macros.</p>
  302. <h4 class="subsection">
  303. <a name="Other Tasks"></a>
  304. Other Tasks
  305. </h4>
  306. <p>The <code>&lt;logcapturer&gt;</code> captures all messages
  307. that pass Ant's logging system and provides them via a
  308. reference inside of the project. If you want to assert
  309. certain log messages, you need to start this task (prior to
  310. your target under test) and use the
  311. <code>&lt;assertLogContains&gt;</code> assertion.</p>
  312. <p><code>&lt;expectFailure&gt;</code> is a task container that
  313. catches any BuildException thrown by tasks nested into it. If
  314. no exception has been thrown it will cause a test failure (by
  315. throwing an AssertionFailedException).</p>
  316. <h4 class="subsection">
  317. <a name="AntUnitListener"></a>
  318. AntUnitListener
  319. </h4>
  320. <p>Part of the library is the <code>AntUnitListener</code>
  321. interface that can be used to record test results. The
  322. &lt;antunit&gt; task accepts arbitrary many listeners and
  323. relays test results to them.</p>
  324. <p>Currently two implementations -
  325. <code>&lt;plainlistener&gt;</code> and <code>xmllistener</code>
  326. modelled after the "plain" and "xml"
  327. JUnit listeners - are bundled with the library.</p>
  328. <h3 class="section">
  329. <a name="Examples"></a>
  330. Examples
  331. </h3>
  332. <p>This is a way to test that <code>&lt;touch&gt;</code>
  333. actually creates a file if it doesn't exist:</p>
  334. <pre class="code">
  335. &lt;project xmlns:au=&quot;antlib:org.apache.ant.antunit&quot;&gt;
  336. &lt;!-- is called prior to the test --&gt;
  337. &lt;target name=&quot;setUp&quot;&gt;
  338. &lt;property name=&quot;foo&quot; value=&quot;foo&quot;/&gt;
  339. &lt;/target&gt;
  340. &lt;!-- is called after the test, even if that caused an error --&gt;
  341. &lt;target name=&quot;tearDown&quot;&gt;
  342. &lt;delete file=&quot;${foo}&quot; quiet=&quot;true&quot;/&gt;
  343. &lt;/target&gt;
  344. &lt;!-- the actual test case --&gt;
  345. &lt;target name=&quot;testTouchCreatesFile&quot;&gt;
  346. &lt;au:assertFileDoesntExist file=&quot;${foo}&quot;/&gt;
  347. &lt;touch file=&quot;${foo}&quot;/&gt;
  348. &lt;au:assertFileExists file=&quot;${foo}&quot;/&gt;
  349. &lt;/target&gt;
  350. &lt;/project&gt;
  351. </pre>
  352. <p>When running a task like</p>
  353. <pre class="code">
  354. &lt;au:antunit&gt;
  355. &lt;fileset dir=&quot;.&quot; includes=&quot;touch.xml&quot;/&gt;
  356. &lt;au:plainlistener/&gt;
  357. &lt;/au:antunit&gt;
  358. </pre>
  359. <p>from a buildfile of its own you'll get a result that looks like</p>
  360. <pre class="code">
  361. [au:antunit] Build File: /tmp/touch.xml
  362. [au:antunit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.249 sec
  363. [au:antunit] Target: testTouchCreatesFile took 0.183 sec
  364. BUILD SUCCESSFUL
  365. Total time: 1 second
  366. </pre>
  367. </div>
  368. </div>
  369. <p class="copyright">
  370. Apache Ant, Apache Ivy, Ant, Ivy, Apache, the Apache feather logo, and the Apache Ant project logos are trademarks of The Apache Software Foundation.
  371. <script type="text/javascript" language="JavaScript"><!--
  372. document.write(" - "+"Last Published: " + document.lastModified);
  373. // -->
  374. </script>
  375. </p>
  376. </body>
  377. </html>