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 kB

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