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

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