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

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