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 19 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 - 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">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">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">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">AntUnit</h1>
  158. <h3 class="section">
  159. <a name="AntUnit 1.1"></a>
  160. AntUnit 1.1
  161. </h3>
  162. <h3>September 26, 2008 - Apache AntUnit 1.1 Released</h3>
  163. <p>Apache AntUnit 1.1 is now available for download as <a href="http://ant.apache.org/antlibs/bindownload.cgi">binary</a>
  164. or <a href="http://ant.apache.org/antlibs/srcdownload.cgi">source</a>
  165. release.</p>
  166. <p>In addition to a few bugfixes and some new assertions AntUnit
  167. 1.1 allows test listeners to receive the log output of the
  168. project under test. Both plainlistener and xmllistener have
  169. an option that makes them echo the project's output into their
  170. respective logs.</p>
  171. <h3 class="section">
  172. <a name="AntUnit 1.1 Beta 1"></a>
  173. AntUnit 1.1 Beta 1
  174. </h3>
  175. <h3>September 3, 2008 - Apache AntUnit 1.1 Beta 1 Available</h3>
  176. <h3 class="section">
  177. <a name="AntUnit 1.0"></a>
  178. AntUnit 1.0
  179. </h3>
  180. <h3>January 8, 2007 - Apache AntUnit 1.0 Available</h3>
  181. <p>Apache AntUnit 1.0 is now available for download as <a href="http://ant.apache.org/antlibs/bindownload.cgi">binary</a>
  182. or <a href="http://ant.apache.org/antlibs/srcdownload.cgi">source</a>
  183. release.</p>
  184. <h3 class="section">
  185. <a name="Idea"></a>
  186. Idea
  187. </h3>
  188. <p>Initially all tests for Ant tasks were written as individual
  189. <a href="http://www.junit.org/">JUnit</a> test cases. Pretty
  190. soon it was clear that most tests needed to perform common tasks
  191. like reading a build file, initializing a project instance with
  192. 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>
  193. was invented, a base class for almost all task test cases.</p>
  194. <p>BuildFileTest works fine and in fact has been picked up by <a href="http://ant-contrib.sf.net/">the Ant-Contrib Project</a>
  195. and others as well.</p>
  196. <p>Over time a new pattern evolved, more and more tests only
  197. executed a target and didn't check any effects. Instead that
  198. target contained the assertions as a <code>&lt;fail&gt;</code>
  199. task. This is an example taken from the build file for the
  200. ANTLR task (using Ant 1.7 features):</p>
  201. <pre class="code">
  202. &lt;target name=&quot;test3&quot; depends=&quot;setup&quot;&gt;
  203. &lt;antlr target=&quot;antlr.g&quot; outputdirectory=&quot;${tmp.dir}&quot;/&gt;
  204. &lt;fail&gt;
  205. &lt;condition&gt;
  206. &lt;!-- to prove each of these files exists;
  207. ANTLR &gt;= 2.7.6 leaves behind new (.smap) files as well. --&gt;
  208. &lt;resourcecount when=&quot;ne&quot; count=&quot;5&quot;&gt;
  209. &lt;fileset dir=&quot;${tmp.dir}&quot;&gt;
  210. &lt;include name=&quot;CalcParserTokenTypes.txt&quot; /&gt;
  211. &lt;include name=&quot;CalcParserTokenTypes.java&quot; /&gt;
  212. &lt;include name=&quot;CalcLexer.java&quot; /&gt;
  213. &lt;include name=&quot;CalcParser.java&quot; /&gt;
  214. &lt;include name=&quot;CalcTreeWalker.java&quot; /&gt;
  215. &lt;/fileset&gt;
  216. &lt;/resourcecount&gt;
  217. &lt;/condition&gt;
  218. &lt;/fail&gt;
  219. &lt;/target&gt;
  220. </pre>
  221. <p>where the corresponding JUnit testcase has been reduced
  222. to</p>
  223. <pre class="code">
  224. ...
  225. public class ANTLRTest extends BuildFileTest {
  226. private final static String TASKDEFS_DIR = &quot;src/etc/testcases/taskdefs/optional/antlr/&quot;;
  227. public ANTLRTest(String name) {
  228. super(name);
  229. }
  230. public void setUp() {
  231. configureProject(TASKDEFS_DIR + &quot;antlr.xml&quot;);
  232. }
  233. public void tearDown() {
  234. executeTarget(&quot;cleanup&quot;);
  235. }
  236. public void test3() {
  237. executeTarget(&quot;test3&quot;);
  238. }
  239. ...
  240. }
  241. </pre>
  242. <p>This approach has a couple of advantages, one of them is that
  243. it is very easy to translate an example build file from a bug
  244. report into a test case. If you ask a user for a testcase for a
  245. given bug in Ant, he now doesn't need to understand JUnit or how
  246. to fit a test into Ant's existing tests any more.</p>
  247. <p>AntUnit takes this approach to testing even further, it
  248. removes JUnit completely and it comes with a set of predefined
  249. <code>&lt;assert&gt;</code> tasks in order to reuse common kind
  250. of checks.</p>
  251. <p>It turns out that AntUnit lends itself as a solution to other
  252. problems as well. The assertions are an easy way to validate a
  253. setup before even starting the build process, for example.
  254. AntUnit could also be used for functional and integration tests
  255. outside of the scope of Ant tasks (assert contents of databases
  256. after running an application, assert contents of HTTP responses
  257. ...). This is an area that will need more research.</p>
  258. <h3 class="section">
  259. <a name="Concepts"></a>
  260. Concepts
  261. </h3>
  262. <h4 class="subsection">
  263. <a name="antunit Task"></a>
  264. antunit Task
  265. </h4>
  266. <p>The &lt;antunit&gt; task drives the tests much like
  267. &lt;junit&gt; does for JUnit tests.</p>
  268. <p>When called on a build file, the task will start a new Ant
  269. project for that build file and scan for targets with names
  270. that start with "test". For each such target it then will</p>
  271. <ol>
  272. <li>Execute the target named setUp, if there is one.</li>
  273. <li>Execute the target itself - if this target depends on
  274. other targets the normal Ant rules apply and the dependent
  275. targets are executed first.</li>
  276. <li>Execute the target names tearDown, if there is one.</li>
  277. </ol>
  278. <h4 class="subsection">
  279. <a name="Assertions"></a>
  280. Assertions
  281. </h4>
  282. <p>The base task is <code>&lt;assertTrue&gt;</code>. It
  283. accepts a single nested condition and throws a subclass of
  284. BuildException named AssertionFailedException if that
  285. condition evaluates to false.</p>
  286. <p>This task could have been implemented using
  287. <code>&lt;macrodef&gt;</code> and <code>&lt;fail&gt;</code>,
  288. but in fact it is a "real" task so that it is possible to
  289. throw a subclass of BuildException. The
  290. <code>&lt;antunit&gt;</code> task catches this exception and
  291. marks the target as failed, any other type of Exception
  292. (including other BuildException) are test errors.</p>
  293. <p>Together with <code>&lt;assertTrue&gt;</code> there are
  294. many predefined assertions for common conditions, most of
  295. these are only macros.</p>
  296. <h4 class="subsection">
  297. <a name="Other Tasks"></a>
  298. Other Tasks
  299. </h4>
  300. <p>The <code>&lt;logcapturer&gt;</code> captures all messages
  301. that pass Ant's logging system and provides them via a
  302. reference inside of the project. If you want to assert
  303. certain log messages, you need to start this task (prior to
  304. your target under test) and use the
  305. <code>&lt;assertLogContains&gt;</code> assertion.</p>
  306. <p><code>&lt;expectFailure&gt;</code> is a task container that
  307. catches any BuildException thrown by tasks nested into it. If
  308. no exception has been thrown it will cause a test failure (by
  309. throwing an AssertionFailedException).</p>
  310. <h4 class="subsection">
  311. <a name="AntUnitListener"></a>
  312. AntUnitListener
  313. </h4>
  314. <p>Part of the library is the <code>AntUnitListener</code>
  315. interface that can be used to record test results. The
  316. &lt;antunit&gt; task accepts arbitrary many listeners and
  317. relays test results to them.</p>
  318. <p>Currently two implementations -
  319. <code>&lt;plainlistener&gt;</code> and <code>xmllistener</code>
  320. modelled after the "plain" and "xml"
  321. JUnit listeners - are bundled with the library.</p>
  322. <h3 class="section">
  323. <a name="Examples"></a>
  324. Examples
  325. </h3>
  326. <p>This is a way to test that <code>&lt;touch&gt;</code>
  327. actually creates a file if it doesn't exist:</p>
  328. <pre class="code">
  329. &lt;project xmlns:au=&quot;antlib:org.apache.ant.antunit&quot;&gt;
  330. &lt;!-- is called prior to the test --&gt;
  331. &lt;target name=&quot;setUp&quot;&gt;
  332. &lt;property name=&quot;foo&quot; value=&quot;foo&quot;/&gt;
  333. &lt;/target&gt;
  334. &lt;!-- is called after the test, even if that caused an error --&gt;
  335. &lt;target name=&quot;tearDown&quot;&gt;
  336. &lt;delete file=&quot;${foo}&quot; quiet=&quot;true&quot;/&gt;
  337. &lt;/target&gt;
  338. &lt;!-- the actual test case --&gt;
  339. &lt;target name=&quot;testTouchCreatesFile&quot;&gt;
  340. &lt;au:assertFileDoesntExist file=&quot;${foo}&quot;/&gt;
  341. &lt;touch file=&quot;${foo}&quot;/&gt;
  342. &lt;au:assertFileExists file=&quot;${foo}&quot;/&gt;
  343. &lt;/target&gt;
  344. &lt;/project&gt;
  345. </pre>
  346. <p>When running a task like</p>
  347. <pre class="code">
  348. &lt;au:antunit&gt;
  349. &lt;fileset dir=&quot;.&quot; includes=&quot;touch.xml&quot;/&gt;
  350. &lt;au:plainlistener/&gt;
  351. &lt;/au:antunit&gt;
  352. </pre>
  353. <p>from a buildfile of its own you'll get a result that looks like</p>
  354. <pre class="code">
  355. [au:antunit] Build File: /tmp/touch.xml
  356. [au:antunit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.249 sec
  357. [au:antunit] Target: testTouchCreatesFile took 0.183 sec
  358. BUILD SUCCESSFUL
  359. Total time: 1 second
  360. </pre>
  361. </div>
  362. </div>
  363. <p class="copyright">
  364. <script type="text/javascript" language="JavaScript"><!--
  365. document.write(" - "+"Last Published: " + document.lastModified);
  366. // -->
  367. </script>
  368. </p>
  369. </body>
  370. </html>