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 16 kB

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